Link to home
Start Free TrialLog in
Avatar of JK2429
JK2429

asked on

How do I add JPanel to an existing panel? -Swing

|-----------------------------------------------------------|
|                                                                          |
|                           JPanel1                                    |
|                                                                          |
|-----------------------------------------------------------|
|                                                                          |
|                           JPanel2                                    |
|-----------------------------------------------------------|
|                           JPanel3                                    |
|-----------------------------------------------------------|

I have three JPanels, JPnel2 consists of a JLabel and two radio buttons.  Id like to add it to JPanel1.
How can this be done?
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

jPanel1.setLayout( new BorderLayout() ) ;
jPanel1.add( jPanel2, BorderLayout.CENTER ) ;
jPanel1.validate() ;
What's wrong with the add () method?
Sorry, Tim, didn't refresh.
Avatar of leeprovoost
leeprovoost

if jpanel1 is something with generic components, i mean that you have elements that you use for serveral other panels then you can extend the jpanel1 with jpanel2

i do the following:

1. create a base jpanel with all the layout: header, menu stuff, company logo, etc etc

class jpanel1 extends jpanel


2. create another jpanel with specific data, let's say an admin form, a user profile form, etc

class jpanel2 extends jpanel1


so jpanel2 gets all the elemetns of jpanel1

very easy to use and worth considering when you work with large programs that have a consistent layout

grtz

lee
Avatar of JK2429

ASKER

leeprovoost,
This is what I am doing.  I have so far JPanel2 extends JPanel1.

In JPanel2, I have a JLabel and 2 JRadioButtons. I need to add them to JPanel1.
I have the following code so far:

public void buildPanel()
{
 setLayout(new BorderLayout());
 JPanel panel = buildPanel1();
 JPanel srPanel = buildPanel1a();
 add(buildPanel2(), BorderLayout.NORTH);

 JPanel wPanel = new JPanel(new BorderLayout());
 wPnl.add(BuildPanel2(), BorderLayout.CENTER);
 wPnl.add(panel, BorderLayout.SOUTH);
 add(srPanel, BorderLayout.NORTH);
 add(buildPanel1a(), BorderLayout.NORTH);
 add(wPanel, BorderLayout.CENTER);
 setControlNamesForRobot();
}

Right now, it comes in the picture shown in the original question.
I need it to look something like this:


|-----------------------------------------------------------|
|                                                                          |
|                           JPanel1                                    |
|                                                                          |
|                                                                          |
|                                                    ------------------|
|                                                    |     JPanel2    |
|-----------------------------------------------------------|
|                           JPanel3                                    |
|-----------------------------------------------------------|

Avatar of JK2429

ASKER

Actually I went about this a bit differently.  I inherited Jpanel1 in the JPanel2 class.
Afterwards just put in the code for textboxes and all radiobuttons into that inherited function.
Avatar of JK2429

ASKER

Solution:
public void buildPanel()
{
super.buildpanel();

 setLayout(new BorderLayout());

 JPanel wPanel = new JPanel(new BorderLayout());
 wPnl.add(BuildPanel2(), BorderLayout.SOUTHEAST);

 setControlNamesForRobot();
}
>> JPanel wPanel = new JPanel(new BorderLayout());
>> wPnl.add(BuildPanel2(), BorderLayout.SOUTHEAST);

Isn't that similar to what TimYates said?:

>> jPanel1.setLayout( new BorderLayout() ) ;
>> jPanel1.add( jPanel2, BorderLayout.CENTER ) ;
Avatar of JK2429

ASKER

I tried his code, and it didn't work, thats why I had to try an alternative.  This code was derived using inheritance from another code.  And placing the Jpanel NOT OVER IT, but actually as a part of it.
TimYates, if you believe that I am wrong,  I have no problem giving you points.
ASKER CERTIFIED SOLUTION
Avatar of RomMod
RomMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial