Java JTable中的JCheckBox值

Java JTable中的JCheckBox值,java,swing,jtable,jcheckbox,Java,Swing,Jtable,Jcheckbox,选中了JCheckBox,但当我使用system.out.print时,它仍将其值显示为false。如果焦点丢失,但仍选中了JCheckBox,则返回true。 i、 e当我选中2复选框时,显示的结果是第一个复选框。未显示第二个复选框的状态。完整的程序如下所示:请运行程序并纠正我的错误。欢迎任何帮助 public class check extends JFrame { public check() { setBounds(00, 40, 400, 400);

选中了
JCheckBox
,但当我使用system.out.print时,它仍将其值显示为false。如果焦点丢失,但仍选中了
JCheckBox
,则返回true。 i、 e当我选中2复选框时,显示的结果是第一个复选框。未显示第二个复选框的状态。完整的程序如下所示:请运行程序并纠正我的错误。欢迎任何帮助

public class check extends JFrame {

    public check() {
        setBounds(00, 40, 400, 400);
        Color c = new Color(160, 200, 100);
        getContentPane().setBackground(c);
        Color c3 = new Color(0, 50, 50, 2);
        setTitle("MARKING OF TARGET HABITATION");
        setUndecorated(true);
        getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String[] columnNames = {"Country", "Capital", "Population in Millions",
            "Democracy"};
        Object[][] data = {
            {"USA", "Washington DC", 280, new Boolean(false)},
            {"Canada", "Ottawa", 32, new Boolean(false)},
            {"United Kingdom", "London", 60, new Boolean(false)},
            {"Germany", "Berlin", 83, new Boolean(false)},
            {"France", "Paris", 60, new Boolean(false)},
            {"Norway", "Oslo", 4.5, new Boolean(false)},
            {"India", "New Deli", 1046, new Boolean(false)}
        };

        final JTable table = new JTable(data, columnNames) {

            public boolean isCellEditable(int rowIndex, int colIndex) {
                return (colIndex == 3);//Disallow the editing of any cell
            }
        };
        table.getColumnModel().getColumn(3).setCellEditor(new CheckBoxCellEditor());
        table.getColumnModel().getColumn(3).setCellRenderer(new CWCheckBoxRenderer());
        JScrollPane scrollPane = new JScrollPane(table);
        JButton button = new JButton("check");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                boolean[] rsel = new boolean[table.getRowCount()];
                for (int i = 0; i < table.getRowCount(); i++) {
                    rsel[i] = (boolean) table.getValueAt(i, 3);
                    if (rsel[i]) {
                        System.out.println("checkbox selected at row"
                            + " " + i + "is" + rsel[i]);
                        for (int col = 0; col <= 3; col++) {
                            table.getValueAt(i, col);
                            System.out.println("data at row" + " "
                                + i + "is" + table.getValueAt(i, col));
                        }
                    }
                }
            }
        });
        JPanel buttonpanel = new JPanel();
        buttonpanel.add(button);
        add(scrollPane, BorderLayout.CENTER);
        add(buttonpanel, BorderLayout.SOUTH);
        Color c1 = new Color(160, 200, 100);
        table.setBackground(c1);
        buttonpanel.setBackground(c1);
        setBackground(c1);
        setVisible(true);
    }

    public static void main(String args[]) {
        new check();

    }

    class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {

        private static final long serialVersionUID = 1L;
        protected JCheckBox checkBox;

        public CheckBoxCellEditor() {
            checkBox = new JCheckBox();
            checkBox.setHorizontalAlignment(SwingConstants.CENTER);
            // checkBox.setBackground( Color.white);
        }

        public Component getTableCellEditorComponent(
            JTable table,
            Object value,
            boolean isSelected,
            int row,
            int column) {
            checkBox.setSelected(((Boolean) value).booleanValue());
            return checkBox;

        }

        public Object getCellEditorValue() {
            return Boolean.valueOf(checkBox.isSelected());
        }
    }

    class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {

        private static final long serialVersionUID = 1L;
        Border border = new EmptyBorder(1, 2, 1, 2);

        public CWCheckBoxRenderer() {
            super();
            setOpaque(true);
            setHorizontalAlignment(SwingConstants.CENTER);
        }

        public Component getTableCellRendererComponent(
            JTable table,
            Object value,
            boolean isSelected,
            boolean hasFocus,
            int row,
            int column) {
            if (value instanceof Boolean) {
                setSelected(((Boolean) value).booleanValue());
                //setEnabled(table.isCellEditable(row, column));
                setForeground(table.getForeground());
                setBackground(table.getBackground());

            }
            return this;
        }
    }
}
公共类检查扩展JFrame{
公开检查(){
立根(00,40,400,400);
颜色c=新颜色(160200100);
getContentPane().setBackground(c);
颜色c3=新颜色(0,50,50,2);
setTitle(“目标居住区标记”);
未装饰的设置(真实);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_对话框);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[]columnNames={“国家”、“首都”、“百万人口”,
“民主”};
对象[][]数据={
{“美国”,“华盛顿特区”,280,新布尔值(假)},
{“加拿大”、“渥太华”,32,新布尔值(假)},
{“联合王国”、“伦敦”,60,新布尔值(假)},
{“德国”,“柏林”,83,新布尔值(假)},
{“法国”,“巴黎”,60,新布尔值(假)},
{“挪威”,“奥斯陆”,4.5,新布尔值(假)},
{“印度”,“新德利”,1046,新布尔值(假)}
};
最终JTable表=新JTable(数据、列名){
公共布尔值isCellEditable(int rowIndex,int colIndex){
return(colIndex==3);//不允许编辑任何单元格
}
};
table.getColumnModel().getColumn(3).setCellEditor(新的CheckBoxCellEditor());
table.getColumnModel().getColumn(3).setCellRenderer(新的CWCheckBoxRenderer());
JScrollPane scrollPane=新的JScrollPane(表);
JButton按钮=新JButton(“检查”);
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
boolean[]rsel=new boolean[table.getRowCount()];
对于(int i=0;i对于(int col=0;col可能最好在模型中重写getColumnClass以返回布尔值。默认呈现器/编辑器处理该情况,您可以查看/使用复选框。

作为参考,这说明了@StanislavL的观点。请注意,在Java 5中引入的第三列具有类型
布尔值。
。在运行时,重写的
getColumnClass()
将返回
Boolean.class
作为索引
的CHECK\u COL
。最后,应在上构造Swing GUI对象

导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.table.DefaultTableModel;
公共类检查扩展了JFrame{
专用静态最终整数检查\u COL=3;
公开检查(){
setTitle(“目标居住区标记”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
字符串[]列名称={
“国家”、“首都”、“百万人口”、“民主”};
对象[][]数据={
{“美国”,“华盛顿特区”,280,真的},
{“加拿大”、“渥太华”,32,假},
{“联合王国”、“伦敦”,60,假},
{“德国”、“柏林”,83,假},
{“法国”,“巴黎”,60,假},
{“挪威”、“奥斯陆”,4.5,假},
{“印度”,“新熟食店”,1046,假}
};
DefaultTableModel dtm=新的DefaultTableModel(数据、列名){
@凌驾
公共类getColumnClass(int-col){
返回getValueAt(0,col).getClass();
}
@凌驾
公共布尔值isCellEditable(int rowIndex,int colIndex){
返回值(colIndex==检查列);
}
};
最终JTable表=新JTable(dtm);
JScrollPane scrollPane=新的JScrollPane(表);
JButton按钮=新JButton(“检查”);
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
对于(int row=0;row
请提供一个示例以获得帮助。仅编辑器/渲染器不足以调查此问题。您还可以查看其中还有一个您希望实现的相同行为的示例。请重新修改您的检查。我不确定应该做什么,但我怀疑它是否做了您认为它在做的事情。您使用
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Check extends JFrame {

    private static final int CHECK_COL = 3;

    public Check() {
        setTitle("MARKING OF TARGET HABITATION");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String[] columnNames = {
            "Country", "Capital", "Population in Millions", "Democracy"};
        Object[][] data = {
            {"USA", "Washington DC", 280, true},
            {"Canada", "Ottawa", 32, false},
            {"United Kingdom", "London", 60, false},
            {"Germany", "Berlin", 83, false},
            {"France", "Paris", 60, false},
            {"Norway", "Oslo", 4.5, false},
            {"India", "New Deli", 1046, false}
        };
        DefaultTableModel dtm = new DefaultTableModel(data, columnNames) {

            @Override
            public Class getColumnClass(int col) {
                return getValueAt(0, col).getClass();
            }

            @Override
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return (colIndex == CHECK_COL);
            }
        };
        final JTable table = new JTable(dtm);
        JScrollPane scrollPane = new JScrollPane(table);
        JButton button = new JButton("check");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (int row = 0; row < table.getRowCount(); row++) {
                    Boolean b = ((Boolean) table.getValueAt(row, CHECK_COL));
                    if (b.booleanValue()) {
                        System.out.print("row " + row + " is " + b + ": ");
                        for (int col = 0; col < table.getColumnCount(); col++) {
                            System.out.print(table.getValueAt(row, col) + " ");
                        }
                        System.out.println();
                    }
                }
            }
        });
        JPanel buttonpanel = new JPanel();
        buttonpanel.add(button);
        add(scrollPane, BorderLayout.CENTER);
        add(buttonpanel, BorderLayout.SOUTH);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Check();
            }
        });
    }
}