0

I have a jtable with 4 columns where the last column has a combo box,

sample code

String[] colNames = {"Name","Email","Department","Status"};
TableModel model = new DefaultTableModel(colNames,200);
table.setModel(model);

TableColumn statusCol = table.getColumnModel().getColumn(3);
comboBox = new JComboBox();
comboBox.addItem("Approver");
comboBox.addItem("Senior Manager");

statusCol.setCellEditor(new DefaultCellEditor(comboBox));

Here the comboBox has 2 options approver and senior manager, I am looking for a way to add condition as, only one senior manager in the table?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
vijay
  • 1,129
  • 7
  • 22
  • 34
  • please whats `I am looking for a way to add condition as, only one senior manager in the table?` hmmm see my [question about prepareEditor](http://stackoverflow.com/questions/7045851/jtable-how-prepareeditor-works) – mKorbel Mar 18 '13 at 12:34
  • 1
    Please show a SSCCE that demonstrates the problem – kleopatra Mar 18 '13 at 13:55

2 Answers2

1

You should handle this within your domain (e.g. add a constraint wherever you maintain your data loaded into the tableModel) and limit the amount of senior managers there. This is business logic, don't implement it in your GUI.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 1
    Thanks for ur answer, i did this with bussiness logic but the condition failed when user add new employee and change status at a time, it doesnt work. @Jeroen: I know what you mean, but i thought its a good idea to implement it in a GUI. I would like to try with GUI – vijay Mar 18 '13 at 12:35
1

Assuming your TableModel starts out in a consistent state, e.g. no more than one senior manager, the goal is to maintain that invariant when a cell in column three is edited. The hard part is ensuring that listeners to your TableModel see any related changes. One approach is to use an object manager, as shown in this example by @Guillaume Polet. The manager fires a PropertyChangeEvent to notify the listening TableModel when a managed object changes state. The TableModel, in turn, notifies any registered TableModelListener, such as the JTable itself, via fireTableRowsUpdated().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045