除了获取表格标题的颜色和边框之外,还涉及更多问题。TableCellRenderer
每个单元格/列都由一个含义呈现,即UIManager
可以忽略返回的值...
例如,以下基于Window 的 Look and Feel 下返回的值呈现JTableHeader
并应用边框/背景...JLabel
UIManager
如您所见,它们之间存在很大差异
但是,如果您感兴趣的只是在滚动窗格上的另一个组件的顶部显示某种“组标题”,您可以直接将 a 添加JTableHeader
到滚动窗格的列视图中......
public class TestHeader {
public static void main(String[] args) {
new TestHeader();
}
public TestHeader() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
TableColumnModel model = new DefaultTableColumnModel();
final TableColumn column = new TableColumn(0, 250);
column.setHeaderValue("Test");
model.addColumn(column);
JTableHeader header = new JTableHeader();
header.setColumnModel(model);
final JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setColumnHeaderView(header);
textArea.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
column.setWidth(textArea.getWidth());
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
更新
public class TestHeader {
public static void main(String[] args) {
new TestHeader();
}
public TestHeader() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
TableColumnModel model = new DefaultTableColumnModel();
final TableColumn column = new TableColumn(0, 250);
column.setHeaderValue("I don't see the problem");
model.addColumn(column);
final JTableHeader header = new JTableHeader();
header.setColumnModel(model);
DefaultTableModel tm = new DefaultTableModel(new Object[]{"A", "B", "C"}, 0);
tm.addRow(new Object[]{"1", "2", "3", "4"});
tm.addRow(new Object[]{"5", "6", "7", "8"});
tm.addRow(new Object[]{"9", "10", "11", "12"});
tm.addRow(new Object[]{"13", "14", "15", "16"});
final JTable table = new JTable(tm);
final JScrollPane scrollPane = new JScrollPane(table);
/**
* For some reason, the header isn't being applied as soon as the
* table is added to the scroll pane, so we need to jump our next
* request to the end of the of event queue so that it will
* occur some time in the future
*/
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
scrollPane.setColumnHeaderView(header);
}
});
table.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
column.setWidth(table.getWidth());
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}