Getting the cell or row count of JTable when using JComboBox
This is the code I'm working with:
//create the table and set the table model
EventHistoryTableModel tableModel =
new EventHistoryTableModel(crs, eventName);
eventTable = new JTable(tableModel, true);
//create and add a JComboBox to the type column
TableColumn typeColumn = eventTable.getColumnModel().getColumn(2);
typeComboBox.addItem("off");
typeComboBox.addItem("on");
typeColumn.setCellEditor(new DefaultCellEditor(typeComboBox));
typeComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventHistoryTableModel model =
((EventHistoryTableModel)eventTable.getModel());
model.setValueAt(typeComboBox.getSelectedItem(),
eventTable.getSelectedRow(), 2);
}
});
As you can see I know that the combo box is in column 2, but I don't
know what row we are going to be edited. The above code works fine IF
the row in the table is already selected. However, if the row is not
selected and the user clicks the cell with the JComboBox, then clearly
it will throw an exception.
I was wondering if anyone knows of the best way to deal with this so
that I know what row is being edited?
Thanks
Lionel.