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

JTree resizing issue

 
Paul McDowell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post a SSCCE that demonstrates the problem.
 
Paul McDowell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul McDowell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic