我想使用JLabel
重新创建表头。 JLabel
的外观与系统指定的JTableHeader
完全相同。
这是我到目前为止所尝试的:
JLabel header = new JLabel("Title");
header.setOpaque(true);
header.setBackground(UIManager.getColor(new JTableHeader().getBackground()));
header.setBorder(UIManager.getBorder(new JTableHeader().getBorder()));
但是,UIManager
会为颜色和边框返回null
。
有什么想法吗?
这就是我设置外观的方式:
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
答案 0 :(得分:4)
还有更多问题需要获取表头的颜色和边框。每个单元格/列都由TableCellRenderer
呈现,这意味着UIManager
返回的值可能会被忽略...
例如,以下内容呈现JTableHeader
并根据窗口外观下的JLabel
返回的值将边框/背景应用于UIManager
正如您所看到的,它们之间存在很大差异
然而,如果您感兴趣的是在滚动窗格上的另一个组件的顶部显示某种“组标题”,则只需在滚动窗格列视图中添加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);
}
});
}
}
答案 1 :(得分:2)
在尝试之前,您需要为应用程序设置外观:
header.setBackground(UIManager.getColor(new JTableHeader().getBackground()));
header.setBorder(UIManager.getBorder(new JTableHeader().getBorder()));
你应该首先设置一个外观和感觉:
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
以下是一个例子:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.JTableHeader;
public class Test {
public Test() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
//set nimbus look and feel
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new Test();
}
});
}
private void initComponents() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel header = new JLabel("Title");
header.setBackground(UIManager.getColor(new JTableHeader().getBackground()));
header.setBorder(UIManager.getBorder(new JTableHeader().getBorder()));
frame.add(header);
frame.pack();
frame.setVisible(true);
}
}
答案 2 :(得分:2)
尝试从UIManager
获取默认值:
Color color = UIManager.getColor("TableHeader.background");
Border border = UIManager.getBorder("TableHeader.CellBorder");
答案 3 :(得分:0)
我想我会创建一个没有任何行的JTable
并在其下方放置JTextPane
。
它就像魅力一样。
JTextPane textPane = new JTextPane();
JPanel panel = new JPanel(new BorderLayout());
JTable table = new JTable(0, 1);
table.setPreferredScrollableViewportSize(new Dimension(600, 0));
JScrollPane js = new JScrollPane(table)
panel.add(js, BorderLayout.NORTH);
panel.add(new JScrollPane(textPane),BorderLayout.CENTER);