I currently have a problem whereby editing the contents of one cell in a JTable alters the content of another; two of the columns are mutually exclusive. They are both checkboxes.
At the moment, if I alter one cell, it isn't until the other is redrawn that it updates. Therefore, both cells in the row can be displayed as being selected at any one time. This can be overcome by calling updateUI(), but it is slow and not a generally great idea.
Has anyone got any tips or suggestions?
It sounds like you should be using a TableModelListener to listen for changes to the TableModel. When a checkbox is changed you will receive an update event and you would then update the other checkbox by using model.setValueAt(...). The model is responsible for notifying the view to repaint the cell.
Related
I have instances of JTable who listen for some remote events, and depending on the type of event I want to lock/unlock a row with model index N. By Locking a row I mean setting the editable flag for all its cells to false. This is handled by my table model.
At the moment, I am calling repaint() because I did not want my table model to fire up the updated event, which is another alternative, but I am having a complex logic which is happening when the actual data in a row are changed, and did not want to trigger that logic.
Is there any other way?
Changes should be made directly to the TableModel. The TableModel will then invoke the appropriate fireXXX() method and the table will automatically repaint the rows affected.
Since it sounds like this "editable flag" is not visible on the table then there is really no reason to repaint the table, so you can override the TableModel to not generate any event when this flag is changed.
In my JTable, I want to edit all relevant cells like in Excel.
I implemented that ENTER stops editing the cell and transfers focus to the next cell. However, when I hit ENTER in the last cell, the focus cycle makes me jump to the fist cell. But I want to continue outside the table and set focus to a JButton.
How can I break this cycle?
JTable is not a spreadsheet, but setCellSelectionEnabled() should allow you to proceed. Use setDefaultButton() as required, for example.
Addendum: In addition to setDefaultButton(), you can evoke any desired action in your custom Action, as shown here.
I am trying to implement a Jtable which includes three check-box tables like this:
Can you tell me how to set a single selection group of checkboxes which only allows 1 selected check-box in a single row at any time?
I know of nothing out-of-the-box for doing this. I´d have a TableModelListener check these columns every time a change is made and call setValueAt on the checkboxes as needed.
I have 2 classes, one to make a frame and the other to handle and implement the interface TableModel. When editing cells inline and updating the values in the class that implements TableModel I then need to refresh the table to show the updated data (as the table needs to auto sort thus when I inline edit a cell the rows may need to be re-ordered). The problem I'm having is after updating the data I can't figure out how to refresh the table, I've tried a hacky way of refreshing it when you click off the cell or press enter but I feel there could be a more elegant solution, any ideas?
The TableModel is responsible for invoking the fireTableCellChanged(...) method when data is changed in the model. Sorting will then happen automatically.
Read the JTable API and follow the link to the Swing tutorial on How to Use Tables for more information about TableModels and sorting.
I suggest you just use the DefaultTableModel so you don't have to worry about this since it implements all the TableModel methods.
Is there any way to detect a cell selection change in a JTable? I've found documentation for detecting a row change using ListSelectionListener but it doesn't seam to work when changing selection on the same row. I'm using JTable to render a simple schedule.
Maybe I should use a different component?
No, the right component for showing tabular data is JTable.
You want to add a listener to the TableModel that's underneath the table. That will fire off events whenever data changes. You get it out of JTable, unsurprisingly enough, by calling getTableModel().
Update
Oh wait, I think I misunderstood you. You're not interested in data changes but column selection changes.
JTable has a method called columnSelectionChanged; its documentation says it's called by TableColumnModelListener, which leads me to believe that what you want to do is getColumnModel() and use the addColumnModelListener() method of that to listen for column selection changes.