Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-04
11
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

730 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