Is there a simple way to sort rows in a JTable with Java 1.5 (setAutoCreateRowSorter and TableRowSorter appear to be Java 1.6 features)?
Sorting in Java 1.5 is only possible via libraries.
E.g. use the JXTable mentioned from Kaarel or VLTable from here.
Another good library is glazedlists
which is also used in the Spring Rich Client project.
There are even ways to use Glazed Lists with JXTable
Use the JXTable from the SwingX project, see e.g.
SwingLabs: How to Use the SwingX JXTable: Sorting Rows
JXTable requires a big package, and is difficult to get the right version for. (no higher than version 1.0 for Java 1.5).
Try instead TableSorter.java. Get it at:
http://ouroborus.org/java/2.1/TableSorter.java
And insert it in your project.
Now you wrap your tableModel in an instance of TableSorter, and instert that into the JTable. TableSorter acts as a "go-between" the JTable-instance and your tableModel.
Use it something like this (code untested):
JTable myTable = new JTable();
TableSorter mySorter = new TableSorter(myTableModel, myTable.getTableHeader());
myTable.setTableModel(mySorter);
You can set sprting programmatically like this:
mySorter.setSortingStatus(0,TableSorter.ASCENDING);
Try tweeking MouseHandler.mouseClicked() to get it to skip the NOT_SORTED option in the clicking order, and mess with renderers for e better column header and placing and visibility of triangle.
Related
In Vaadin 8, I have a Grid with columns and rows populated from jsondata using setDataProvider. Now, I have an edit button, upon clicking it user should be able to edit (a few) columns in the Grid.
In Vaadin 8, there are number of ways to incorporate editing in Grid.
There is a builtin feature of row editor, which can be used also in unbuffered mode. This is not equivalent of having the whole column editable, but mimics it pretty well, while you can use Binder for setting fields, validation, etc. There exists also an add-on that helps keyboard navigation with un-buffered editor.
Alternatively you can use the Grid Renderers Collection add-on, that provides set of editable renderers with edit events etc.. This is literaly what you are looking for, column oriented editing. This is useful approach when only few columns are editable.
https://vaadin.com/directory/component/grid-renderers-collection-for-vaadin7
If you use renderers extensively, there will be more widgets for the browser to render, and that will UI possibly slower than using e.g. row editor in un-buffered mode. This depends heavily on your application and use case. I recommend to study multiple approaches and select the one that fits you the best.
Third alternative is to use ComponentColumn feature, see chapter Component Renderer in Vaadin documentation. This is somewhat easier than implementing custom renderers, but adds some further overhead.
For my company, I'm working on an Eclipse plugin which should do the following: show a table with sentences from the active editor that are incorrect in some way, accompanied by proper feedback. So far, so good.
However, sometimes the sentences/feedback are incorrect. I want a third column with checkboxes so that, when clicked, the related sentence/feedback combination is put in a different tab. Because I'm only programming in Java for 2 weeks now, I'm stuck: the plugin (written by my predecessor) is a class 'public class feedbackView extends ViewPart' using TabFolder and Table. But when I try to include checkboxes, all examples on the Internet use JTable. And I cannot get JTable to work in the current script and in the way I want (in Eclipse itself, instead of in a separate frame).
Ideally, I want Example of feedbackView with TabFolder. If in the 'Hide?' column the checkbox is checked, the sentence/feedback row should be transferred to the '||Hidden||' tab.
After this long introduction (hopefully clear), my questions:
Can I use Tabfolder/Table for this? Or
Should I use the JTable class (and how can I get this to work)?
Thanks in advance!
JTable is from Java Swing, with is the Java GUI component. Where Eclipse Table is the an Eclipse GUI component. There are two different world, for building GUI's in Java.
Warning: You can not mix these two different Java GUI libraries!
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
For a specific screen, I'm looking for a JList that I could filter (the same way you can filter a JTable using a RowFilter)
Is there a good implementation of this kind of component anywhere here in the wild (and do you have an experience with it) or do I have to code it myself ?
(it is not that long to do, but if there is any valid implementation, I would be happy to use it)
Yes, I'm sure that the SwingX components provide this. Check out JXList.
You can get SwingX from here.
You could consider using a single column JTable instead of a JList. If you follow this approach you'll get filtering and sorting for free (providing you're using JDK 6).
IMHO glazedlists should do filtering as well as sorting for JTable, JList and JComboBox.
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.