I have a JTable, X Rows by 4 Columns. The first column is text, but the other 3 are CheckBoxes. I am attempting to make it so when 1 CheckBox is selected, the other 2 in its row deselect. However I cant seem to get that to work. To specify, the Checkboxes just don't deselect.
Here is my current attempt:
table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer() { public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col) { boolean marked = (Boolean) value; JCheckBox rendererComponent = new JCheckBox(modelSel.get(row).B717+""); if(marked) { rendererComponent.setSelected(true); } return rendererComponent; } }); DefaultCellEditor cellEditor1 = new DefaultCellEditor(new JCheckBox()); cellEditor1.setClickCountToStart(1); cellEditor1.addCellEditorListener(new CellEditorListener(){ public void editingCanceled(ChangeEvent e) { } public void editingStopped(ChangeEvent e) { if(((JCheckBox)((DefaultCellEditor)e.getSource()).getComponent()).isSelected()) { data[table.getSelectedRow()][2] = false; data[table.getSelectedRow()][3] = false; } else { data[table.getSelectedRow()][2] = true; data[table.getSelectedRow()][3] = false; } data[table.getSelectedRow()][1] = ((JCheckBox)((DefaultCellEditor)e.getSource()).getComponent()).isSelected(); } }); table.getColumnModel().getColumn(1).setCellEditor(cellEditor1);
In this attempt, I tried to make the values change in the CellEditorListener, but that doesnt seem to do it.
On a parallel, I have an issue when I close my JDialog when it reference those cells. It should try to find the column in each row that has a check, and set the value that corresponds to that column.
For a 5 Entry Test, I make the following Selections at Runtime:
Entry 1: Column 2
Entry 2: Column 3
Entry 3: Column 1
Entry 4: Column 2
Entry 5: Column 3
My printout is:
Entry 1 chose B737
Entry 2 chose B717
Entry 3 chose B717
Entry 4 chose B737
Entry 5 chose Blended
The printout should be:
Entry 1 chose B737
Entry 2 chose Blended
Entry 3 chose B717
Entry 4 chose B737
Entry 5 chose Blended
I would think the problem is most likely a result of my selections not working correctly, but I'm not totally sure of that yet, and I wont be until I get the selections working right.
tempDialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { for(int i=0;i<data.length;i++) { String name = data[i][0].toString(); int index = -1; for(int x=0;x<marketList.size();x++) { if(marketList.get(x).Name.equals(name)) { index = x; break; } } int number = 0; Object[] it = data[i]; if((Boolean)(data[i][1])) { marketList.get(index).Cost = modelSel.get(i).B717; System.out.println(name+" chose B717"); } else if((Boolean)(data[i][2])) { marketList.get(index).Cost = modelSel.get(i).B737; System.out.println(name+" chose B737"); } else if((Boolean)(data[i][3])) { marketList.get(index).Cost = modelSel.get(i).Blended; System.out.println(name+" chose Blended"); } else { System.out.println("Error with "+name); } } } });