This discussion is archived
8 Replies Latest reply: Oct 2, 2009 8:18 AM by DarrylBurke RSS

JTable column header to span multiple columns

796211 Newbie
Currently Being Moderated
Hi people. I've spent the last day trying to do this with no luck. Most of the links in the forum are old and don't exist any more and the ones that lead to some code end up not working on java 5 (I can compile and run but the result is not what I wanted)!

So my question is this, how do I have a column header that spans more than one column, which has individual column headers below it?

Thanks, Paul.

EDIT: I should point out that this is the place I've tried and although the screen shot of the first example looks exactly like what I want to do, it does not work as advertised. [http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples1.html]

Edited by: Boomah on 05-Nov-2008 15:08
  • 1. Re: JTable column header to span multiple columns
    796211 Newbie
    Currently Being Moderated
    Hi guys. Sorry to bump this up but I really need a solution to this problem. I know that many people have come up against this, and solutions have been posted. It's just the only solution still available (at least that I can find) doesn't seem to work....

    Could someone please help?

    Thanks, Paul.
  • 2. Re: JTable column header to span multiple columns
    796211 Newbie
    Currently Being Moderated
    please, please, please... can some kind soul help.... ???
  • 3. Re: JTable column header to span multiple columns
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Something like this?
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.TableColumnModelEvent;
    import javax.swing.event.TableColumnModelListener;
    import javax.swing.table.*;
    
    public class MultiSpanHeader {
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             
             @Override
             public void run() {
                new MultiSpanHeader().makeUI();
             }
          });
       }
    
       public void makeUI() {
          final JTable table = new JTable(5, 6);
          final JTableHeader header = table.getTableHeader();
          header.setReorderingAllowed(false);
          final TableColumnModel model = table.getColumnModel();
          Enumeration<TableColumn> enumColumns = model.getColumns();
          final List<TableColumn> columns = Collections.list(enumColumns);
          
          final JTable dummy = new JTable(0, 3);
          final JTableHeader dummyHeader = dummy.getTableHeader();
          dummyHeader.setReorderingAllowed(false);
          dummyHeader.setResizingAllowed(false);
          final TableColumnModel dummyModel = dummy.getColumnModel();
          Enumeration<TableColumn> enumDummyColumns = dummyModel.getColumns();
          final List<TableColumn> dummyColumns = Collections.list(enumDummyColumns);
          
          model.addColumnModelListener(new TableColumnModelListener() {
    
             @Override
             public void columnAdded(TableColumnModelEvent e) {
             }
    
             @Override
             public void columnRemoved(TableColumnModelEvent e) {
             }
    
             @Override
             public void columnMoved(TableColumnModelEvent e) {
             }
    
             @Override
             public void columnMarginChanged(ChangeEvent e) {
                dummyColumns.get(0).setWidth(columns.get(0).getWidth());
                dummyColumns.get(1).setWidth(columns.get(1).getWidth() +
                      columns.get(2).getWidth());
                dummyColumns.get(2).setWidth(columns.get(3).getWidth() +
                      columns.get(4).getWidth() + columns.get(5).getWidth());
             }
    
             @Override
             public void columnSelectionChanged(ListSelectionEvent e) {
             }
          });
          JScrollPane pane = new JScrollPane(table);
          pane.setPreferredSize(new Dimension(600, 200));
          
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(pane);
          frame.pack();
          
          JPanel panel = new JPanel(new GridLayout(2, 1));
          panel.add(dummyHeader);
          panel.add(header);
          pane.getColumnHeader().setView(panel);
          
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }
    Note: this is just presented as an idea, would need a lot of work to make it robust and flexible enough for practical use.

    db
  • 4. Re: JTable column header to span multiple columns
    843806 Newbie
    Currently Being Moderated
    The link you posted (for the infamous GroupableHeader code) perhaps points to outdated source.

    Read through this thread. It contains more links for updated versions. Not saying they work these days either but it should be closer and if not you can fix it and post it back for the rest of us :-)
  • 5. Re: JTable column header to span multiple columns
    796211 Newbie
    Currently Being Moderated
    Hey guys. Thanks very much for your responses. Tom, the link you posted does contain source that sort of works in that tables are displayed with multiple headers. However, it seems that the resize although works is a bit funny and the reorder doesn't do what I expect at all.

    Darryl. Thanks very much for posting that code. I had something similar and came to the same conclusion as you, it would need lots of work to make it robust and act the way I wanted it, which is why I was hoping something else had been done.

    We even purchased JIDE software as their table (or grid as they call it) promised multiple spanning headers and although it is very close, there is still a problem with the reordering.

    It looks like I'm going to give in for a bit and maybe revisit when I have a bit of spare time. For now I'm just going to keep single columns with long names describing exactly what they do!

    Thanks anyway, Paul.
  • 6. Re: JTable column header to span multiple columns
    843807 Newbie
    Currently Being Moderated
    I also need to span a JTable header cell into multiple columns,
    or equivalently merge the cells of a JTable header;
    basically something like this:

    -------------
    | test |
    -------------
    | | |
    -------------
    | | |
    -------------

    I couldn't find the answer to that in the tutorial online, nor in the javadocs, nor in books.
  • 7. Re: JTable column header to span multiple columns
    843807 Newbie
    Currently Being Moderated
    Something went wrong with the visualization of the table.
    I meant something like this:
    -----------------
    |      test     |
    -----------------
    |       |       |
    -----------------
    |       |       |
    -----------------
    Edited by: krige on Oct 2, 2009 8:01 AM

    Edited by: krige on Oct 2, 2009 8:02 AM

    Edited by: krige on Oct 2, 2009 8:03 AM
  • 8. Re: JTable column header to span multiple columns
    DarrylBurke Guru Moderator
    Currently Being Moderated
    krige, please don't post in threads that are long dead. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

    I'm locking this thread now.

    db