Forums Register Login

JTree resizing issue

+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
Post a SSCCE that demonstrates the problem.
+Pie Number of slices to send: Send
 

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 :/
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
You're welcome
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3835 times.
Similar Threads
JTree not updating
JTable In JTree
JTree
jTree with the TreeModel
How to add a JLabel in any leaf of a JTree
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:59:21.