How to select the last inserted row in a JTable? - java

I have a JTable with two columns and a table model that uses a class which works with a hash map. The elements of this map are the table rows.
Every time I add a new element I need the element to be selected, regardless of any sorting. Now, I've tried using the following method:
int lastRow = table.convertRowIndexToView(tableModel.getRowCount() - 1);
table.setRowSelectionInterval(lastRow, lastRow);
The problem is, this works only when I'm adding data in a sorted fashion (e.g. "A", "B", "C" and not even then since "D", for example, is placed ahead of A once added). Could someone tell me how to solve this problem?

The problem is most likely in your TableModel, since the code you posted is just fine. You mention you use a HashMap for storing the data of your TableModel. Problem with a HashMap is that it hasn't any ordering. Adding an element to it might alter the order in which you get the elements in the map.
Better to use a data structure which respects the ordering.

better could be to use prepareRendered for hightlighting max row index from XxxTableModel
JTable could be Sorted and Filtered too, then last added row couldn't be visible in JTables view
depends of ListSelectionMode, but by default you can do
table.setRowSelectionInterval(lastRow, lastRow);
table.setColumnSelectionInterval(columnMinIndex, columnMaxIndex);
for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame, JTable in JScrollPane and a new addRow fired from swing.Timer

Related

Is there a way to remove the sort after an SWT Table is sorted?

I have a Table and TableViewer that are set up to take a ViewerComparator:
this.viewer.setComparator( this.comparator );
Now I am looking at my table. I click on a column header. The table sorts on that row. I click on that column header again and it sorts in the other direction. I click on a different column header and the sorting switches to the new column. This works beautifully.
My question is if there is a way to remove the sorting. This would mean getting rid of the sort arrow on top of the column header that I clicked, the gray background on the sorted column's elements would disappear, and the data would go back into its original order. I am open to adding some sort of button somewhere to start this command.
Is this possible?
The Tables sort indicator and the comparator used by the TableViewer are actually independent.
To remove the sort indicator, call table.setSortDirection( SWT.NONE ). To remove the comparator, call viewer.setComparator( null ).
You could let the user switch through UP, DOWN, NONE when the column header is selected or provide an extra UI element to reset the sorting.

When to use convertRowIndexToModel

I have some JDialogs displaying JTables.
When the header columns are clicked a sort occurs on that column.
My question is : how can I know when a column header has been clicked and thus made a sort active.
When the sort is active, I know I should user the .convertRowIndexToModel method.
But how do I detect that a column is sorting in order not to mess the correct index if no sort is active?
Generally speaking, you should ALWAYS uses the convertRowIndexToModel when you take an index value from the view (JTable) and try and look up some value within the model. The JTable does this automatically when you use it's methods, but incase you're not, you need to take care of it yourself.
There's no need to know if the view is sorted or not...
If you "really" want to know when a table is sorted, you could attach a RowSorterListener to the TableRowSorter used by the table.
You could also use the TableRowSorter#getSortKeys to see which columns are included in the sort...
I have used it for my selection table. When the auto order is activated (setAutoCreateRowSorter(true))
the indices of the model table and the visual change, so you have to tell it to look for it within the model with respect to the one you are seeing.
((CustomTable)form.getAvailListView().getModel()).data.get(form.getListView().convertRowIndexToModel(i))

How can I hide a row in my JTable?

I'm using Eclipse Indigo SR1 with JDK 1.7, on Windows 7 Pro.
I've written a desktop app, Swing based.
My app includes a JTable; it shows many records of type T, one row per record.
The table model points to Vector vect, named vect, containing all data to be shown in the JTable.
The app includes a combo, named sele, showing three values: 0, 1, 2.
When sele = 0, every record of vect has to be visible in the JTable.
When sele = 1, the JTable has to show only vect records having odd row index and all records with even row index mustn't be visible. Viceversa, when sele = 2.
So, here's my question: how can I make a row not visible in the JTable ?
I can't use the table model, because it points to vect that contains "all" data.
I tried a table cell renderer, but it seems that you can set the color of a cell, but you can't set it not visible or modify its size.
I've tried another way: if r is the row index, and I want that row to be not visible, I write table.setRowHeight(r,0), but this instruction throws an exception, the height can't be set to zero.
I could solve the problem by splitting the data, dividing vect in two, but I don't like that.
Does anybody have an idea ?
thanx in advance,
William
PS: someone told me to create a filtering TableModel that wraps the existing TableModel. The filtering model would be sensitive to the filtering criteria and fire the appropriate methods (TableDataChanged) when the filter was changed. The getRowCount method would return the filtered count. The getValueAt method would map the filtered row to the actual row in the underlying TableModel.
Mah, perhaps it's a good idea, but frankly I'm not able to understand it...
Use a TableRowSorter - which is:
An implementation of RowSorter that provides sorting and filtering using a TableModel. ..
See How to Use Tables & especially Sorting and Filtering for more info.

sorting a JTable using a data vector

I got stuck with a performance problem while writing my program and I need your help! :)
I'm using a JTable to view test results taken from a vector I made and it has 4 columns in it. When I click on a row the details from a saved txt file of that test are shown in a child window. Also, when I click on the columns header the event sends the vector to a function that sorts it according to the pressed column. Every time a new value needs to be entered the sorting function is called again.
My program works fine with a small number of rows. But, when I enter say, 150 rows, every time I enter a new row the Table flicks (the sort probably takes a lot of time), but I have to keep the vector synchronized with the jable because of the "push to view the result" option.
I would really appreciate some help with this.
thanks
You shouldn't have to do any sorting yourself. JTable supports sorting natively, and has the convertRowIndexToModel and convertRowIndexToView methods to go from the view index to the model index and vice-versa.
See http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting.
Use JTable's internal sorter (DefaultRowSorter). Do not re-create the vector which holds the data - use Vector's add() method to add new records. In many years of Java GUI development I haven't seen a single case where I had to keep records in the TableModel sorted. Make sure getColumnClass() returns a proper type, so the default sorter knows how to sort the column, and that is all.

Column index not changing after dragging columns in Jtable

I am using JTable for displaying the information. After rendering the information if I drag the columns to reorder them, the information is displayed in the same fashion in the session. But when I try to capture the changes by checking the column names by iterating the column names, the sequence is same as the older one. Why is the latest view not available from the API?
As commented by Hovercraft Full Of Eels, the column indices in the view change independently of the column indices in the model. JTable's JavaDoc has this to say about it:
By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.
JTable offers the methods convertColumnIndexToModel() and convertColumnIndexToView() that you can use to translate column numbers from one to the other. You can use these to figure out if (and how) the columns were rearranged.
To be notified of column changes as they happen, use a TableColumnModelListener:
myTable.getColumnModel().addColumnModelListener( new TableColumnModelListener() {
//etc.
} );

Categories