java 如何将 JTable 背景颜色默认更改为白色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12528792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change JTable Background Color default to white?
提问by manav
I have a JTable that I am displaying in a JScrollpane. The JTable only displays a few rows of information in its grid. The space below the grid to the bottom of the JPanel, that contains the JScrollpane, (Which in turn contains the JTable) is colored solid gray. I'd like to change that color to white. I tried setting the JTable's background color to white, [using the method setBackground(Color,WHITE) ] but that didn't work.
我有一个 JTable 显示在 JScrollpane 中。JTable 仅在其网格中显示几行信息。网格下方到 JPanel 底部的空间,包含 JScrollpane,(又包含 JTable)是纯灰色的。我想把那个颜色改成白色。我尝试将 JTable 的背景颜色设置为白色,[使用方法 setBackground(Color,WHITE) ] 但这没有用。
Can anyone tell me which method to use to change that gray to white?
谁能告诉我使用哪种方法将灰色变为白色?
回答by mKorbel
depend of your code
取决于你的代码
JTable#setFillsViewportHeight(true);
JTable#setFillsViewportHeight(true);
or
或者
JScrollPane#getViewport().setBackground(JTable#getBackground());
JScrollPane#getViewport().setBackground(JTable#getBackground());
or you can to fits JScrollPanes JViewport
to the JTables view
by
或者你可以配合JScrollPanes JViewport
到JTables view
由
JTable#setPreferredScrollableViewportSize(JTable#getPreferredSize());
JTable#setPreferredScrollableViewportSize(JTable#getPreferredSize());
回答by Purnesh
All you have to do is to get the JViewport of the JScrollPane in your JTable and set its background color.
您所要做的就是在 JTable 中获取 JScrollPane 的 JViewport 并设置其背景颜色。
JTable table=new JTable(model);
JScrollPane scroll=new JScrollPane(table);
scroll.getViewport().setBackground(Color.WHITE);
Hope that gives a solution, for making your remaining table space as WHITE.
希望给出一个解决方案,使您剩余的表空间为白色。