JTable onchange event - java

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.

Related

Getting modified values from JTable

I have a JTable with the required values. After editing a cell, if I use table.getvalue(row,column), I get the previous unaltered values. But if I select another cell before clicking the save button I get the modified values. Can anyone help me to remove this problem??
PS: I have not yet added any actionlisteners for the table
The default update mechanism only changes the model when the cell editor loses the focus. Either tabbing out of the cell or clicking in a different cell will cause the vital "focus lost" event which triggers the model change
You could add an ActionListener (see http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html). It will get triggered when you press RETURN. In the handler, call fireEditingStopped() to trigger the "copy to model" code (see http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editor).
or add following code to your table,
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Try TableModel.
Every table object uses a table model object to manage the actual table data. A table model object must implement the TableModel interface. If the programmer does not provide a table model object, JTable automatically creates an instance of DefaultTableModel.
A table model can have a set of listeners that are notified whenever the table data changes. Listeners are instances of TableModelListener.
Have you tried this
int row=table.getSelectedRow();
int column=table.getSelectedColumn();
table.getValue(row,Column)
If Yes you need to use TableModel

How to add a new row to JXTable at runtime?

I am using JXTable to display a list of records.
When I click "Refresh" button, I want the JXTable to be refreshed to display the newly inserted records.
And also when I click "Add new Row" button, a new row must be added to JXTable.
How can I do these? I am unable to find any useful reference or samples. Please guide me.
It works the same as with a regular JTable, which is all explained in the table tutorial. But to answer your questions:
There is no need for a 'Refresh' button, unless the 'Refresh' button updates the TableModel. The moment you update your TableModel you should fire the appropriate events so that the JTable is aware of the changes made to the model. At that moment, the JTable will refresh itself automatically. If your TableModel extends from DefaultTableModel there are already methods available which take care of the events (like e.g. insertRow)
Inserting a row means adding a row to the TableModel + firing the appropriate events. If you use a DefaultTableModel, you can use the available API of the DefaultTableModel

Disable particular rows of a JTable

I would like to "grey out" particular rows of a JTable so that they may not be selected by any means. The other rows should still be selectable. How do I accomplish this?
You can either override JTable.changeSelection() to deselect the offending row whenever it's selected, or provide your table with a custom ListSelectionModel where you override setSelectionInterval(), addSelectionInterval(), etc. to prevent the row from being selected in the first place.
You will want to create a custom TableCellRenderer, one that will display "disabled" information greyed out. Read the Swing Table Tutorial for more on how to create these renderers, especially the section, Concepts: Editors and Renderers.
Create a temporary TableModel which has only the rows that you want to select. After the selection made and when you want to revert, change back to original TableModel

Updating JTable after inline edit

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.

Mutually Exclusive CellEditors in JTable

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.

Categories