简体   繁体   中英

JTable Headers and Spacer Rows

I'm new to Swing and have been struggling with the JTable component today. Because I'm trying to make a somewhat-fancy table I haven't been able to find the particular answers I'm looking for anywhere yet.

I would like to have a "group" of headers at the very top of the table. This means 3 or 4 rows that span the whole table, and "group" the various columns, essentially partitioning them.

Example (pardon the eye strain):

////////////////////////////////ANIMALS//////////////////////////
/////////Mammals///////////////////////Rodents///////////////////
////People/////Dogs//////////Rats/////Rodents of Unusual Size////

Where slashes ("/") denote whitespace. The first header, titled "ANIMALS" refers to all the columns in the table. The second header down partitions ANIMALS, so on and so forth. After the third and final header we arrive the actual table, where each row will have 4 columns, one for each type of Animal.

Obviously this is an example, and probably a bad one, but this is what I mean by "partitioning headers".

My question: I don't see how JTableHeader and TableColumnModel support this functionality. If they do, can someone nudge me in the right direction. If they don't, this surely can't be the first time a Swing app has needed something like this. Any 3rd party components anyone knows of?

Also, I'd like to be able to have "Spacer" or Spanning Rows sprinkled throughout my table, representing a group of rows. These Spacers would exist inside the table (not on top of it like a header), but would span every column and have a single label, representing the name of the group represented by the subsequent rows. Again, I don't see Swing supporting this, any ideas?

Thanks and I do apologize for the truly-awful example/drawing.

You might be able to adapt this venerable Multiple Row Header example.

By default there is only one row of header in a JTable (and it's not properly a row of the JTable it's a JTableHeader). Anyway you can implement a TableCellRenderer that display the first n non-header-rows in a fashion like it seems to be header.

The steps are more ore less the following:

  1. Implement TableCellRenderer to display ordinar JTable's cell of desired row like header's cell

like:

public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column){   

    this.setOpaque(true);
    TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer();
    return renderer.getTableCellRendererComponent(table, value, isSelected,
          hasFocus, row, column);
}
  1. Extend JTable and implement the following method in order to return the desidered cell renderer:

     public TableCellRenderer getCellRenderer(int row, int column){}

Anyway with your requirements there is another problem to solve: a JTable has a fixed number of column, so you have to find a workaround to achive that result.

cell (both row/column) grouping or splitting is not supported by core Swing, neither for the header nor the table itself. A JTable by design is not a general-purpose Grid. Trying to tweak some grid-like behaviour and visuals into it would be fighting its design. Can be done, but it's not trivial to achieve. @Trashgod pointed to some of those fights (do they still work? they look like the oldish first-generation Tame examples?). A JIDE is a commercial product which supports it (disclaimer: never used it in production:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM