Solved

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

Posted on 2004-08-04
11
204 Views
Last Modified: 2013-11-23
|-----------------------------------------------------------|
|                                                                          |
|                           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?
0
Comment
Question by:JK2429
11 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11717221
jPanel1.setLayout( new BorderLayout() ) ;
jPanel1.add( jPanel2, BorderLayout.CENTER ) ;
jPanel1.validate() ;
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11717277
What's wrong with the add () method?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11717293
Sorry, Tim, didn't refresh.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 2

Expert Comment

by:leeprovoost
ID: 11717352
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
0
 
LVL 4

Author Comment

by:JK2429
ID: 11717773
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                                    |
|-----------------------------------------------------------|

0
 
LVL 4

Author Comment

by:JK2429
ID: 11751866
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.
0
 
LVL 4

Author Comment

by:JK2429
ID: 11774460
Solution:
public void buildPanel()
{
super.buildpanel();

 setLayout(new BorderLayout());

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

 setControlNamesForRobot();
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11780877
>> 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 ) ;
0
 
LVL 4

Author Comment

by:JK2429
ID: 11784833
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.
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 11797025
The question has been PAQ'd and the 75 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 80
reverse digits of a number using for loop 5 40
eclipse apache tomcat admin console 52 97
hibernate example for saving data 19 38
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question