4

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.

Philippe
  • 1,949
  • 4
  • 31
  • 57
  • 1
    import videos to reproduce _your_ problem? You must be kidding :-) It's kind of the other way round: you strip it down to the smallest unit that demonstrates the problem, then there's a realistic probability to get help. In your shoes, I would start with the tablemodel implementation: track down why/how you get invalid columnIndices - would bet one (German!) beer on that being the reason ... – kleopatra Aug 24 '11 at 09:50
  • Nope, no kidding :p (wasn't really expecting anyone to go that far either ;) I've spent hours trying to figure this thing out with no success, I can't describe all the tests here. I had to bypassed some other weird behaviours, because the columns of the view weren't coherent with my model and the events being triggered when e.g. removing a column weren't behaving the way I expected them too. That's why I rather hoped for someone to point me to a component I would have missed and replace the JTable. – Philippe Aug 24 '11 at 11:17
  • you don't have much of a choice to dig until you find the underlying error in your code. Without, even if by some magic it's starting to look like working at some point, it'll be doing so only accidentally, no guarantee it'll stick to it. Good luck! – kleopatra Aug 24 '11 at 12:27
  • I totally agree, don't like magic behaviour in there. If I had a starting point I wouldn't mind digging through it, but after spending quite a lot of time trying to figure this out, I'm really clueless on what methods called by the sorter I can influence to correct (or even understand) it. I've seen different weird behaviours I think are related to timing issues, where e.g. the columns have been updated in the view before the model and the system asks for a column which doesn't exist anymore (see comment in getValue method in FileDisplayTableAdapter) – Philippe Aug 24 '11 at 12:41
  • have seen the comment, that's why I bet on the model being the culprit, somehow :-) How comes the column doesn't exist anymore? How do you remove the column, and how do you notify the system about the removal? – kleopatra Aug 24 '11 at 12:50
  • Now I'm feeling a bit ashamed.. The relevant removal was being done on the table itself. It seems changing the code to remove it from the model solves this issue. This is unrelated to the sorting issue though. – Philippe Aug 24 '11 at 13:41
  • Going to have a Schneiders Weisse now, should I order two? – Philippe Aug 24 '11 at 13:43
  • :-) sure. when, where? No chat (as suggested by the automatic forum police), though, cant convince my firefox to enter – kleopatra Aug 24 '11 at 14:59
  • Would have been in Fribourg, Switzerland :) – Philippe Aug 25 '11 at 06:08

1 Answers1

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • This related [example](http://stackoverflow.com/questions/7137786/how-can-i-put-a-control-in-the-jtableheader-of-a-jtable/7137801#7137801) may offer additional insight. – trashgod Aug 24 '11 at 10:17
  • Thanks, this should be taken care of. I had to refresh the table (according to the model) because the header names got messed up when dragging them around. The TabelModel is implemented by FileDisplayTableAdapter which implements com.jgoodies.binding.adapter.AbstractTableAdapter implementing javax.swing.table.AbstractTableModel. I've read everything I could find on the www to no avail; I'll check out the example you've linked. – Philippe Aug 24 '11 at 11:22
  • I've got the feeling something isn't working well for sorting when using the JTable bound with the jgoodies framework. In an example I had found, The code for sorting was commented out with the remark 'disable sorting because it is buggy anyway'. – Philippe Aug 24 '11 at 11:26
  • It's a problem, as `RowSorter` is new in 1.6. More above. – trashgod Aug 24 '11 at 11:38
  • Ohh I've missed the netbeans ETable. Definitely going to give this a go, as I could keep the rest of the code if it resolves my issues – Philippe Aug 24 '11 at 11:57
  • 1
    I've finally rewritten almost the entire TableModel, got rid of the ColumnModel (which wasn't used anyway as the jgoodies TableAdapter instanciated another one). Everything is working as expected now (https://github.com/taconaut/pms-mlx/tree/pms-mlx-test_project_structure/ps3mediaserver/src/main/java/net/pms/medialibrary/gui/tab/libraryview) – Philippe Jan 03 '12 at 07:43