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
Related
I'm currently using a JFrame to hold a JTabbedPane that contains multiple tables. In my class that extends JFrame and implements TableModelListener, I have an onChanged() method that takes a TableModelEvent as an argument. I can successfully obtain data from the event on the table that the event was fired from, but I can't determine which table it was.
From what I understand, this is not the way to do what I intend to do. I believe that I may need to write a custom TableModelListener or JTable and implement the onChanged() method there.
What do I need to do to determine which JTable was changed in the JTabbedPane? I'll need to find the table and the row that was modified.
TableModelListener and TableModelEvent won't provide information about the JTable that the model is associated with, as the model may be shared by multiple tables, in theory.
Getting the row is matter of getting the row from the event, which comes from the firstRow and lastRow properties. Once you can establish which table the model belongs to you, you can determine the view row by using JTable#convertRowIndexToView
To find the JTable you have, at least, two basic solutions
You Could...
Ask each table, stored in each JTabbedPane for their model and compare it with the model that generated the table model event
You Could...
Maintain some kind of look up between the TableModel and the JTable or JTabbedPane, depending on what it's you are ultimately after
This could be achieved by using Map of some kind, keyed to the TableModel
I believe that I may need to write a custom TableModelListener...
Check out the Table Cell Listener.
It is very similar to a TableModelListener, but you do need to specify the JTable when creating the TableCellListener, so you do have access to the table when a value is changed.
I've created a simple JTable with check box like below:
DefaultTableModel model = new DefaultTableModel();
jTable1.setModel(model);
model.addColumn("No:", no1);
model.addColumn("Remark", remark1);
model.addColumn("Color", colors1);
model.addColumn("Done");
TableColumn col1 = jTable1.getColumnModel().getColumn(0);
col1.setPreferredWidth(1);
TableColumn col4 = jTable1.getColumnModel().getColumn(3);
col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
col4.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
col4.setPreferredWidth(50);
jTable1.setShowGrid(true);
jTable1.setGridColor(Color.BLACK);
jTable1.setAutoCreateRowSorter(true);
It's working fine but how to do if I want to add action listener for the check box. For an example, when my check box is checked I need to pop up a confirmation message.
For an example, when my check box is checked I need to pop up a
confirmation message.
You don't need to add an ActionListener to the renderers/editors but you need to listen to table model data changes. Take a look to Listening for Data Changes section of How to Use Tables tutorial:
Add a new TableModelListener to your TableModel
Validate if the updated cell value is a Boolean and its value is true.
Ask the user to confirm the update. If s/he doesn't then set the cell's value back to false.
See TableModelEvent API as well.
Edit
Note in this case as you're working with booleans then there's 2 possible values to do the check. However for input validation in other cases the described procedure won't work simply because the listener will be notified when the change already happened and you won't be able to set the value back just because it won't be there any longer.
Take a look to #kleopatra's answer to this question: JTable Input Verifier. As stated there a better approach is providing a custom CellEditor and do the validation in stopCellEditing() method implementation. Just as a suggestion I'd use a DefaultCellEditor that takes a JCheckBox as parameter and override the aforementioned method.
I made a column editable in Jtable.
I want old values from a cell when I have finished editing a cell
You can get the value by using
table.getModel().getValueAt(row_index, col_index);
where table is the name of the table and it will return an Object
Go through this Getting cell value. It may be useful for you.
You can use a TableCellListener, like they show here. It uses a PropertyChangeEvent to keep track of the old and new values.
You also could create your own implementation of a TableModel and override the setValueAt method to keep track of the changes.
Add a TableModelListener to your TableModel. Whenever an event fires you can updated the contents of your text field with the new value in the cell.
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
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.