Using setValueAt to recreate mutually exclusive check boxes?

Don't add your own MouseListener Instead override setValueAt() in the TableModel to see the new value set by the default editor for Boolean. Class Addendum: Here's an sscce For expedience, it simply clears all entries in CHECK_COL sets the new value and conditions the button accordingly import java.awt. *; import javax.swing.

*; import javax.swing.table. DefaultTableModel; /** * @see stackoverflow.com/questions/7920068 * @see stackoverflow.com/questions/4526779 */ public class CheckOne extends JPanel { private static final int CHECK_COL = 1; private static final Object DATA = { {"One", Boolean. FALSE}, {"Two", Boolean.

FALSE}, {"Three", Boolean. FALSE}, {"Four", Boolean. FALSE}, {"Five", Boolean.

FALSE}, {"Six", Boolean. FALSE}, {"Seven", Boolean. FALSE}, {"Eight", Boolean.

FALSE}, {"Nine", Boolean. FALSE}, {"Ten", Boolean. FALSE}}; private static final String COLUMNS = {"Number", "CheckBox"}; private DataModel dataModel = new DataModel(DATA, COLUMNS); private JTable table = new JTable(dataModel); private ControlPanel cp = new ControlPanel(); public CheckOne() { super(new BorderLayout()); this.

Add(new JScrollPane(table)); this. Add(cp, BorderLayout. SOUTH); table.

SetSelectionMode(ListSelectionModel. SINGLE_SELECTION); table. SetPreferredScrollableViewportSize(new Dimension(250, 175)); } private class DataModel extends DefaultTableModel { public DataModel(Object data, Object columnNames) { super(data, columnNames); } @Override public void setValueAt(Object aValue, int row, int col) { if (col == CHECK_COL) { for (int r = 0; r getColumnClass(int col) { if (col == CHECK_COL) { return getValueAt(0, CHECK_COL).getClass(); } return super.

GetColumnClass(col); } @Override public boolean isCellEditable(int row, int col) { return col == CHECK_COL; } } private class ControlPanel extends JPanel { JButton button = new JButton("Button"); public ControlPanel() { button. SetEnabled(false); this. Add(new JLabel("Selection:")); this.

Add(button); } } private static void createAndShowUI() { JFrame frame = new JFrame("CheckOne"); frame. Add(new CheckOne()); frame. SetDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE); frame.pack(); frame. SetLocationRelativeTo(null); frame. SetVisible(true); } public static void main(String args) { java.awt.EventQueue.

InvokeLater(new Runnable() { @Override public void run() { createAndShowUI(); } }); } }.

Don't add your own MouseListener. Instead override setValueAt() in the TableModel to see the new value set by the default editor for Boolean.class. Addendum: Here's an sscce.

For expedience, it simply clears all entries in CHECK_COL, sets the new value and conditions the button accordingly. Import java.awt. *; import javax.swing.

*; import javax.swing.table. DefaultTableModel; /** * @see stackoverflow.com/questions/7920068 * @see stackoverflow.com/questions/4526779 */ public class CheckOne extends JPanel { private static final int CHECK_COL = 1; private static final Object DATA = { {"One", Boolean. FALSE}, {"Two", Boolean.

FALSE}, {"Three", Boolean. FALSE}, {"Four", Boolean. FALSE}, {"Five", Boolean.

FALSE}, {"Six", Boolean. FALSE}, {"Seven", Boolean. FALSE}, {"Eight", Boolean.

FALSE}, {"Nine", Boolean. FALSE}, {"Ten", Boolean. FALSE}}; private static final String COLUMNS = {"Number", "CheckBox"}; private DataModel dataModel = new DataModel(DATA, COLUMNS); private JTable table = new JTable(dataModel); private ControlPanel cp = new ControlPanel(); public CheckOne() { super(new BorderLayout()); this.

Add(new JScrollPane(table)); this. Add(cp, BorderLayout. SOUTH); table.

SetSelectionMode(ListSelectionModel. SINGLE_SELECTION); table. SetPreferredScrollableViewportSize(new Dimension(250, 175)); } private class DataModel extends DefaultTableModel { public DataModel(Object data, Object columnNames) { super(data, columnNames); } @Override public void setValueAt(Object aValue, int row, int col) { if (col == CHECK_COL) { for (int r = 0; r getColumnClass(int col) { if (col == CHECK_COL) { return getValueAt(0, CHECK_COL).getClass(); } return super.

GetColumnClass(col); } @Override public boolean isCellEditable(int row, int col) { return col == CHECK_COL; } } private class ControlPanel extends JPanel { JButton button = new JButton("Button"); public ControlPanel() { button. SetEnabled(false); this. Add(new JLabel("Selection:")); this.

Add(button); } } private static void createAndShowUI() { JFrame frame = new JFrame("CheckOne"); frame. Add(new CheckOne()); frame. SetDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE); frame.pack(); frame. SetLocationRelativeTo(null); frame. SetVisible(true); } public static void main(String args) { java.awt.EventQueue.

InvokeLater(new Runnable() { @Override public void run() { createAndShowUI(); } }); } }.

It just so happens that I've overridden setValueAt(). There must be a flaw in my logic though. What I want to do is to have only one checkbox activated at a time or pass setValueAt() to my GenericTableModel.

The reason I want to use a MouseListener is because I want to activate a JButton if one tickbox is checked (one of the Booleans is true). – James Poulson Oct 27 at 18:00 1 I've shown one approach above. – trashgod Oct 27 at 19:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions