I have a JTable that I am populating from a database. When a user clicks on a cell and edits it I would like to update the database but I don't know which database row index that information came from.
I know that I could add the database index as a column in my JTable and get the index from there when a cell is updated but is there a way that I can do the same thing without showing the index column to the user? Thanks
Related
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.
As question in title, I need to get selected value of a cell from my jtable, the function "table.getSelectedValueAt(row,column)" is not useful in my state because I don't know which row and column will user select?
I have a very odd issue with JTable.
I put data in JTable from DB. When user double clicks on any cell, it copies the cell contents of the first column of the row where user double clicked. So far, it works perfect.
The problem arises when the user sorts the JTable by clicking on the header. when the table has been sorted and now when the user double clicks on any row, it doesn't what is currently stored on that row's first column. It copies what was originally stored on the first column of that row when the JTable was not sorted.
Any ideas?
Problem:
The problem here is that you are getting the initial rows indexes in the JTable TableModel, and not the relevants row indexes shown in the table view.
Solution:
You can map the shown indexes of a sorted jTable with their relevants ones in the dataModel using convertRowIndexToModel(index) method which takes in input the row index in the view and returns the index of the corresponding row in the model.
Let's say that you have the following jTable:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));
And then loop throught model indexes and use this method with each index to get its corresponding one in the TableModel:
table.getRowSorter().convertRowIndexToModel(0); // index 0 here
As suggested in How to Use Tables: Sorting and Filtering, "When using a sorter, always remember to translate cell coordinates." Most likely, you have neglected this in your event handler.
Try Sorting your JTable TableModel data too. Jtable -> TableModel is the one which hold the actual data. JTable is just a view.
I want to create a frame in java with variable number of checkboxes which will depend on the number of rows present in my database.
Problems that I am facing are:-
1. How to count the number of rows in database?
2. How and where to store the data present in columns of database?
3. How to create checkboxes with that data present in column of database?
Can anyone help me.??
Looking forward for solution.
Thanks in advance :)
How to count the number of rows in table?
SELECT COUNT(*) FROM table_name;
How and where to store the data present in columns of database?
I would suggest you to use JTable and look the tutorial How to use Table
How to create checkboxes with that data present in column of
database?
Read about Renderer and Editor section to know more about registered cell renderers in JTable.
I need to develop an jsp application where I need to bring data from database table and display it in table in frontend dynamically. I need to make this table editable. The user can edit any number of rows which all should be updated in the database table automatically on clicking Save Changes button.
I am able to bring data from database table and display it. I am displaying the values obtained from table in each cell of input type text of form. We am i clicking edit button i am able to update only last edited row in the table.
Please tell how to update all the edited rows at a time.
Provide two links for view and edit
of table values
In view keep the values simply in td element of table.
In edit , use input elements of type text inside td element.
If you want to do everything in same page, Ajax can be used for both view and edit.