JTable Data Refresh Issue - java

I want to refresh the JTable data by clicking a button.
The problem is that the old data in the JTable can't be removed and the new data are just added into the table. I tried below ways to remove the old data but none of them works.
1. table.setModel(new DefaultTableModel());
2. ((DefaultTableModel)table.getModel()).setRowCount(0);
3. ((DefaultTableModel)table.getModel()).fireTableDataChanged();
4. ((DefaultTableModel)table.getModel()).getDataVector().removeAllElements();
5. table.repaint();
6. model = (DefaultTableModel)table.getModel();
while(model.getRowCount() > 0) {
model.removeRow(0);
}

Having a refresh button for a JTable is very suspect. It makes me think you aren't correctly adding data as JTables should refresh everytime data is added or removed.
I would verify a couple of things when using a DefaultTableModel:
Make sure to only add data using addRow
Data should only be inserted using insertRow
Remove data using removeRow
Never modify the internal vectors directly. It won't cause events to fire and you're stuck with a refresh button. I don't know why they even expose it. The JavaDocs should at least specifically warn against this.
If all else fails, fire up a debugger and see what happens.

More of your code might be appropriate here. Hard to tell exactly where you're calling these methods and the order. If you change the model and then call fireTableDataChanged() it should work....assuming you've updated the right TableModel. There is a good Java tutorial for using tables: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

Had the same issue myself, but my solution was different. After checking just about everything, I checked the contents of the table via the console, and found that the contents were indeed being updated. However, the update was not being reflected on the table which was visible on the screen.
In fact, this code:
model = (DefaultTableModel)table.getModel();
while(model.getRowCount() > 0) {
model.removeRow(0);
}
Did not only remove the rows, but also removed the table.
My solution was to remove the table from the form and then re-add it, whenever the table data was changed.
Seemed in my case there was nothing wrong with my coding to generate the table but that the layout manager didn't like overwriting or updating a component the area where I wanted to put it already had something in there.
Something weird going on methinks but at the end of the day this worked for me.

Related

ColumnHideCommand makes the table columns to disappear in NatTable, when all table elements are selected

I have a NatTable component with the following layers:
ViewportLayer
SelectionLayer
RowHideShowLayer
ColumnGroupExpandCollapseLayer
ColumnHideShowLayer
DataLayer
I need to show/hide a specific column, when a checkbox selection changes. In order to that, I use the #doCommand() method provided by the NatTable component:
if(selection) {
nattable.doCommand(new ColumnShowCommand(nattable, COLUMN_INDEX));
} else {
nattable.doCommand(new ColumnHideCommand(nattable, COLUMN_INDEX+1));
}
Everything works just fine, excepting the case when ALL the items in the table are selected, and the ColumnHideCommand is executed. On this specific scenario, the whole table content disappears (Screenshot). If there is no selection in the table, or not all the elements are selected, then everything works just fine.
Please let me know if you have any idea what's going on there or if you experienced this kind of issues before. My experience with NatTables is quite limited, so please let me know if you need any additional information. Thank you!
This is a feature of the SelectionLayer to support multi column hide operations based on the column selection. A ColumnHideCommand get consumed and instead a MultiColumnHideCommand is created and executed based on the fully selected columns. The code in charge is located in SelectionLayer#handleColumnHideCommand(ColumnHideCommand). The method is protected, so if you don't need that feature because you only support column hide/show programmatically and not via UI performed by a user, you can override the method to simply perform a super.doCommand(command); without the check for selections.

Refreshing/Updating contents of a JTable from Database, after user interaction

I have been researching this for days and have tried everything I have come across, but to no avail. I have built an item checkout system in JAVA using a Derby Embedded Database and everything that I have wanted it to do works great. The only thing I can't get to work is having my table update or refresh the information displayed after the user (A) Adds a new entry in the system due to a customer checking out an item. (B) A customer returns an item and is checked back into the system. (C) The table data is changed/fixed due to a previous user error. Case in point: The user checks out an item using the check out dialogue box, then clicks the check in dialogue box but the new entry just made, does not appear. Closing out of the program and going back into it then shows the new information.
I have a method called loadTable() that makes the DB connection all the way through building the table how I want and returns the table.
So far I have tried:
- Calling the loadTable() on any event (Button pressed, window closed, etc).
setFireTableDataChanged() method.
table.setModel(), here I tried inserting the loadTable().getModel or using a temp table.
jtable.tableChanged(new TableModelEvent(jtable.getModel())).
SwingUtilities.updateComponentTreeUI(yourJScrollPane); Ultimately it gets added to JSP.
table.UpdateUI();.
((DefaultTableModel)table.getModel()).fireTableDataChanged();.
repaint().
revalidate().
So far everything I have tried, does not produce the desired results.
I know you all will want to see code, there's a lot so I don't know what I should start with. This is my first time posting so, have mercy on me if i'm going about this the wrong way. Many thanks in advance!

How to disable cell editing of a Jtable displayed from a result set

Hi guys i am having issues trying to disable editing for a table after displaying it. i would have used setEnabled but i still want the table to be clickable because i am displaying and editing the content of it's rows with the help of text fields.
i have searched and got hints that i have to override isCellEditable() or use DefaultTableModel. However, the major problem now is that my table is displayed using rs2xml because i am actually loading contents of a database table into the JTable. here is the segment of my code that displays the table from a result set:
do {
//get the table...
attendanceTable.setModel(DbUtils.resultSetToTableModel(
} while (rs2.next());
rs2 is my result set.
i tried using default table model... i tried something like this:
do {
DefaultTableModel myTable = (DefaultTableModel)attendanceTable.getModel();
myTable.setModel(DbUtils.resultSetToTableModel(rs2));
} while (rs2.next());
but gave me errors because there was no setModel method under defaultTableModel. that was what i understood by using defaultTableModel...
about overriding isCellEditable(), someone that asked a similar question (but without displaying table with rs2xml) mentioned that doing that also made it impossible for his program to edit the table.
please guys i really need help with this...thanks in advance
i have searched and got hints that i have to override isCellEditable()
Good advice.
However, the major problem now is that my table is displayed using rs2xml because i am actually loading contents of a database table into the JTable
Why is that a problem? You can override the isCellEditable(...) method of the JTable.
Also, why does you code have a do...while loop? You only create one TableModel for the JTable. The DBUtils code will do the looping to read all the data from the ResultSet and create the TableModel.
...trying to disable editing for a table after displaying it. i would have used setEnabled but i still want the table to be clickable because i am displaying and editing the content of it's rows with the help of text fields.
Overriding isCellEditable() should not disable clicking on a cell or selecting a cell. It just prevents the cell from being edited.
i have searched and got hints that i have to override isCellEditable() or use DefaultTableModel. However, the major problem now is that my table is displayed using rs2xml because i am actually loading contents of a database table into the JTable.
This should have no bearing on whether or not you can override and disable editing.
here is the segment of my code that displays the table from a result set:...
Snippets don't help much. Post a minimal code example program please, one without need of a database.
i tried using default table model... i tried something like this:
This code makes no sense to me, mainly because you're creating a DefaultTableModel object and then promptly ignoring it -- why? Then you're using a class, DbUtils, that we have no knowledge of and so can't help you with.
about overriding isCellEditable(), someone that asked a similar question (but without displaying table with rs2xml) mentioned that doing that also made it impossible for his program to edit the table.
That's about all the help I can give other than to direct you to the tutorials and ask for more and better information and code.

TableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex) is not updating the cell in JTable

I have a JTable in my code.
And whenever there is an update to any specific column in the row (cell basically), I will update the corresponding icon in that cell.
so I'm basically following these steps.
Step 1: I update the model.
Step2 2: I'm calling
tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
This works fine.
But problem comes when I drag and drop the columns from one position to another in Table header. And whenever there is an update to any specific cell, I follow the same steps as I mentioned before.
Problem: I'm not seeing the Icon painted. But if I bring the focus on top of that row in table it is painting the icon.
Observation: I see the tableRowIndex and tableColumnIndex are correct after dragging the columns.
Just for testing I added this piece of code in the problem scenario.
examTable.repaint(examTable.getCellRect(examTableRowIndex, examTableColumnIndex, true));
This is repainting the cell properly.
But this is not the right solution I guess. I tried to debug the code I didn't find much about the problem
I'm calling tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
That is wrong. You should never invoke the firXXX methods directly. That is the job of the TableModel to invoke the appropriate event when the data is changed.
I will update the corresponding icon in that cell.
All you need to do is invoke model.setValueAt(...) method to change the Icon and the model will notify the table that data has changed so the table can repaint itself.
examTable.repaint(...)
Again you should not need to manually invoke repaint on the table
But problem comes when I drag and drop the columns from one position to another in Table header.
Not sure why you need special code for this if you follow the advice from above. But if for some reason it is still necessary then you need to look at the convertColumnIndex...(...) methods to make sure you are using the proper index for your column.

Changing current selection in JXTable doesn't work?

I have a JXTable where the users need to introduce data, then save it. Only the thing is, the user has to deselect the last edited cell before saving it. If they don't, the data of that cell isn't saved.
The only thing I thought of is to change the current selection automatically just before saving. This is what i tried :
table.getSelectionModel().setSelectionInterval(0, 0);
table.getColumnModel().getSelectionModel().setSelectionInterval(0, 0);
OR
table.getSelectionModel().setLeadSelectionIndex(0);
table.getColumnModel().getSelectionModel().setLeadSelectionIndex(0);
None of both seem to work yet these are the only two methods I found to do this.
Can anyone please tell me how to do this properly or propose an alternative to also let it save the data from that last cell?
I am assuming the user clicks on another component (JButton) when he wishes to save data. If you have a reference to the JXTable when that event happens you could add the following piece of code there:
if (table.isEditing()) {
table.getCellEditor().stopCellEditing();
}
The stopCellEditing() should save the state of the model and allow you to save all the contents, including the currently selected / edited cell.
EDIT: As kleopatra pointed out, the default (and better!) way to handle this is through the client property of JTable component:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
For JXTable this should already be set though, which indicates that the way your UI handling of the save functionality works does not include moving the focus away from the table. So in essence you'd be better off changing the focus when your 'save' event is being fired.

Categories

Resources