Change TableModel structure - java

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.

Related

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.

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.

Java reorganizable grid

Is there any layout or library in Java to make GUIs that contain a list of data with separate items, and the rows can be reordered by clicking the top of the column. I'm sure that was a terrible job of explaining what I want, so here is a screenshot showing what I mean:
(taken from Spotify's desktop client, but you can find GUIs like this in all kinds of apps.)
How about a JTable?
Your criteria:
A list of data – yes. A table model can be as simple as an Object[][] or as complex as a custom class.
Reordering by clicking header – yes. You can use a TableRowSorter to sort when the column header is clicked.
For information on tables, see the "How to Use Tables" section of The Java Tutorial.
This document also has a section, "Sorting and Filtering", that talks about sorting with the column header. Here's an excerpt:
Table sorting and filtering is managed by a sorter object. The easiest
way to provide a sorter object is to set autoCreateRowSorter bound
property to true:
JTable table = new JTable();
table.setAutoCreateRowSorter(true);
This action defines a row sorter that is an instance of
javax.swing.table.TableRowSorter. This provides a table that does a
simple locale-specific sort when the user clicks on a column header.
This is demonstrated in TableSortDemo.java, as seen in this screen
shot:
More reference:
JTable javadoc
TableRowSorter javadoc

JTable model column

I have a JTable and a Model for that table.
Now I want to change the order of the columns and to hide or show some columns (e.g. like Windows Explorer in "Detail View" via Menu on rightclick).
My first problem here is, the getColumnName function. Do I have to keep track of, which column is at which place and then return the right columnName or is this already part of the model?
Same for the getValueAt function. Can I always return the value for the first column if I get columnIndex = 0, even if the user has dragged this column to the end of the table?
And nearly the same problem for adding/removing columns. If I do that, of course I have to fireTableStructureChanged, but do I also have to adapt e.g. the getColumnName function?
I haven't found a tutorial for that. All tutorials stop at "you can use a model".
I'd really like to see an example of such a dynamic model.
Thanks a lot.
You should use the getColumn(int) method of the model, and for accessing the model, you'll need to convert the row and column view indices with JTable's convertRowIndexToModel(int), convertColumnIndexToModel(int) and the equivalents for converting the model indices to view indices.
You need to understand the difference between the "View" and the "Model". When you reorder the columns in the JTable (view), this does not change the order of the data in the model.
If you want to access the first column that is displayed in the table you use:
table.getValueAt(row, 0);
if you want to access the first column in the model, then you use:
table.getModel().getValueAt(row, 0);
I want to hide or show some columns
See Table Column Manager.

Categories

Resources