Results 1 to 3 of 3

Thread: Swing layouts

  1. #1
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default Swing layouts

    Hey yall,

    Been trying to understand layouts for a while now,started with GridbagLayout and the only way I could get what I want is through sheer luck.
    Now Im making a gui for a chat application Im working on and using BorderLayout to place components onto a frame and I can't seem to get what I want.
    Can someone help me out please.

    this is the panel Im adding onto the frame
    Java Code:
    public void createConfig(){
    
            configPanel = new JPanel();
            portNumber = new JTextField(5);
            friend = new JTextField(5);
            ipAddresText = new JTextField(5);
            connect = new JButton("Connect");
    
            JLabel portNumberLabel = new JLabel("Enter port number");
            JLabel friendLabel = new JLabel("Enter name");
            JLabel ipAddress = new JLabel("Enter ip address");
            JLabel onlineFriends = new JLabel("Friends");
    
    
            configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.Y_AXIS));
            configPanel.setSize(500,500);
            configPanel.setBackground(Color.LIGHT_GRAY);
    
    
    
            //configPanel.add(portNumberLabel);
            //configPanel.add(portNumber);
    
            //configPanel.add(friendLabel);
            //configPanel.add(friend);
    
            //configPanel.add(ipAddress);
            //configPanel.add(ipAddresText);
    
            Dimension dimension = new Dimension(50,50);
            friendsList.setPreferredSize(dimension);
    
    
            configPanel.add(onlineFriends);
            configPanel.add(friendsList);
    
        }
    this is the frame Im adding the panel onto

    Java Code:
    public void addToFrame() {
    
            createConfig();
            Container guiFrame = this.getContentPane();
            guiFrame.setLayout(new BorderLayout());
    
            guiFrame.add("Center", new Client(loginame));
    
            guiFrame.add("North", new JLabel("welcome "+loginame));
    
            guiFrame.add("East",configPanel);
    
    
            this.setSize(300,300);
            this.setVisible(true);
    
        }
    The client constructor creates an ok chat window but its the panel (createConfig()) that Im worried about.

    Thanx in advance...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Have you gone through the tutorial?
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Also
    -- a contentPane has a default BorderLayout.
    -- the add(...) overload you're using is obsolete as of version 1.1. Get with the times!
    Container (Java Platform SE 6)
    -- Don't use setSize(...) and avoid using setPreferredSize(...) in client code. Let the layout managers do their job.

    db

  3. #3
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

Similar Threads

  1. Layouts and Sizes
    By Bagzli in forum New To Java
    Replies: 2
    Last Post: 07-01-2011, 05:24 PM
  2. Java Panels and Layouts help
    By Zookey in forum New To Java
    Replies: 2
    Last Post: 01-30-2011, 12:06 PM
  3. problems with layouts !
    By ron2794 in forum NetBeans
    Replies: 1
    Last Post: 12-15-2010, 01:23 AM
  4. Can you have multiple layouts using swing?
    By fresh83 in forum New To Java
    Replies: 2
    Last Post: 12-27-2009, 09:38 PM
  5. Layouts ?
    By plodos in forum Eclipse
    Replies: 3
    Last Post: 07-30-2007, 03:03 PM

Posting Permissions

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