Multiple column sort in JTable - java

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.

Related

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

huge JTable AbstractTableModel remove all rows

java JTable, Say I have a huge JTable (800*50) with AbstractTableModel. Now I want to remove all table rows and put new data rows into that table. Which way is easiest and high-performance way to achieve this ?
Thanks.
The AbstractTableMoeel doesn't support this. If you extend the AbstractTableModel to create a custom model then you need to implement this method yourself.
Or you can use the DefaultTableModel which implements a setRowCount() method. So you can reset the rows to 0. You can then use the insertRow(...) method to add new rows.
However the easier way is to probably just create a new TableModel. Then you can refresh the table by using:
table.setModel( newlyCreatedModel );

Problem in giving input to JTable from keyboard

I want to know how to give input to the cell in a jtable from keyboard.
And when I try to do this when I move on to next cell the previous enter data is removed or erased automatically.
I am using abstracttable model for creating jtable.
iam using abstracttable model for creating jtable....
and when i try to do dis when i move on to next cell the previous enter data is removed or erased automatically....
The AbstractTableModel doesn't implement the setValueAt(...) method. So unless your custom model implements this correctly you will lose the data entered in the editor.
I suggest you keep it simple and use the DefaultTableModel until you better understand how a JTable works. The code would be:
DefaultTableModel model = new DefaultTableModel(...);
JTable table = new JTable( model );
As discussed in How to Use Tables, you can specify a renderer and editor for each column in a JTable, or you can override getColumnClass() to obtain the default for any of the listed data types. In addition, you might compare what you're doing with one of the examples listed there or edit your question to include an sscce.
Sounds like you are not saving the data entered into your model. Secondly, I would suggest extending DefaultTableModel instead of AbstractTableModel unless you have a good reason.

Initial state of autoCreateRowSorter in Swing JTable

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.

Working with data and a JTable?

I have a JTable that I want to use to display some data (a String and a Boolean in each row). The data is maintained by my own class. Is there some way to bind the data model to the JTable, so that when I add to the model, the JTable is dynamically updated and when I remove something from the model, the row is removed from the JTable?
I have previously worked with Flex and Actionscript, and this is very easy to do there with data binding, so I'm just wondering how it's done in Java.
Thanks.
You will need to have your dataset implement the TableModel interface. if you do that then you can apply it to the JTable. If you extend AbstractTableModel you will inherit some event firing methods that your table will handle and will update the view. see this tutorial. Note that the default implementation of JTable will renderer your data for you, and if a Boolean is found, it will show up as a check box.
You'll probably find both the Java JTable tutorial and the JTable API documentation helpful in understanding how JTable works, but otherwise here's a quick rundown.
The premise of a JTable is that it is paired with an object that implements the TableModel interface, which by default is an instance of DefaultTableModel. The table model object is made up of a list of columns, each of which has its own data type (String and Boolean in your case), and a list of rows containing the actual data for the table.
Whenever the JTable is drawn by the swing drawing code, it repeatedly calls the method:
public Object getValueAt(int row, int col)
Thus, when you add data to the table model, it is always rendered as you expect in the next screen refresh (dynamically).
The only thing you really need to worry about, then, is getting the data from your object into the table model and back out again. Other than that, JTable takes care off all the heavy lifting.
While implementing TableModel is easy enough for simple cases, you might want to consider a true binding approach (my favorite is Glazed Lists - watch the 30 second video on how easy this is and you'll be won over). Beans Binding (now Better Beans Binding) also has an implementation of observable lists that might be useful (although I much prefer the Glazed Lists approach)

Categories

Resources