aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTree resizing 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 "JTree resizing issue" Watch "JTree resizing issue" New topic
Author

JTree resizing issue

Paul McDowell
Greenhorn

Joined: Nov 01, 2010
Posts: 4
Hello,

I have come to this site for a few years looking for various issues and 99% of the time find my answer here (thank you by the way!) This time however I think I've programmed myself into a corner requiring a bit more of a specialized answer. I am writing an XMPP Client using the smack API for cross platform functionality, and I needed something I could use for displaying the roster easily for users. The JTree seemed like a natural fit for this (rosters are a collection of collections containing user objects, so just a two tier tree with leafs in each sub root). The problem I'm running into is that the tree doesn't resize itself upon being painted into the JPanel that contains it. I have to actually resize the window just a little to get it to expand to its own content size, and even then it never expands beyond the size of its content. I would like it to behave a bit more like a standard UI for a chat client (being resized to slightly smaller than the containing frame at all times).

Fair warning, while I would enjoy being a professional programmer I'm not a professional programmer by any means. I enjoy it, and its a hobby / something I do when asked to do so for work, but this isn't exactly 'pro code' here so please be gentle ;)

EDIT: This is the SSCCE as requested.


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
Post a SSCCE that demonstrates the problem.
Paul McDowell
Greenhorn

Joined: Nov 01, 2010
Posts: 4
Rob Camick wrote:Post a SSCCE that demonstrates the problem.


I'll modify the original post to show it in a few minutes after I write one up.

Sorry for the great wall of text :/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

The JFrame uses a BorderLayout. The JFrame has one child component, a JPanel, at its center. That JPanel is stretched.
The JPanel uses a FlowLayout, with center alignment. This means that it will show all child components next to each other, with their preferred size.

If you get rid of that intermediate JPanel, the JScrollPane will be stretched over the entire JFrame.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul McDowell
Greenhorn

Joined: Nov 01, 2010
Posts: 4
Rob Prime wrote:The JFrame uses a BorderLayout. The JFrame has one child component, a JPanel, at its center. That JPanel is stretched.
The JPanel uses a FlowLayout, with center alignment. This means that it will show all child components next to each other, with their preferred size.

If you get rid of that intermediate JPanel, the JScrollPane will be stretched over the entire JFrame.


hmm, I see this seems to be an issue with my layout manager then. The actual program utilizes GridBagLayout for everything (JFrame and JPanel) which I left out of the SSCCE for simplicity (I didn't think about the layout manager being the issue). I have the same problem if I apply GridBagLayout to both containers as well.

The purpose of the JPanel is to allow me to add items below the JTree, like a banner for daily announcements or other things like that as well as to swap between screens easily (swapping between logged in and logged out states). Do you have any idea why GridBagConstraints is still centering the JScrollPane despite specifying the coordinates as 0,0?
Paul McDowell
Greenhorn

Joined: Nov 01, 2010
Posts: 4
Paul McDowell wrote:
Rob Prime wrote:The JFrame uses a BorderLayout. The JFrame has one child component, a JPanel, at its center. That JPanel is stretched.
The JPanel uses a FlowLayout, with center alignment. This means that it will show all child components next to each other, with their preferred size.

If you get rid of that intermediate JPanel, the JScrollPane will be stretched over the entire JFrame.


hmm, I see this seems to be an issue with my layout manager then. The actual program utilizes GridBagLayout for everything (JFrame and JPanel) which I left out of the SSCCE for simplicity (I didn't think about the layout manager being the issue). I have the same problem if I apply GridBagLayout to both containers as well.

The purpose of the JPanel is to allow me to add items below the JTree, like a banner for daily announcements or other things like that as well as to swap between screens easily (swapping between logged in and logged out states). Do you have any idea why GridBagConstraints is still centering the JScrollPane despite specifying the coordinates as 0,0?


I have a solution!

I was thinking about how you mentioned JFrame and JPanel defaulting to different layout managers, and I realized that instead of trying to force everything to have one layout manager for organizational purposes I could simply use two layouts instead. I can use GridBagLayout for the JPanel containing the login screen and BorderLayout on the JTree screen containing the username branches. This allows me to still get the look and feel from both JPanels without the stretching issue I found before.

The next trick is getting the rest of the interface cleaned up so that it doesn't look like a third grader wrote it, but my code is generally hodgepodge anyway so we'll see how that goes.

Thank you Rob for the help! I plan to be a bit more vocal to help others now that I've signed up.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTree resizing issue