| Author |
Why can't I display a JPanel?
|
Fernando Sanz
Ranch Hand
Joined: Jun 27, 2003
Posts: 101
|
|
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
Joined: Oct 29, 2001
Posts: 8263
|
|
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.
|
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
|
 |
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
|
|
|
Notice that MyGUI is not a constructor because constructors do not have return type (void).
|
SCJP2. Please Indent your code using UBB Code
|
 |
Fernando Sanz
Ranch Hand
Joined: Jun 27, 2003
Posts: 101
|
|
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
Joined: Sep 03, 2003
Posts: 399
|
|
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
Joined: Jun 27, 2003
Posts: 101
|
|
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
Joined: Jun 27, 2003
Posts: 101
|
|
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
Joined: Oct 29, 2001
Posts: 8263
|
|
|
java.awt.CardLayout
|
 |
Fernando Sanz
Ranch Hand
Joined: Jun 27, 2003
Posts: 101
|
|
I tried, but I'm not able to make my JPanel ocupy all the available space for the card.
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8263
|
|
|
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.
|
 |
 |
|
|
subject: Why can't I display a JPanel?
|
|
|