8

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.

Can anyone tell me which method to use to change that gray to white?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
manav
  • 119
  • 1
  • 2
  • 9

2 Answers2

12

depend of your code

  • JTable#setFillsViewportHeight(true);

or

  • JScrollPane#getViewport().setBackground(JTable#getBackground());

or you can to fits JScrollPanes JViewport to the JTables view by

  • JTable#setPreferredScrollableViewportSize(JTable#getPreferredSize());
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • wincing on the last - that'll "work" only if the scrollPane is contained in a manager which lays out to prefSize. Anyway, setPrefScrollableViewportSize is just as bad as any of the setXXSize, simply don't – kleopatra Sep 21 '12 at 11:26
  • actually, in terms of usabilty the second is a no-go: it only appears like the table fills the entire scrollPane area, but doesn't - so behaviour expected from the table (f.i. componentPopups, tooltips, focus transfer by clicking) will not work and thus confuse the users – kleopatra Sep 21 '12 at 11:30
  • JTable#setFillsViewportHeight(true); worked for me. Thanks. – Danger Dec 27 '17 at 11:24
1

All you have to do is to get the JViewport of the JScrollPane in your JTable and set its background color.

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.

Purnesh
  • 123
  • 1
  • 1
  • 4
  • good idea - but not really recommended, see my comment in @mKorbel 's answer – kleopatra Sep 21 '12 at 11:31
  • @kleopatra: Well Said. Thanks for the Note. It lacks table behaviours, since the Table doesn't fill the entire scrollPane as it appears to be. – Purnesh Sep 24 '12 at 06:04