• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does a vertical scrollbar appears?  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable inside a JScrollPane. I managed to put the horizontal scrollbar just under the table, but if it needs to be drawn it brings some margin inside the panel and consequently the vertical scrollbar. What am I missing?
And is this the right way to do this? I imagine that I should ovveride some method in order to keep track of the size of the JFrame. And I read that using setXXXsize methods is generally a bad idea.


 
Marshal
Posts: 59462
187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you have been through the Java™ Tutorials about scroll panes. What you describe appears to be normal behaviour if the component inside the scroll pane almost completely fills it. If you remove space for the scroll bar one way, there is no longer enough space for the table and scroll bar the other way will have to appear. Consider making the scroll pane a bit larger to accommodate the horizontal scroll bar.
 
German Jackson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read it and it brought me to this code.
The problem is that I believe to have taken the height of the horizontal scrollbar into account and I stored it in the variable barHeight.
What am I missing?
 
German Jackson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thank you for the welcome!
 
Campbell Ritchie
Marshal
Posts: 59462
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Sorry Maybe you need to allow slightly more size than the exact size of the table.

Maybe somebody else will know.
 
Rancher
Posts: 2915
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what your real requirement is so I con only make a couple of comments:

1. Components should be added to the frame BEFORE the frame is make visible.

2. you should use frame.pack() just before frame.setVisible() and components will be displayed at their preferred sizes.

3. To display a table in a scroll pane without scrollbars you can use:



before you add the table to the scrollpane.
 
German Jackson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Not sure what your real requirement is so I con only make a couple of comments:


I need the table to be shown at a certain size depending on the size of the frame and if necessary to show the scrollbars. The horizontal one should be just under the table.

1. Components should be added to the frame BEFORE the frame is make visible.


I actually added them before the call to setVisible. What I did after is to calculate their size because otherwise i would get 0.

2. You should use frame.pack() just before frame.setVisible() and components will be displayed at their preferred sizes.


Thank you for making me learn a new method, but it makes the frame become huge if there are too much columns.

3. To display a table in a scroll pane without scrollbars you can use:



before you add the table to the scrollpane.

If a horizontal scrollbar is needed there is the same problem. A vertical scrollbar is shown.
 
German Jackson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify the table should fill the frame horizontally and take the vertical space it needs. The window can be resized to show more of it.
 
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made some changes to the code posted initially, and here it is.



Note the changes:
- Added line 13-14
- Changed code on line 22, to add 2 to the height
- Finally to the end of the program, there is some code to add data to the table (which is commented):
// Adds 5 rows of data to the table
// Adds 6th and 7th rows to the table data

Try this:
1. Run the code I posted.
2. Run the code with the  comments removed for "Adds 5 rows of data to the table".
3. Run the code with the  comments  removed for "Adds 6th and 7th row to the table data".

Observe the results at each step. See if this helps in anyway to resolve/understand the issue you are facing.
 
Campbell Ritchie
Marshal
Posts: 59462
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does look better, but you are lucky not to have some sort of baleful effects from not calling setVisible() last.
 
Rob Camick
Rancher
Posts: 2915
18
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If a horizontal scrollbar is needed there is the same problem. A vertical scrollbar is shown. 



Well you can modify the logic to control the width:



No need for the code to calculate the size of the scrollpane.

Or another option might be to have the horizontal scrollbar always displayed. It will be greyed out if it is not needed. Read the JScrollPane API for the constructor or method to set this property.
 
German Jackson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:

If a horizontal scrollbar is needed there is the same problem. A vertical scrollbar is shown. 



Well you can modify the logic to control the width:



No need for the code to calculate the size of the scrollpane.

Or another option might be to have the horizontal scrollbar always displayed. It will be greyed out if it is not needed. Read the JScrollPane API for the constructor or method to set this property.



It works when I combine your solutions(with the horizontal scrollbar always shown), but why?

@Campbell Ritchie: why?

@Prasad Saya: I understand your example. But where does the +2 in the height come from?
 
German Jackson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was the Border of the JScrollpane. If you set a border to null the default border will be rendered. Hence the two extra pixels in height(and in width, too). I had to use this:
In addition I called setSize instead of setPreferredSize on the tableHeader. With the latter I can place the call to setVisible at the end.
 
today's feeble attempt to support the empire
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!