Reputation: 1391
In my form I have a JTable with a TableModelListener. The tableChanged method updates the logic on my JButtons. This all works correctly. When a user edits a value in a cell in the jtable - the tableChanged method executes and the buttons are refreshed accordingly.
The problem I am having and it is a show stopper. The JTable displays objects and some attributes of the object. The user selects the objects from the application.
If I there is a object selected and being displayed in the jtable. If that user changes a attribute value in the application and not edit it in the JTable. The jtable is still refreshed and the changed value is displayed. But a TableModelEvent is not taking place and my button logic is never refreshed.
I have looked at TableCellListeners - but that is still looking for a edit in the table. So I don't think that will work here.
How can I tell that something has changed and the Table has been updated without a Event taking place in the jtable itself?
EDIT: Placing some of the jtable code
This is in my base dialog class
selectTable = new JTable(SingletonSelectTable.getInstance());
selectTable.getModel().addTableModelListener(this);
selectTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Font font = comp.getFont();
if (SingletonSelectTable.getInstance().isCellBold(row, column) == true){
comp.setFont(font.deriveFont(Font.BOLD));
}
return comp;
}
});
selectTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
selectTable.setCellSelectionEnabled(false);
selectTable.setRowSelectionAllowed(true);
selectTable.setColumnSelectionAllowed(false);
JScrollPane ScrollPane = new JScrollPane(selectTable);
the tableChanged method
public void tableChanged(TableModelEvent e) {
setRemoveButtonVisibility();
setRemoveAllButtonVisibility();
setCommentButtonVisibility();
setOKButtonEnabledStatus();
}
my singleton class
public class SingletonSelectTable extends AbstractTableModel {
I hope this helps
Upvotes: 0
Views: 5227
Reputation: 36611
How can I tell that something has changed and the Table has been updated without a Event taking place in the jtable itself?
There is never an event in the JTable
. It is the underlying TableModel
that changes, and it is the TableModel
that fires an event.
The JTable
registers a listener to the TableModel
so it knows when it should update the displayed table content.
So if you are saying that the JTable
gets updated (automatically, so without you scrolling/selecting/hovering/...) means that the TableModel
does fire an event, meaning you can also listen for it.
In the scenario an element of your TableModel
is changed in your application, something should fire the TableModelEvent
from the TableModel
. Typically this is done by either letting the TableModel
listen for changes in the objects it contains, or letting the code that changes the object also notify the TableModel
that the underlying data has been changed. Either way, the TableModel
needs to fire an event, so there is no reason why your listener would not receive it, unless it is simply not fired (meaning an incorrect TableModel
).
My best guess, based on your comments, is that you have an incorrect implementation of your TableModel
and that your TableModel
simply does not fire an event, and that the JTable
gets updated 'by accident' (in my experience is a JTable
rather robust for missing events, certainly when no rows are added/removed ... definetely compared to a JTree
)
The getValueAt() method picks up the change and then updates the jtable
This really sounds incorrect. The getValueAt()
method is normally called by the JTable
after it receives an event. Thanks to the event, the JTable
knows it must be updated so it queries the model for the new data. So the getValueAt
method does not pick up the change, but gets called as a result of the change.
I would strongly suggest to take a look at the Swing table tutorial, and certainly the sections about Creating a table model, Listening for data changes and Firing data change events
Upvotes: 3