Results 1 to 8 of 8
- 07-07-2008, 09:04 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
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-07-2008, 11:56 PM #2
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, 12:21 PM #3
Member
- Join Date
- Jul 2008
- Location
- Bangalore, India
- Posts
- 11
- Rep Power
- 0
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, 04:58 PM #4
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, 10:20 PM #5
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
@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, 10:37 PM #6
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
@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, 10:39 PM #7
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
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, 12:06 AM #8
The following shows a frame with a multicolored square in the upper left corner. My changes are marked by //<<<<<<<<
If you don't add components to a container, you don't need to change its layout manager. No components were added to p1-p4Java Code: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); } }
Similar Threads
-
Adding and removing panels dynamically
By kbyrne in forum AWT / SwingReplies: 1Last Post: 04-12-2008, 08:28 PM -
Problem in adding sound.
By shanky_sanks in forum Java AppletsReplies: 6Last Post: 03-29-2008, 08:37 PM -
Buttons to show new panels
By Lehane_9 in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:22 PM -
Working with Labels on Panels.
By vargihate in forum AWT / SwingReplies: 2Last Post: 01-04-2008, 04:09 AM -
Adding checkboxes on frame
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks