Reputation: 139
I don't know how to specify my problem because I can't think about exact words for it.
But I'll try to be as specific as possible.
What I am doing:
I am making a database application which uses a JTable to get the user-data and feed it to the database.
My problem: In my application whenever a user clicks on a JButton the code fetches the entries from the JTable and submits them to the database(i.e. I have added the database operations in the actionlistener of the JButton).
The problem is that whenever a user types an entry into the JTable's cell and does not hit "enter" or select another "row", the code won't fetch that entry.
I'll try to be specific:
What I am trying to say is that suppose the user is typing on the 1st row's 2nd column, and he hits the JButton right away without pressing enter on the JTable's cell or selecting a different row, then
model.getValueAt(0,1).toString ///is returning null because intially nothing was there.
However, if enter is pressed or a different row is selected it yields the entered value.
So, how can I obtain all the entered values into the JTable's cells while pressing the JButton.
I didn't knew the exact words to describe this problem or question, so I couldn't frame the question properly.
Thank You!
Upvotes: 0
Views: 82
Reputation: 109557
Collect the model changes and on button press make your SQL.
TableModel model = table.getModel();
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
... e.firstRow, e.lastRow
}
});
Or in Java 8
model.addTableModelListener((e) -> {
... e.firstRow, e.lastRow
});
Upvotes: 2
Reputation: 223
You can try something simple like adding fireTableDataChanged() which... "Notifies all listeners that all cell values in the table's rows may have changed" You can call this in your JButton Action listener or perhaps implement a Lose Focus Listeneron your table.
More info can be found here http://docs.oracle.com/javase/7/docs/api/javax/swing/table/AbstractTableModel.html
Or here https://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html
Lastly, a more indepth answer that might help you can be found here How to refresh data in JTable I am using TableModel
Upvotes: 1