This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Keep Space at The Bottom Of My Scrollable JTable  RSS feed

 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a JTable that I have added to a JScrollPane. The JScrollPane has a fixed length and width. In the initial state, the table has no rows and as such there is an empty area under the table column headers. As the user enters rows, this empty area shrinks, until eventually the table has more rows than can fit in the view area, at which time the vertical scroll bar invokes. All is well until this point.

What I need to do is keep a minimal amount of empty space underneath the table such that the user can click in the scrollable area (in the empty space) and not click on the table.

I tried to create a JPanel, add the table to it and then a fixed size JPanel under the table, then add the JPanel with these objects onto the JScrollPane, which DOES work, but then the table headers dont show up in the JScrollPane.

Does anyone have any ideas on how I can accomplish the above? I am fairly good in Swing, but I am stumped on this one.

Thanks in advance for any help.
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All...thanks anyway, I figured this out. For anyone that has a similar issue, code I used below. I had failed to use tablePanel.add(aTable.getTableHeader());

JPanel fillPanel = new JPanel();
fillPanel.setPreferredSize(new Dimension(0, 100)); //obviously, I dont care about the width, its the height I need at minimum 100 pixels.

JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS));
tablePanel.add(aTable.getTableHeader());
tablePanel.add(aTable);
tablePanel.add(fillPanel);

aScrollPane = new JScrollPane(tablePanel);
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel silly being the only one in here and posting three times , but in case any else ever has the saem problem, the final solution is as follows, my last post was close but no cigar...

JPanel fillPanel = new JPanel();
fillPanel.setPreferredSize(new Dimension(0, 100));

JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS));
tablePanel.add(aTable);
tablePanel.add(fillPanel);

JScrollPane aScrollPane = new JScrollPane(tablePanel);
aScrollPane.setColumnHeaderView(aTable.getTableHeader());
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!