/* (swing1.1beta3) */
package tame.examples;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;

import tame.table.ColumnGroup;
import tame.table.GroupableTableHeader;
import tame.table.GroupableTableHeaderUI;

/**
<pre>
* |-----------------------------------------------------|
* |        |       Name      |         Language         |
* |        |-----------------|--------------------------|
* |  SNo.  |        |        |        |      Others     |
* |        |   1    |    2   | Native |-----------------|
* |        |        |        |        |   2    |   3    |
* |-----------------------------------------------------|
* |        |        |        |        |        |        |

</pre>
@author Nobuo Tamemasa
@version 1.0 11/09/98
*/
public class GroupableHeaderExample extends JFrame {

    GroupableHeaderExample() {
      super( "Groupable Header Example" );

      DefaultTableModel dm = new DefaultTableModel();
      dm.setDataVector(new Object[][]{
        {"119","foo","bar","ja","ko","zh"},
        {"911","bar","foo","en","fr","pt"}},
      new Object[]{"SNo.","1","2","Native","2","3"});

      JTable table = new JTable( dm ) {
protected JTableHeader createDefaultTableHeader() {
            return new GroupableTableHeader(columnModel);
        }
      };
      TableColumnModel cm = table.getColumnModel();  //得到列模型,可以取出表头
      ColumnGroup g_name = new ColumnGroup("Name");
      g_name.add(cm.getColumn(1));
      g_name.add(cm.getColumn(2));
      ColumnGroup g_lang = new ColumnGroup("Language");
      g_lang.add(cm.getColumn(3));
      ColumnGroup g_other = new ColumnGroup("Others");
      g_other.add(cm.getColumn(4));
      g_other.add(cm.getColumn(5));
      g_lang.add(g_other);
      GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
      header.addColumnGroup(g_name);
      header.addColumnGroup(g_lang);

      TableCellRenderer renderer =  new DefaultTableCellRenderer() {
          public Component getTableCellRendererComponent(JTable table, Object value,
                           boolean isSelected, boolean hasFocus, int row, int column) {
            JTableHeader header = table.getTableHeader();
            if (header != null) {
              setForeground(header.getForeground());
              setBackground(header.getBackground());
              setFont(header.getFont());
            }
            setHorizontalAlignment(JLabel.CENTER);
            setText((value == null) ? "" : value.toString());
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            return this;
          }
        };

      TableColumnModel model = table.getColumnModel();
      for (int i=0;i<model.getColumnCount();i++) {
        model.getColumn(i).setHeaderRenderer(renderer);
      }
        table.getTableHeader().setUI(new GroupableTableHeaderUI());
      JScrollPane scroll = new JScrollPane( table );
      getContentPane().add( scroll );
      setSize( 400, 120 );
    }

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

}

____________________________________________________________________________

public class GroupableHeaderExample extends JFrame {

  GroupableHeaderExample() {
    super( "Groupable Header Example" );

    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][]{
      {"119","foo","bar","ja","ko","zh"},
      {"911","bar","foo","en","fr","pt"}},
    new Object[]{"SNo.","1","2","Native","2","3"});

    JTable table = new JTable( dm ) {
      protected JTableHeader createDefaultTableHeader() {
  return new GroupableTableHeader(columnModel);
      }
    };
    TableColumnModel cm = table.getColumnModel();
    ColumnGroup g_name = new ColumnGroup("Name");
    g_name.add(cm.getColumn(1));
    g_name.add(cm.getColumn(2));
    ColumnGroup g_lang = new ColumnGroup("Language");
    g_lang.add(cm.getColumn(3));
    ColumnGroup g_other = new ColumnGroup("Others");
    g_other.add(cm.getColumn(4));
    g_other.add(cm.getColumn(5));
    g_lang.add(g_other);
    GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
    header.addColumnGroup(g_name);
    header.addColumnGroup(g_lang);

    TableCellRenderer renderer =  new DefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value,
                         boolean isSelected, boolean hasFocus, int row, int column) {
          JTableHeader header = table.getTableHeader();
          if (header != null) {
            setForeground(header.getForeground());
            setBackground(header.getBackground());
            setFont(header.getFont());
          }
          setHorizontalAlignment(JLabel.CENTER);
          setText((value == null) ? "" : value.toString());
          setBorder(UIManager.getBorder("TableHeader.cellBorder"));
          return this;
        }
      };

    TableColumnModel model = table.getColumnModel();
    for (int i=0;i<model.getColumnCount();i++) {
      model.getColumn(i).setHeaderRenderer(renderer);
    }
    JScrollPane scroll = new JScrollPane( table );
    getContentPane().add( scroll );
    setSize( 400, 120 );
  }

  public static void main(String[] args) {
    GroupableHeaderExample frame = new GroupableHeaderExample();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
  System.exit(0);
      }
    });
    frame.setVisible(true);
  }
}
——————————————————————————————————————
我发现表头和所对应的列不能够对齐,这是怎么一回事 ?
原因为在先前的header中已经考虑过margin的问题了,而后面重复累加了此margin。你可以更改GroupableTableHeaderUI的paint方法,删除对margin的累加。
本来我已经看到这种不能对齐的情况,没想到你真的来问。不过我还是鼓励你自己解决,实在不行的话,在来此问。
具体代码如下:
				
public void paint(Graphics g, JComponent c) {
Rectangle clipBounds = g.getClipBounds();
if (header.getColumnModel() == null) return;
// ((GroupableTableHeader)header).setColumnMargin();
int column = 0;
Dimension size = header.getSize();
Rectangle cellRect = new Rectangle(0, 0, size.width, size.height);
Hashtable h = new Hashtable();
// int columnMargin = header.getColumnModel().getColumnMargin();

Enumeration enumeration = header.getColumnModel().getColumns();
while (enumeration.hasMoreElements()) {
cellRect.height = size.height;
cellRect.y = 0;
TableColumn aColumn = (TableColumn)enumeration.nextElement();
Enumeration cGroups = ((GroupableTableHeader)header).getColumnGroups(aColumn);
if (cGroups != null) {
int groupHeight = 0;
while (cGroups.hasMoreElements()) {
ColumnGroup cGroup = (ColumnGroup)cGroups.nextElement();
Rectangle groupRect = (Rectangle)h.get(cGroup);
if (groupRect == null) {
groupRect = new Rectangle(cellRect);
Dimension d = cGroup.getSize(header.getTable());
groupRect.width = d.width;
groupRect.height = d.height;
h.put(cGroup, groupRect);
}
paintCell(g, groupRect, cGroup);
groupHeight += groupRect.height;
cellRect.height = size.height - groupHeight;
cellRect.y = groupHeight;
}
}
cellRect.width = aColumn.getWidth() ;//+ columnMargin;
if (cellRect.intersects(clipBounds)) {
paintCell(g, cellRect, column);
}
cellRect.x += cellRect.width;
column++;
}
}
————————————————————————————————————————————————————



在AttributiveCellTableMode中暂时把setDataVector方法的内容用下面的代码替代:

public void setDataVector(Vector newData, Vector columnNames) {
if (newData == null)
throw new IllegalArgumentException("setDataVector() - Null parameter");

dataVector = (newData != null) ? newData : new Vector(0);
columnIdentifiers = (columnNames != null) ? columnNames : new Vector(0);

cellAtt = new DefaultCellAttribute(dataVector.size(),
columnIdentifiers.size());

newRowsAdded(new TableModelEvent(this, 0, getRowCount()-1,
     TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}


关于对