rendered multiple many example check java swing jtable jcheckbox

java - multiple - Valor JCheckBox en JTable



selectonemenu multiple select (2)

JCheckBox está marcado, pero aún muestra su valor como falso cuando uso system.out.print. Si se pierde el foco pero con JCheckBox aún marcado, se devuelve verdadero. es decir, cuando marque 2 casilla de verificación, el resultado que se muestra es para la primera casilla de verificación. el estado de la segunda casilla de verificación no se muestra. El progreso completo se muestra a continuación: los pls ejecutan el progreso y rectifican mi error. cualquier ayuda es bienvenida.

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; } } }


Como referencia, esta escena ilustra el punto de @ StanislavL. Tenga en cuenta que el autoboxing , introducido en Java 5, da como resultado que la columna tres tenga un tipo Boolean . En tiempo de ejecución, getColumnClass() reemplazado getColumnClass() devolverá Boolean.class para el índice CHECK_COL . Finalmente, los objetos de Swing GUI se deben construir en el hilo de envío del evento .

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(); } }); } }


Puede ser que sea mejor anular getColumnClass en el modelo para devolver Boolean. El Renderer / Editor predeterminado maneja la caja y puede ver / usar la casilla de verificación.