JTable multiselect loses selection when table data changes - java

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.

Related

Update mysql when cell value is edited in JTable

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.

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)

Related to checkbox in jtable

I am using a table to display data.
I am providing checkbox to each row of a table to perform some operations based on selection. When I did like that, I am able to check multiple rows.
But my requirement is, at any point of time I should check only one checkbox. To be precise, I need the behavior of Buttongroup to all checkboxes in table.
How can I do this?
If you really want to use checkboxes, I assume your TableModel holds a boolean for those checkboxes. It should be trivial to move the logic for the single selection to the TableModel.
If you do not need the checkboxes but just want to operate on the selected rows (see JTable#getSelectedRows), you can adjust the ListSelectionModel which is present on the JTable to only allow for single selection (see ListSelectionModel#SINGLE_SELECTION)
CheckOne is a complete example that simply clears all check boxes in a specific column and sets the new value. This related example uses JRadioButton.

Change TableModel structure

so the scenario is that I've got a JTable with a number of JComboBox's as cells. On the selection of an element of a JComboBox, there needs to be a change in the structure of the Table Model. I've also got an 'output table' below which listens to the selection of the JComboBox's and re-validates accordingly, because of this, I need to keep the model of the query table the same so that it can reuse the listener. How can I change the structure of the Table Model?
DefaultTableModel QueryTableModel = new DefaultTableModel(dropDownUserSelection, resultsListHeadings );
queryTable.setModel(QueryTableModel);
JComboBox box = new JComboBox(boxModel);
queryTable.getColumnModel().getColumn(i).setCellEditor(new DefaultCellEditor(box));
I apologize if I am asking a question which has already been asked elsewhere, but I've had a poke around and couldn't find what I was looking for.
Thanks
The TableModel has the responsibility for notifying the parent table (or anybody listening) of changes to the model.
The general events available are data changed, cell updated, row inserted/removed and structure changed.
The "structure changed" tells the parent table that the structure of the table model (the number of columns and/or column names and/or types has changed) and it should completely update itself.
There are a number of ways you could achieve this. You could have the existing table model change it self accordingly and fire a "structure changed" event or you could construct a new table model and apply it to the JTable, depending on your needs.

JTable erratic behavior

I have some old Java application that uses JTable to show some data and allow input as well.
JTable has 2 columns. Next to JTable, there is button that adds new row into JTable.
My problem is behavior of Tab and Enter keyboard keys and mouse clicks when you navigate JTable.
I will try to explain:
Let say there are 5 rows in JTable:
if I click with mouse on one row, sometime whole row gets highlighted, and sometime cell gets into edit mode (in addition to whole row being higlighted)
Sometime, selected row gets highlighted, but cell above or below highlighted row gets into edit mode
If I use Tab to skip from one field to another, editable field is always above highlighted row.
There are other issues as well.
Any ideas what might be wrong with it?
That sounds a bit odd. Are you fully invalidating your table and notifying listeners etc that the number of rows have changed?
In your table model, loop over all of your TableModelListeners and fire a TableModelEvent 'insert' event:
TableModelEvent event = new TableModelEvent(
modelInstance,
positionOfNewRow,
positionOfNewRow,
TableModelEvent.ALL_COLUMNS,
TableModelEvent.INSERT);
for (TableModelListener l: listeners) {
l.tableChanged(event);
}
If you can't work out the problem, it is possible to write your own behaviour for the table.
These are some useful methods:
editCellAt(int row, int column);
rowAtPoint(Point point);
columnAtPoint(Point point);
You can add your own listeners to the table to intercept events and edit cells in any way you like.
Turn off the Table cell editing. And see how it behaves. Typically this can be handled by changing the TableModel.isCellEditable() method to simply return false.
Turn it back on. Is your instance of JTable subclassed? Is it overriding editCell()? If so that's the method that will trigger an edit based on the event occurring or not. That method is turning on editing inappropriately. The implementation of that method is bad if it's overridden.
If your table isn't subclassed look for calls to editCell(). Those are probably calling it inappropriately so you can look for those calls and start setting breakpoints or log statements.

Categories

Resources