2

I'm trying to find a way to make a Java table (Swing component) that can wrap the list of columns into multiple lines per row.

     /[     Column Hdr A    ][    Column Hdr B    ]
Hdr  |[ Column Hdr C  ][columnHdr D ][ColumnHdr E ]
     \[    column Hdr F ][COlhdr G][colhdr H][CH I]
     /[     Cell 1 A        ][    Cell 1 B        ]
Row 1|[ Cell 1 C      ][cell 1 D    ][Cell 1 E    ]
     \[    Cell 1     F ][Cell 1 G][Cell 1 H][C 1I]
...

Where each column size is independent of any other. IE: I'm not doing spanning or column header grouping. It should retain the column resizing, hiding and sorting features. Drag and drop re-ordering would be nice but isn't necessary.

So I've searched everywhere for something like this. All I've found are various schemes for spanning cells or using fixed width sub-columns. There was one person who claimed to have done it by overriding getRect, but there was no code to look at, so I'm not sure how that would work wrt to resizing or hiding columns, and how would you specify which columns went where?

I've considered just extending TableColumn to include a "sub-row" property but that means also having custom TableColumnModel, JTableHeader and Jtable, AND Jpanel. And I suspect that the renderer and all the LookandFeel UIs would also have to be modified.

An ugly hack that occurred to me is to create one table per sub-row of columns, and then use some form of mutant jpanel to expose the rendered rows interleaved down the y axis. I'm not sure that would work with scroll bars though.

So, does anyone have a neat, concise way to implement this? Any suggestions about how to proceed?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chris J. Kiick
  • 1,487
  • 11
  • 14
  • Maybe look at [this](http://stackoverflow.com/a/11666046/2587435) – Paul Samsotha Apr 03 '14 at 02:55
  • 1
    It appears this use-case needs a renderer that returns a compound component. OTOH most users would find it better utility to simply have 9 columns per (single line) row. Is this table intended to support sorting by each cell value? – Andrew Thompson Apr 03 '14 at 03:08
  • As I said, these are not group headers. The reason for not using one line for all columns is that some of them are quite wide, and scrolling back and forth to read everything would be quite distracting. I'd like to support sorting by each column. – Chris J. Kiick Apr 03 '14 at 10:42
  • I have it (mostly) working. I had to extend Jtable, DefaultTableColumnModel, implement my own TableUI, and I'm still working on getting the header bugs ironed out. While implementing this, I found several ways that it won't work, and have ideas for a couple of alternatives that might work and be less code. – Chris J. Kiick Apr 12 '14 at 19:42

4 Answers4

1

I would hope that you would re-organize the output to be more human readable. While this does fit the data onto the page, a human reading it would have a great deal of problems trying to understand what table cell belonged to what header and which row was which.

Can you create rows with only the 'important' data visible first, then the user could click (or some gesture) to open the more detailed results? Or include multiple cell values together in a multi-line cell to reduce the data to a single row per record?

This gives you two great benefits. First the data will be understandable at a glance, and second, you can use the existing table implementations and save yourself the significant development work.

Finally, have you tried using an HTML widget to display the data? You could create the table as divs in HTML and have the widget wrap the rows. Then you could style the cells for each column to be a fixed width.

Zagrev
  • 2,000
  • 11
  • 8
  • If each sub-row has a different background color, then matching cells to headers is easier. Using multi-line cells doesn't allow resizing of each one separately. It also makes it harder for the user to edit/input the data. I'll look into using HTML, but I suspect that this would not allow resizing of the columns. – Chris J. Kiick Apr 03 '14 at 10:51
  • Even with each row a different color, you will have issue when the distance from the header row is more than 2 rows, and editing in this format will be quite difficult. While your users may be able to figure this out, working on matching the columns distracts the user from focusing on the actual task to be performed. – Zagrev Apr 05 '14 at 19:51
1

I can't offer you any code, but you could look into customizing the table model (class MyTableModel extends AbstractTableModel {...}).

The approach could be to overload the SetValueAt(Object value, int row, int col) method so that writing row 1, col 3 for example would actually write row 2 , col 1 and so on to display multiple lines for 1 line of you data table

Archytas60
  • 11
  • 2
1

Do you indeed need the behaviour which prevents some columns from scrolling?

It is a quite standard usability solution. I would recommend you to come up with a dialog allowing to hide and freeze columns. In this case users could organize the viewport the way they feel best at the moment. Here is an example of scroll freezing.

The second option may well be HTML. You can generate whatever you want this way. Though this solution may have a limitation if you need to edit your data.

Ergonomics uggestions:

  1. Is it acceptable to present headers or data vertically? Sometimes it is better readable.
1

Basically JTable supports adding swing components including nested tables or cells. please refer to the ff links

com.lang.java.gui discussion

a similar thread

Community
  • 1
  • 1
Misgevolution
  • 825
  • 10
  • 22