Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable not occupying full scroll pane size - Scrollbar issue

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a JTable with only one column. I am enclosing a JTable inside a JScrollPane. I am facing two issues.

1. When i make resize mode property of JTable as JTable.AUTO_RESIZE_OFF, the table ends halfway in the scroll pane and leaves the right side blank in the scroll pane.

2. If i remove the above property, then the JTable occupies the entire ScrollPane view port, but the horizontal scrollbar is not scrolling the entire column width of the JTable. the column is cut out.

Code -

 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the JTable occupies the entire ScrollPane view port, but the horizontal scrollbar is not scrolling the entire column width of the JTable. the column is cut out.

Not likely. What's more likely is that the column width is reduced to the width of the viewport. Have you tried setting the column width explicitly, along with AUTO_RESIZE_OFF?

To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bruke.

Now after i included code to set the width the scrollbar is shown. My question now is - is there a way the JScrollPane automatically put a Horizontal scroll bar till the length of the JTable column content.

Added code -


After adding above, the scrollbar is shown till the width of 1000. But if the column content (String) is more than 1000, three dots (...) are shown at the end.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I even tried reading the pixel length like below but it also cut off the String after some size.


I was able to see the ScrollableViewPortSize change in the console like below -

java.awt.Dimension[width=450,height=400]
java.awt.Dimension[width=1427,height=60
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Now after i included code to set the width the scrollbar is shown. My question now is - is there a way the JScrollPane automatically put a Horizontal scroll bar till the length of the JTable column content.

Did you not read Darryl's post? He mentioned something else apart from setting the column width.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad... here is a simple code that compiles. you can see the ... at the end when you scroll the JTable.

Class 1 - MainUI.java


Class 2 -> TopicsTableModel


 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a problem (after turning off the look&feel, I don't have that installed). Your table model has one column. Your table has a maximum of 500 pixels for that column. That's not enough to display the long value. You can calculate that width though:
This is still not good enough - the max width is just a few pixels short; this probably has something to do with the padding. You can again overcome this:
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad... here is a simple code that compiles.

Did you even take the time to go through the SSCCE web page? The code you posted is full of irrelevant stuff that detracts from the problem at hand.

Your table has a maximum of 500 pixels for that column. That's not enough to display the long value. You can calculate that width though:

Or use Rob Camick's Table Column Adjuster. The three lines of sample code on the linked page are all that's needed.

the max width is just a few pixels short; this probably has something to do with the padding.

And the intercell spacing, which still isn't accounted for.

Here's a true SSCCE:
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob and Darryl.
Issue got fixed by modifying code to add additional padding width. will try to post better SSCCE code next time....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic