Initial state of autoCreateRowSorter in Swing JTable - java

I have this JTable on my Swing app with the autoCreateRowSorter enabled. My table only has 3 columns, two strings and one int, it works well for all of them when I click the column headers.
However, I'm looking for way to do it programatically. I wanted to set the "initial state" for this table. With the Windows look and feel, the column header (when sorted) has a little arrow showing the sort order. But at startup that doesn't show, I have to do one initial click.
How can I do that by code?

To programaticallly sort the table you can do something like:
DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();

I think DefaultRowSorter#toggleSortOrder(int column) will do the work

You don't even need the DefaultRowSorter interface.
table.getRowSorter().toggleSortOrder(column);
works as well.

Related

Is a custom TableCellEditor constructor only called once?

I have a JTable. One column in the JTable is assigned an extended TableCellEditor that displays an extended JComboBox.
There is a fixed list of 100 String objects that populates the comboboxes.
The challenge:
Design the JComboBoxes so that any selection is unique relative to other boxes? That is, if "A" is slected from the combobox in the first row, it is automatically removed from the list of each other combobox.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
The problem:
My comboboxes work beautifully. I can select items at will. I even have made some progress in eliminating already used items from the lists. But I can't figure out how to correctly auto-populate.
I am very confused because it appears that my combobox constructor is only called once when the table is created, not once for each row.
Is this the case? Is the constructor for a TableCellEditor only called once ever? If so, how do I modify the behavior of each combobox as it come into existence?
Thanks for your help!
If you would like specific code, please let me know. I don't know if you want me to paste in the whole classes.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
When you add a new row of data to the TableModel you must add the values of all columns in the row. This should not be a function of the editor. The editor allows you to change values in the cell.
I was able to get around my problem by creating an abstract superclass for my combobox that can be accessed from the tablemodel extension when it sets up its data.

How do I avoid the array bound error with JTables?

Well my question is fairly simple, I have put a JTable in a JFrame, but I want that the table to automatically resizes according the size of an ArrayList or to have a side scroll bar
For example if I do something like this:
for (int i=0;i<list.size();i++){
jTable1.getModel().setValueAt(list.getVar(i),i,0);
}
If my ArrayList has 10 elements and my JTable was set up to 4 elements I got the arraybound error. Is there a way to put a scroll bar or something in the JTable so it can shows all the rows that I want (depending on the size of the ArrayList of course)?
Set the number of rows in the table before you run your code:
((DefaultTableModel)(jTable1.getModel())).setRowCount(list.size());
DefaultTableModel documentation
If you want to put the JTable in a scroll box, when you add it to the container you have to say
container.add(new JScrollPane(jTable1));
instead of
container.add(jTable1);

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

Java Swing JTable sort

I have a JTable with column headers. When I click on a column header the data gets sorted. This is the default sort behavior.
The thing is that I need to remember the last column the user clicked to sort. Anyone knows which listener I need to implement in order to catch the column name that user clicked for sorting on the JTable?
The code is already implemented and I'm new to Swing. I just need to add that extra functionality. So any clues will be helpful.
Thanks in advance.
You can add a MouseListener to the JtableHeader. Then you just use the columnAtPoint(...) method to determine when a column is clicked.
Use Swing-X components, there is a JXTable which is more powerful than JTable.

Multiple column sort in JTable

I know that JTable can sort by a single column. But is it possible to allow for multiple column sort or do I need to write the code myself?
You can sort by multiple columns by specifying more than one sort key when calling setSortKeys in the RowSorter you're using.
Look into JXTable. JXTable is an extension of JTable that supports multi-column sorting, as well as other functions that JTable doesn't provide. It's freely available from JDNC / SwingLabs.
You should be able to set the TableRowSorter and the Comparator associated with it.
Example:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
TableRowSorter t = new TableRowSorter(myModel);
t.setComparator(column that the comparator works against, Comparator<?> comparator);
table.setRowSorter(new TableRowSorter(myModel));
ETable from the netbeans collection.
It is part of org-netbeans-swing-outline.jar
A google search aught to turn it up.
The ETable is primarily a foundation for Outline (a TreeTable) but it has multi-column ordering built in as well as many other nice features
"I know that Jtable can sort by a single column. But is it possible to allow for multiple column sort or do i need to write the code myself? "
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.

Categories

Resources