Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes JTable not occupying full scroll pane size - Scrollbar issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable not occupying full scroll pane size - Scrollbar issue" Watch "JTable not occupying full scroll pane size - Scrollbar issue" New topic
Author

JTable not occupying full scroll pane size - Scrollbar issue

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: May 03, 2008
Posts: 4642
    
    5

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.

luck, db
There are no new questions, but there may be new answers.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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

Joined: May 03, 2008
Posts: 4642
    
    5

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
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks Rob and Darryl.
Issue got fixed by modifying code to add additional padding width. will try to post better SSCCE code next time....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable not occupying full scroll pane size - Scrollbar issue