Results 1 to 8 of 8
  1. #1
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,575
    Rep Power
    25

    Default

    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.

  3. #3
    udhayageetha is offline Member
    Join Date
    Jul 2008
    Location
    Bangalore, India
    Posts
    11
    Rep Power
    0

    Default

    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);
    }
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,575
    Rep Power
    25

    Default

    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.

  5. #5
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    @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();

  6. #6
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    @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.

  7. #7
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    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.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,575
    Rep Power
    25

    Default

    The following shows a frame with a multicolored square in the upper left corner. My changes are marked by //<<<<<<<<
    Java 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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •