/* (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));
}
關于對