JTable column spanning
Asked Answered
S

3

6

I am trying to make a JTable that has column spans available. Specifically, I am looking to nest a JTable inside another JTable, and when the user clicks to view the nested table, it should expand to push down the rows below and fill the empty space. This is similar to what you see in MS Access where you can nest tables, and clicking the expand button on a row will show you the corresponding entries in the nested table.

If someone knows of a way to perform a column span with JTable, can you please point me in the right direction? Or if you know of an alternative way to do this, I am open to suggestions. The application is being built with Swing. Elements in the table, both high level and low level, have to be editable in any solution. Using nested JTables this won't be a problem, and any other solution would have to take this into consideration as well.

Shaquitashara answered 28/9, 2008 at 19:0 Comment(0)
R
4

As a pointer in the right direction, try this article at SwingWiki that explains the TableUI method of column spanning quite well. Before this, I also tried some alternatives such as overriding the TableCellRenderer paint methods without much success.

Rad answered 29/9, 2008 at 3:38 Comment(1)
Copy if article is still offline: web.archive.org/web/20080726035429/http://www.swingwiki.org/…Fujio
B
0

You need to write your own TableUI for the master table. It can also helpful to use your own TableModel to save additional data like if a row is expanded. But this is optional.

I write an equals TableUI that expand a row and show an text editor. In the TableUI you need to change the the row hight dynamically with table.setRowHeight(height). Also it is necessary to copy some stuff from the BaseTableUI because you can not access the private stuff.

Burdelle answered 28/9, 2008 at 19:36 Comment(1)
I still don't see how you would go about telling your JTable to merge columns in a given row. I know how to insert a new row into the table, but how do I tell the table to merge those cells?Shaquitashara
F
0

Based on Code from Code-Guru:

/*
 *  (swing1.1beta3)
 * 
 * |-----------------------------------------------------|
 * |   1st  |      2nd    |      3rd         |
 * |-----------------------------------------------------|
 * |    |    |    |    |        |        |
 */
//package jp.gr.java_conf.tame.swing.examples;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

import jp.gr.java_conf.tame.swing.table.*;

/**
 * @version 1.0 11/09/98
 */
public class MultiWidthHeaderExample extends JFrame {

  MultiWidthHeaderExample() {
    super( "Multi-Width Header Example" );

    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][]{
      {"a","b","c","d","e","f"},
      {"A","B","C","D","E","F"}},
    new Object[]{"1 st","","","","",""});

    JTable table = new JTable( dm ) {
      protected JTableHeader createDefaultTableHeader() {
        return new GroupableTableHeader(columnModel);
      }
    };
    TableColumnModel cm = table.getColumnModel();
    ColumnGroup g_2nd = new ColumnGroup("2 nd");
    g_2nd.add(cm.getColumn(1));
    g_2nd.add(cm.getColumn(2));
    ColumnGroup g_3rd = new ColumnGroup("3 rd");
    g_3rd.add(cm.getColumn(3));
    g_3rd.add(cm.getColumn(4));
    g_3rd.add(cm.getColumn(5));
    GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
    header.addColumnGroup(g_2nd);
    header.addColumnGroup(g_3rd);
    JScrollPane scroll = new JScrollPane( table );
    getContentPane().add( scroll );
    setSize( 400, 100 );  
    header.revalidate(); 
  }

  public static void main(String[] args) {
    MultiWidthHeaderExample frame = new MultiWidthHeaderExample();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
  }
}

Source: http://www.codeguru.com/java/articles/125.shtml (unavailable since 2012, see now in web archive)

Other ressources:

Fujio answered 21/2, 2011 at 12:4 Comment(1)
Code-Guru says "page not found". That's exactly why link-only answers are discouraged.Caulis

© 2022 - 2024 — McMap. All rights reserved.