设置JTableHeader持有者背景颜色 [英] Setting JTableHeader holder background colour
问题描述
我的程序中有一个 JTable
,我想改变 JTableHeader
的颜色。我使用以下代码完成了此操作
I have a JTable
in my program and I wanted to change the color of the JTableHeader
. I did this using the following code
JTableHeader header = table.getTableHeader();
header.setBackground(Color.WHITE);
然而,当我拖动标题时,我注意到标题后面有一个灰色区域,如图所示在下面的照片中。
However, when I dragged the header, I noticed that there was a grey area behind the header as shown in the photo below.
我该怎么办?设置为白色以便它适合我的 JTableHeader
?
How can I set this to white so that it fits in with my JTableHeader
?
MCVE
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class JTableTest extends JFrame {
private JTableTest() {
super("JTable Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
createPanel();
pack();
setVisible(true);
}
JPanel panel = new JPanel(new GridLayout(1, 1));
JScrollPane scroll = new JScrollPane();
private void createPanel() {
Object[] headers = {"Select", "Title", "Artist", "Length"};
Object[][] sampleData = {{true, "Bat Outta Hell", "Meat Loaf", "673"},
{false, "Spanish Train", "Chris De Burgh", "358"}};
JTable table = new JTable(sampleData, headers);
///
JTableHeader header = table.getTableHeader();
header.setBackground(Color.WHITE); //Sets header white
///
scroll.getViewport().add(table);
scroll.getViewport().setBackground(Color.WHITE); //Sets table container white
panel.add(scroll);
panel.setBackground(Color.WHITE); //Sets scroll pane container white
getContentPane().add(panel);
getContentPane().setBackground(Color.WHITE); //Sets panel container white
//What should be set to white to make header container white
}
public static void main(String[] args) {
SwingUtilities.invokeLater (
new Runnable() {
@Override
public void run() {
new JTableTest();
}
}
);
}
}
推荐答案
好正如我所料,我们需要将标题的父级设置为您想要的背景颜色。
Well as I expected we need to set the parent of the header to be the background color you want.
但滚动窗格不是我期望的标题的父级。
However the scroll pane is NOT the parent of the header as I expected.
当我添加如下代码时:
System.out.println( header.getParent() );
它显示JViewport作为让我困惑的父母。
it showed a JViewport as the parent which confused me.
但是当我添加:
System.out.println( scroll.getViewport() );
我注意到两个视口不同。所以看起来滚动窗格的视口与标题的视口不同。
I noticed that the two viewports were different. So it appears the viewport of the scrollpane is different than the viewport of the header.
因此解决问题的方法是:
so the solution to the problem is:
JTableHeader header = table.getTableHeader();
header.setBackground(Color.GREEN);
然后 AFTER 可以看到框架:
header.getParent().setBackground(Color.YELLOW);
(我使用不同的颜色只是为了显示每个陈述的效果)
(I used different colors just to show the effects of each statement)
请注意,在框架打包或可见之前,标题不会添加到滚动窗格中。如果您在创建表时尝试设置背景,则在尝试访问父项时将获得NPE。
Note the header is not added to the scrollpane until the frame is packed or made visible. If you try to set the background when the table is created you will get a NPE when trying to access the parent.
或另一个选项是添加 AncestorListener
到 JTableHeader
。然后可以调用代码,然后将标头添加到可见帧。对于这种类型的方法,请查看请求焦点侦听器 /rel =nofollow noreferrer>对话焦点
Or another option is to add an AncestorListener
to the JTableHeader
. Then the code can be invoked then header is added to a visible frame. For this type of approach check out the Request Focus Listener
found in Dialog Focus
这篇关于设置JTableHeader持有者背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!