Solved

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

Posted on 2004-08-04
11
199 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:mayankeagle
ID: 11717277
What's wrong with the add () method?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11717293
Sorry, Tim, didn't refresh.
0
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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:mayankeagle
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now