Solved

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

Posted on 2004-08-04
11
208 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 42
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 57
printf performancy 11 69
Eclipse for Java EE development 2 27
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
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.

856 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