Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Components to a JPanel not working correctly  RSS feed

 
Tim Anderson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to build a JFrame that contains a parent JPanel that will hold a JPanel used to display a message view, a vertical strut and a JPanel that holds VCR-like buttons to cycle through the messages.

My parent JPanel uses a BorderLayout and the Border is a TitledBorder which tells which product you are viewing (i.e., Message 1 of 5). I build the message JPanel, vertical strut and button JPanel and add them all in order to the parent JPanel which then gets added to the rootContentPane of the JFrame. All that appears is the parent JPanel's TitledBorder, the strut and the button JPanel. Using JSwat, I've been able to determine that the message JPanel has 0 for both its height and width after adding the message components to the JPanel.

I create the message JPanel with a BorderLayout and an OvalBorder as copied from Manning Press's Swing book (which works fine in other JFrames that I have built), then add other components as necessary to the individual messages (mostly items around the edges with a central display for the actual message). What I can't figure out is why the height and width of the message JPanel isn't growing as I add components.

I had previously used the same code to display a single message (minus the parent JPanel, strut and button JPanel) where I added the border panels (northPanel, eastPanel, southPanel and westPanel) created in createMsgPanel() directly to the contentPane and it worked perfectly, so I know that the code that adds the message works fine. Then, the requirements changed (go figure) and I had to display multiple messages from the same screen.

Here's what I've got:
 
Tim Anderson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out I should have used a BoxLayout instead of BorderLayout for the mainPanel.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!