How to add a new row to JXTable at runtime? - java

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

Related

Detecting the table of an onChanged() event with multiple JTable in a JTabbedPane

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.

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

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.

JTable onchange event

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.

Categories