Update mysql when cell value is edited in JTable - java

Well I have a JTable in my app that fetches records from a MySql database adn displays them in a sorted order.
Till now I have been using a separate JTextArea and a JComboBox to allow the users to edit the table, something like
so whenever someone clicks on a row in the table the ID of the record is automatically updated below in a JLabel that lies before the JComboBox.
The question is how can I allow users to simply double click and edit values in the cells which would automatically fire a SQL Update query and update the same in the database. I want to allow this on certain Columns only not every Column.
A response with a code example would be highly appreciated.

how can I allow users to simply double click and edit values in the cells which would automatically fire a SQL Update query and update the same in the database.
You can use a TableModelListener to be notified of changes to the TableModel.
I want to allow this on certain Columns only not every Column.
The row/column is found in the TableModelEvent so you check the column that changed before invoking the SQL update.
One potential problem with this is that a TableModelEvent is generated even if the data isn't changed. That is if you place the cell in edit mode and, for instance, tab to the next cell without change the data and event will still be generated.
To get around this problem you may want to consider using the Table Cell Listener. When using this class the event is only generated if the data has actually been changed.

Related

(Java) Jtable Operations and Exceptions

I just got this Jtable on Netbeans . First I want it to auto-calculate some grades, as you can see it has 3 columns (1st, 2nd and 3rd evaluation). I have to type between 0 and 100, in each row.
In the end, get the total and average. This can be displayed in a Jlabel, or a textfield. But, it has to be displayed real-time (as I'm typing the values).
Also, I can't type data on all the cells, just one for each row. How do I do this?
I know I can change the columns to accept only Integer values, but for everything else, I have no idea how to proceed.
In the end, get the total and average. This can be displayed in a Jlabel, or a textfield. But, it has to be displayed real-time (as I'm typing the values).
Well, you would want to update "after" the user has finished editing a cell and saves the value to the table.
So you can use a TableModelListener. You add the TableModelListener to the TableModel. Then when the data is saved an event will be generated and you can recalculate the values and update the label.
Check out the following for a simple example to get your started: TableModelListener and multiple column validation. Your logic in the listener will change based on your exact requirement.
You can do this by attaching a defaultTableModel to your JTable. After attaching TableModel to your JTable. You can use command JTable.addRow(Object[]) to add rows to table.

How can i save the data in sql from the cell after editing in jTable(autosave)?

i have populated Jtable, but now i want to update sql record if i change a cell's value then it should change in sql databases automatically.
i want to update sql record if i change a cell's value then it should change in sql databases automatically
There is no such thing as autosave (in the base JDK classes). So you will need to write code to listen for changes to the data in the table and then update your database manully using SQL.
For this you can add a TableModelListener to the TableModel of your JTable. Then when the event fires you get the data from the TableModel and update your database.
Note the TableModelListener will generate an event if you start to edit the cell and just tab out of the cell even if you don't change the data. So you may want to consider using the Table Cell Listener which will only generate an event if the data in the cell is actually changed.

JTable multiselect loses selection when table data changes

A JTable displays data from an array of objects. Object data changes in the background and the table is updated. When rows are selected and the table data changes the row selections are lost. It is difficult for the user to select rows for an action when the selections are frequently lost.
Is there a way to stop the deselection?
Here's what I found using the debugger. The row selection is being cleared during the call to fireTableDataChanged, which I was using to apply the updates in the table.
Apparently that method redraws the table and the selection may not be valid (as suggested by the MadProgrammer comment above). I will still be needing that feature when objects (rows) are be added or removed.
So what I must do is find a way to notify the table model to make updates to without clearing the selection. I have chosen for now to use fireTableRowsUpdated when updates are to be applied that do not require restructuring the table and fireTableDataChanged otherwise.

How to auto-refresh a JTable bound to Mysql database after insert using NetBeans

I'm working on a project in NetBeans.
I have a Jtable which i bound to my database and there is a form that I use to insert data.
I want the records in the table to be refreshed with each insert.
How can I do that?
I want the records in the table to be refreshed with each insert.
Then your "insert" logic need to do two things:
insert the data into the database
insert the data into the JTable. This is done by using the addRow(...) method of the DefaultTableModel. You get the data from the form, create a Vector to contain the data for each column then you add the Vector to the model.
I should simply select the table and go to the navigator window. Under "Other Components" there is the list that was created after the binding and contains the table records (you should know its name). Right click on the list > Properties > check "observable".
Please try the following code:
if you want update the table on button click or on event of any component
you have to put the code in the event
the list generated should be Observable checked
code
udetailsList.clear();
udetailsList.addAll(udetailsQuery.getResultList());

Update values in a cell of a JTable

I want to put subject results in a Jtable. I set DefaultTableModel to the table. In my table I have three columns.Student ID,Marks and Grade.Student ID is auto generated in column one.then user should enter marks for for second column.Then I want to automatically put the necessary grading in 3rd column.I load the grading data from the database and find the correct grading for the marks.I try this with both MouseReleaseEvent and KeyReleaseEvent.But it wasn't successful.Can any one suggest me a better way.thank you.
You don't have to listen for the input events, because the table has cell editors already. So you should be capable of editing your table. The only thing you need to do is to listen for the tableChanged event and update the database there.
See: TableModelListener
In case I was confused by your thoughts about the events and you just want to basically set values of a cell programmatically, try this method: setValueAt(Object,int,int)

Categories

Resources