|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-07-2008, 10:04 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Problem in adding Multiple Panels at the Specific positon on frame
I Want to add mulitple Panels on the Frame at the specific position. And Each panel should contain multiple components at the desired location in the panel( I use setLocation for this purpose). But this code do not solve my purpose. Its shows only JPanel p2(RED in color) and Buttons b1,b2.
Can Anyone tell me Where I am wrong in the code or suggest any other alternative to solve my purpose.
Here is the CODE:
import java.awt.*;
import javax.swing.border.*;
import javax.swing.*;
public class Class3 extends JFrame {
JButton b1,b2;
JPanel p,p2;
Border blackline;
//Container con=getContentPane();
public Class3(String s)
{
super(s);
setLayout(new BorderLayout());
// p=new JPanel();
//p2=new JPanel();
p=(JPanel)getContentPane();
p2=(JPanel)getContentPane();
p.setLayout(null);
p2.setLayout(null);
b1=new JButton("Ok");
b1.setLocation(350,650);
b1.setSize(80, 20);
p.add(b1);
p.setBackground(Color.GREEN);
blackline = BorderFactory.createLoweredBevelBorder();
p.setBorder(blackline);
p.setLocation(0,500);
p.setSize(650, 270);
// p.setBorder(blackline);
b2=new JButton("Ok");
b2.setLocation(850,650);
b2.setSize(80, 50);
p2.add(b2);
p2.setBackground(Color.RED);
p2.setLocation(650, 500);
p2.setSize(650, 270);
}
public static void main(String ar[])
{
Class3 c=new Class3("ADMIN");
c.setSize(1300, 770);
c.setLocation(0, 0);
c.setVisible(true);
}
}
|
|

07-08-2008, 12:56 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
|
|
|
p and p2 both refer to the same panel. So if you change what p points to and then change what p2 points to, you are changing the same panel first via p and then via p2.
Instead of using the contentPane, create new panels and add them to the contentPane. For some reason (if you wrote comments here to explain why would be good) you commented out getting new panels for p and p2???
Restore getting new panels, add them to the contentPane and use setLocation etc to position and color them.
|
|

07-08-2008, 01:21 PM
|
|
Member
|
|
Join Date: Jul 2008
Location: Bangalore, India
Posts: 11
|
|
|
try this coding...
like u can add so many panels & components with a single panel
import java.awt.*;
import javax.swing.border.*;
import javax.swing.*;
public class Class3 extends JFrame {
JPanel p,p1,p2,p3,p4;
Container contentPane;
public Class3(String s)
{
super(s);
setLayout(new BorderLayout());
p=new JPanel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
contentPane = getContentPane();
p.setLayout(null);
p1.setLayout(null);
p2.setLayout(null);
p3.setLayout(null);
p4.setLayout(null);
contentPane.add(p);
p.add(p1).setBounds(0,0,200,200);
p.add(p2).setBounds(200,200,200,200);
p.add(p3).setBounds(200,0,200,200);
p.add(p4).setBounds(0,200,200,200);
p1.setBackground(Color.GREEN);
p2.setBackground(Color.BLUE);
p3.setBackground(Color.CYAN);
p4.setBackground(Color.RED);
}
public static void main(String ar[])
{
Class3 c=new Class3("ADMIN");
c.setSize(1024, 700);
c.setVisible(true);
}
}
|
|

07-08-2008, 05:58 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
|
|
|
I don't think you have to setLayout(null) for panels p1-p4 as they are not being used as containers. p seems to be the only container being used that way.
|
|

07-08-2008, 11:20 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
@Norm,
initially I used new JPanel(), but the problem is setLocation funtion is not working for components Thats why I used getcontentPane. With new JPanel I can see only blank Frame. You can try this, Just make (JPanel)getContentPane() a comment and remove it from new JPanel();
|
|

07-08-2008, 11:37 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
@udhayageetha
Thanx My purpose is Solved.
@ Norm
Its necessary to make all panels Layout Null otherwise setLoction Function wont work properly.
And thanx to you too.
|
|

07-08-2008, 11:39 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Can you tell me From where I can get org.JDesktop.swingx.* library file(package) . Actually It is missing in JDK1.5, or tell me other version which have this package.
|
|

07-09-2008, 01:06 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
|
|
The following shows a frame with a multicolored square in the upper left corner. My changes are marked by //<<<<<<<<
import java.awt.*;
import javax.swing.border.*;
import javax.swing.*;
public class Class3 extends JFrame {
JPanel p,p1,p2,p3,p4;
Container contentPane;
public Class3(String s)
{
super(s);
getContentPane().setLayout(new BorderLayout()); //<<<<<<<
p=new JPanel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
contentPane = getContentPane();
p.setLayout(null);
//p1.setLayout(null); //<<<<<<<
//p2.setLayout(null); //<<<<<<<<<
//p3.setLayout(null); //<<<<<<<<
//p4.setLayout(null); //<<<<<<<<
contentPane.add(p);
p.add(p1); //<<<<<<<
p1.setBounds(0,0,200,200); //<<<<<<<< and same below
p.add(p2);
p2.setBounds(200,200,200,200);
p.add(p3);
p3.setBounds(200,0,200,200);
p.add(p4);
p4.setBounds(0,200,200,200);
p1.setBackground(Color.GREEN);
p2.setBackground(Color.BLUE);
p3.setBackground(Color.CYAN);
p4.setBackground(Color.RED);
}
public static void main(String ar[])
{
Class3 c=new Class3("ADMIN");
c.setSize(1024, 700);
c.setVisible(true);
}
}
If you don't add components to a container, you don't need to change its layout manager. No components were added to p1-p4
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|