JTable with jgoodies sorting trouble - java

I've got a blocking problem with the sorting functionality of a JTable; this made stall the development of the spare time open source project for 4 months now. Hope to be pointed into the right direction here.
Context: I'm working on extending the functionality of the ps3mediaserver to add a media library with pms-mlx. The UI of the media server has been done using swing.
Problem: When clicking on a column header in the JTable, a seemingly random column gets sorted instead of the one having been clicked.
Current implementation: Here's the description of the different components and classes being used for the implementation:
ETable: As alternate row colours aren't supported by default in the JTable, I've switched to the ETable extending the JTable. Source comes from here
FileDisplayTable: This is the class creating the table. In the init() method, the sorting is being enabled with 'table.setAutoCreateRowSorter(true);'
FileDisplayTableCellRenderer: Exists to always align cell content on the left
FileDisplayTableColumnModel: Does some mapping between internal types and column names
FileDisplayTableAdapter: This class implements com.jgoodies.binding.adapter.AbstractTableAdapter to map the objects with the table columns.
Possible solutions:
Preferably, I'd like to keep the current implementation and figure out how to correct the sorting, but I doubt someone can help me out with that!? Additionally their are some bits of code I had to add because of strange behaviours; they're commented in the code
The alternate option would be to change the JTable for another control altogether. I've made some research but didn't find the solution I was hoping for. The constraints are that
it must be embeddable in a swing UI
preferably it should support data bindings
support alternate row colours
row sorting
At some point it will be possible to open an editing dialogue, where the content of the row has to be retrieved, can be edited and when saved the row has to be updated.
Before reworking the entire thing I'd like to be sure the component will be able to handle all I want to do with it.
I'm more used to create GUIs using .NET in Visual Studio. It's quite different and a lot more difficult to do the same with swing. Please show me I'm wrong :)
[edit] If someone is willing to reproduce the problem, either get the source or the binaries, launch the application, navigate to the media library tab. In the Genral section import some videos by adding some video files. Go to the library section, click on apply to refresh the list and try to sort the table.

It may be useful to know that JTable columns can be dragged by the user. As a result, the view (JTable or a subclass) and model (an implementation of TableModel) may have different column numbers. Similarly, a RowSorter may affect the order or number of rows in the view as compared to the model. The related conversion methods are mentioned in How to Use Tables: Sorting and Filtering. In particular: "When using a sorter, always remember to translate cell coordinates."
Addendum: As an alternative, consider org.netbeans.swing.etable.ETable or it's subclass org.netbeans.swing.outline.Outline, depicted here.

Related

Vaadin Grid vs Table

What is the difference between the Grid and Table components in Vaadin 7?
Which should I use, and when?
Summary
Grid → New & AmazingTable → Venerable & Reliable
Table is a very good data-grid display widget built into the earliest versions of Vaadin.
Grid is grand rewrite from scratch, designed to supplant Table. The Vaadin team is leveraging their wisdom gained from experience, “if we knew then what we know now”, to make the very best data-grid possible given today’s Web technology. Grid is such a big deal that it gets its own vanity page. See this company blog post for a quick overview.
So, generally speaking, I suggest you focus on Grid. Try it out, learn it first, and see if it meets your needs. If you run into bugs or problems, or you have need features lacking in Grid, then fallback to Table. You can mix-and-match both in a project, with the caveat that the different appearance and behavior may confuse your users.
Think of Grid as the precocious adolescent full of promise and eager to make the leap into adulthood, and Table as the mature grownup working hard in its prime years of middle-age while dreaming of a well-earned future retirement sailing into the sunset.
Details
If using Vaadin 6, on a continuing project or you need to support very old browsers, then Table is your only choice. Grid requires Vaadin 7 or later.
Here are some major Table features currently lacking in Grid.
Drag-and-drop features (to be added later).
Resize column by user dragging edge of column header.
Both share many features. They practice lazy-loading to the browser, automatically loading data only as needed from the server-side so as to not overload the web browser. Both allow the user to drag columns to re-order. Both let the user show/hide columns.
Row Selection
Both allow selecting single rows or multiple rows.
Grid also has an automatic feature where it adds a column of checkboxes. The user can select multiple rows by clicking those checkboxes rather than using a mouse or mouse+keyboard. Many, if not most, users are clumsy with mouse-driven multiple row selection. See this screenshot, and notice the very first column.
The programming support for selection is different. Grid does not extend AbstractSelect, instead defines its own selection API. Call addSelectionListener() and define a SelectionListener. See The Book Of Vaadin.
Headers & Footers
Both have headers and footers, but Grid has more options. Grid can place widgets instead of text. Grid can have multiple rows of headers. Grid can join header cells, like spanning in an HTML table.
In-Place Editing
Both provide in-place editing of data, but in different ways. Table allows editing of data in the cell. Grid took a different approach, for editing the entire row by displaying a mini-window, a little data-entry form. This form includes a pair of confirmation & cancellation buttons. This form is much more flexible than Table’s cell-editing.
Filtering
Grid offers user-controlled filtering, where a row of enterable cells appears below the headers. As users type a filter is applied to show only matching rows. See this screenshot. With Table, you need to create some kind of user-interface and apply the filtering.
Backed By Data Container
UPDATE: Vaadin 8 brings a new version of Grid that leverages a newly improved and greatly simplified data model. This is a major reason to use Grid instead of Table. Note that both the original Grid as well as Table are still available in Vaadin 8 via the Vaadin 7 compatibility layer.
The following old info left intact…
Both Table and Grid are a presentation-only widget, backed by a separate data object implementing the Container interface according to the Vaadin Data Model.
The Table class also acts as a Container which always confused me. I’m glad to see Grid maintain a more clear distinct separation.
Like Table, Grid does offer some convenience methods for quick-and-dirty situations where you want to throw some data at the Grid itself without formally producing a Container. But Grid’s convenience methods use row and column terms in contrast to the Container’s item and property terms. These terms make it more clear that your are talking to the Grid but the Grid is acting on its default attached IndexedContainer instance on your behalf.
Cell Content
UPDATE: In Vaadin 8.1, Grid gains the ability to display a Component in a cell. See a live demo of the Component Renderer.
Cell content handling is different. Grid cannot directly display column icons, nor can it place components (widgets) in a cell. Instead used the new Renderer features.
Doc & Demo
Both have a chapter in The Book Of Vaadin, one for Table and one for Grid.
Both have a live demos. One for Table (and TreeTable). And a couple for Grid, one full-window and one with various aspects.
See this brochure page for Grid, including an embedded live demo, with a link to further demos.
Miscellaneous Differences
Grid has a built-in widget for displaying a number as a small thermometer widget. See this screenshot, in the last column.
For more specific differences, see section 5.24.1 Overview – Differences To Table in The Book Of Vaadin.
Esoterica… Grid is the first component in Vaadin Components, a high quality set of Web Components built on Google Polymer that is ready to be used with any framework that supports Web Components. While the Vaadin team has promised to support Table for years in the future, don’t expect it to receive such special attention.
Vaadin 8
In Vaadin 8.0 and 8.1, Grid is getting even better. Major enhancements include:
Works with the simpler sleeker data model new in Vaadin 8
Pass a collection of entities for display
Easily define columns with type-safe lambda syntaxgrid.addColumn( Person::getFirstName ).setCaption( "First Name" );
Easier lazy-loading of data now that Container is gone:grid.setDataProvider( ( sortorder , offset , limit) -> service.findAll( offset , limit) , () -> service.count() );
The ability to display Vaadin components rather than just renderers
Drag-and-drop via the drag-and-drop support defined by HTML5.
Even more speed
The Table component is still available via the Compatibility layer in Vaadin 8 for continuing the use of Vaadin 7 classes.
Future
The Vaadin team has great plans for Grid, so much of what you read on StackOverflow page will change. The team will be eagerly adding features, enhancements, and bug fixes in the coming months and years. Many enhancements have already been made to Grid in its short history, so beware when reading older documents about limitations or lacking features – that may not be so anymore.
Grid is a new more powerful component which is supposed to be the successor of Table (see The Table is dead, long live the Grid). So there should not be any need to favor Table over Grid.
Here is a first in the series of articles by Vaadin aboout migrating from Table to Grid:
https://vaadin.com/blog/-/blogs/mission-rip-table-migrate-to-grid-basic
Actually it's possible to implement everything you want with both of them. But my experience is, that the Grid is more comfortable to use.
The Table is easy to understand and easy to use for simple tables (as you might guess). So if you just want to show a few lines of data visualized nicely - use the Table. It is stable and works well on that.
The Grid looks like a Table but it has some features towards the Table. If you have a whole lot of data to render the Grid might handle it better. Also there is a good practice for "inline editing" your data. There is a way to customise the headers of a grid extensively. If you want to do a lot of customisation and interaction inside a table-like component - use the Grid.
See the features here:
https://vaadin.com/grid
http://demo.vaadin.com/sampler/#ui/grids-and-trees/grid
There is a difference how you can select rows/cells in these two components.
For example the EventListeners for selection are used slightly different concerning the value they return.
Also there's a difference in how you add columns and rows to them, but thats just an implementation thing, so it shouldn't really matter.
In filter table ,if we clicking on the table header,first row will be defaultly highlighted by a method called setSelectable(true); but in grid table there is no such type of action,
I want to apply the same action in grid table. Is there any possibilty for having that nature? Is there any method or code available?
Below is the code I used in my grid table:
private void buildPagedGrid(Class<T> clazz) {
setWidth("100%");
setSelectionMode(SelectionMode.SINGLE);
setImmediate(true);
setSizeFull();
setContainerDataSource(dataSource);
setFooterVisible(true);
}

Combine javafx 2 ListView and GridPane features

My target is to display an abbreviation list with two entries per line: the abbreviation and the corresponding long version. For a nice layout I used a GridPane because of the vertical alignment over all entries - it's nice to read.
But I also want to scroll to the clicked abbreviation and set the focus on it like in a ListView version of it.
For example the # on page links in good old HTML. Is there another javafx layout element I miss to achieve this?
I don't believe there is a provided control that will work for the specific scenario you are describing. However, I think one of these options might work for you...
Use the TableView control and add two columns for the information you want to show (one for the abbreviation and another for the long version). TableViews also have the scrollTo and setFocus functionality you're looking for. Here is a good resource to get you started with the Tableview control. You can also style the Tableview with CSS to look less like a table and more like a list if thats what your intention is.
The second option is to set a custom cell factory on your ListView that builds custom cells using HBoxes, VBoxes, Labels, etc. to achieve your desired look. You would also want to use the cell factory to populate each ListView cell with an object that contains both the abbreviated text and long version text. A couple good resources, 1, 2
Although I think both option will work fine, I would suggest option 1 since in option 2 you are sort of building a table type structure anyway. I hope this is helpful!

Create a properties frame in Java

I am trying to make a properties frame just like the one in netBeans (or Visual Studio). My problem is that I don't know exactly how to design it. First I thought I'll make it with JTable (2 columns, multiple rows) but then I realised that on the second column I will have different types of values (booleans, String, color choosers, etc.), but I think that JTable allows only 1 type of data to be placed in a column.
I would like someone to tell me "JTable allows multiple data types on the same column" and show me how to do it, or tell me a different approach to the problem.
You can perfectly tell a JTable to have a column that contains Object, this way you will be able to put whatever ou want in.
BUT.
You'll then have to implement a very good TableCellRenderer/TableCellEditor pair in order to display whatever the cell contains.
Another option would be to use a Grid or GridBag layout inside of a JScrollPane, then dynamically populate the cells of the grid with different editors depending on the data type of the property.
If you can use external libraries, the JGoodies FormLayout is really suited to create such dialogs. Just take a look at the screenshots in their demo.
There is also a rather good PDF available containing with some examples and explanations.

Creating JTable inside node of JTree

I know that this question is being asked before but I did not get proper solution.
I read about outline but that was just opposite of what I want i.e. JTable inside JTree
I want to display data in a JTable but it is stored as different groups,so these groups
are node of JTree when I expend one group(node) the JTable of that group is shown.
I went through this article in oracle :Creating TreeTables in Swing:part 1
This was pretty old and not working on my version of java(I don't know why !),so I went
through the next part of TreeTable :Creating TreeTables in Swing: Part 2
The whole code was comparatively new, as it was using "javax.swing" in place of
"com.sun.java.swing" but the problem I faced here was one file named as
"FileSystemModel2.java" was missing it is modified from previous version so I can't work
with old one.There are some method added to new "FileSystemModel.java".How do I inform
them about this missing file or if you have different approach please let me know.
What I want to achieve is shown in picture below :
This image was from "LastPass" and I want to make my own password manager as my project
So please help me with this I am so close but yet I can't move further.
The SwingX project contains a ready-made TreeTable
http://swingx.java.net/
The downloads are a bit hard to find, so here is the link:
http://java.net/downloads/swingx/releases/1.6.2/
A short article about using that component can be found here:
http://sandarenu.blogspot.com/2008/02/treetable-in-java-using-swingx.html

GUI: Changing panels based on value of combo box

I have a question about GUI design, specifically with Java Swing and creating clean separation between presentation and model.
It's a bit difficult to describe, but essentially we have lots of reference data in our system (i.e. that would correspond to lookup tables in the DB). We want people to be able to edit them all from one screen.
So, in an ideal world what we'd like is a combo box in the top-left corner with a list of 'types' of reference data (so each corresponding to one table in the DB).
Then, when selected, a list of the data is populated below, also a filter (or search box). When one of these items is selected, the panel to the right is activated which will allow the actual data to be edited.
Now, here's the problem: each type of data we need to edit is different, so it has different fields etc. We could go with a generic solution but I'm not really a fan of them - there are lots of different validation rules for each etc, even for different clients, and it would be a nightmare to manage.
We're using the Presentation Model pattern to achieve some degree of separation between GUI code and the model but I can't think of a clean way of doing this which doesn't somehow blur the line of responsibilities a bit.
What are the ways you have solved problems like this?
[Note: apologies for the long question, hope it's understandable, I can re-phrase if necessary]
You could use the Factory Pattern to create a UI widget for the element that you are selecting. You could also use it to create a validation rule object depending on the type. This would give you some of the flexibility you desire.
So you can have something like:
IWidget widget = UIFactory.createFor(myObject.getType())
That can be invoked on the selection event to create the right widget to edit the selected element.
The IWidget could have methods such as:
validateData()
refreshData()
setDataElement(IDataElement element)
That would allow you to treat all UI widgets generically, but still have a special UI widget for each element type. I am assuming that the elements that you are selecting from the table all implement some IDataElement interface that contains the getType() method.
I used this solution tied together with the Eclipse Extension mechanism to plug-in new UI elements into my "base" solution, to have an extensible core and a high level of reuse. You could achieve something similar by injecting types and widgets into your factory, either manually or with Spring.
If you dont want to go down the generic path, you could have your model hold a mapping of combobox item -> panel name for use with a CardLayout. You could then create custom panels for the editing each of the reference data types. When the combo box selection is changed, you can save the current state in your model, request the panel name of the current selection, prepare your next panel for display and then have your CardLayout show it.

Categories