Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-07-2008, 10:04 PM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-08-2008, 12:56 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-08-2008, 01:21 PM
Member
 
Join Date: Jul 2008
Location: Bangalore, India
Posts: 11
udhayageetha is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-08-2008, 05:58 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-08-2008, 11:20 PM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
@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();
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-08-2008, 11:37 PM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
@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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-08-2008, 11:39 PM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-09-2008, 01:06 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
Norm is on a distinguished road
The following shows a frame with a multicolored square in the upper left corner. My changes are marked by //<<<<<<<<
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); } }
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding and removing panels dynamically kbyrne AWT / Swing 1 04-12-2008 09:28 PM
Problem in adding sound. shanky_sanks Java Applets 6 03-29-2008 09:37 PM
Buttons to show new panels Lehane_9 AWT / Swing 1 03-06-2008 05:22 PM
Working with Labels on Panels. vargihate AWT / Swing 2 01-04-2008 05:09 AM
Adding checkboxes on frame Java Tip Java Tips 0 12-21-2007 09:40 AM


All times are GMT +3. The time now is 03:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org