JCheckBox value in JTable

后端 未结 2 544
耶瑟儿~
耶瑟儿~ 2020-12-20 03:02

JCheckBox is checked but it still shows its value as false when i use system.out.print .If the focus is lost but with JCheckBox still checked then

相关标签:
2条回答
  • 2020-12-20 03:29

    For reference, this sscce illustrates @ StanislavL's point. Note that autoboxing, introduced in Java 5, results in column three having type Boolean. At run time, the overridden getColumnClass() will return Boolean.class for index CHECK_COL. Finally, Swing GUI objects should be constructed on the event dispatch thread.

    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();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-20 03:45

    May be it's better to override getColumnClass in the model to return Boolean. Default Renderer/Editor handle the case and you can see/use checkbox.

    0 讨论(0)
提交回复
热议问题