Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't I display a JPanel?

 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I don't want to implement a JFrame as my GUI, so I thought I'd use a JPanel.
Since I was having problems with my GUI, I thought I should start again, with something very simple:
// I didn't post the import, but they're in my code

What am I missing so that it doesn't display my GUI at all?
Thanks!
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fernando Sanz:

What am I missing so that it doesn't display my GUI at all?

A top-level container like JWindow or JFrame which has a heavyweight peer that can be displayed on its own. JPanel is a lightweight container. It cannot be displayed alone.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that MyGUI is not a constructor because constructors do not have return type (void).
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, this time I'm trying with a JFrame. So I've created my own JPanel:

Which is used by:

But, the top JPanel doesn't display the buttons that it should.
What am I still doing wrong?
Thanks guys
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things. First, look at what Jose said. In the class "MyButtonPanel" your constructor has a return type of "void". Constructors DO NOT have return types:

Second, just before your call to "setSize()" you probably want to call "pack()". This lets the layout manager (BorderManager in your case) go actually place and size all of it's components.
If you call "pack()" before the the call to "setSize()", you'll get a full-screen frame. If you call "pack()" after the call to "setSize()", you'll get a small frame only large enough to fit the three buttons.
There is no need to call "setVisible()" in the "MyButtonPanel" class.
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wayne L Johnson:
A couple of things. First, look at what Jose said. In the class "MyButtonPanel" your constructor has a return type of "void". Constructors DO NOT have return types:

Second, just before your call to "setSize()" you probably want to call "pack()". This lets the layout manager (BorderManager in your case) go actually place and size all of it's components.
If you call "pack()" before the the call to "setSize()", you'll get a full-screen frame. If you call "pack()" after the call to "setSize()", you'll get a small frame only large enough to fit the three buttons.
There is no need to call "setVisible()" in the "MyButtonPanel" class.

Thanks a lot!
I haven't tried it yet, but it sounds like that's my problem
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
finally I've managed to get it almost working.
I have a container that "holds" everything. At the SOUTH I added my buttons, and at the CENTER I thought I'd add all my different JPanels. Having something like:
c.add(twoPanel, BorderLayout.CENTER) ;
c.add(onePanel, BorderLayout.CENTER) ;
Then, by clicking on the appropiate button, I'd use something like nameOfJPanel.setVisible(true).
But, it doesn't work. The container will only display the last element to be added to any certain location. I mean, it'll display on the CENTER the last panel to be added. In my example, it'll display only the "onePanel".
Any other solution/idea to how to display different panels in the same location?
Thanks,
Fernando
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
java.awt.CardLayout

I tried, but I'm not able to make my JPanel ocupy all the available space for the card.
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try browsing through the Swing Tutorial, especially the part about Layout Managers. You need to understand how components get laid out on a GUI and how the different layout managers interact.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic