Results 1 to 7 of 7
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default BorderLayout Sizes

    I am trying to set the sizes of a BoarderLayout so that there is a JTextArea along the top of the screen and a TextField and JButton on the bottom (Side by side). Just like you would see in an instant messenger application.

    I have tried settings the sizes by making a border layout and then changing the sizes of the components. What have I done wrong?
    Does the layout adjust to the size changes of the components or not? Do I need to change the sizes of the layout, not the components?

    Java Code:
    public class GUI extends JFrame{
    
        JTextField textInputField;
        JTextArea textOutputField;
        String textInput;
        String textOutput;
    
        public GUI(int width, int height){
    
            BorderLayout layout = new BorderLayout();
            
            textOutputField = new JTextArea();
            textInputField = new JTextField();
            JButton inputButton = new JButton();
    
            setLayout(layout);
            setVisible(true);
            setSize(width, height);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            add(textOutputField, BorderLayout.NORTH);
            add(textInputField, BorderLayout.WEST);
            add(inputButton, BorderLayout.EAST);
            
            textOutputField.setSize(width, height/2);
            textInputField.setSize(width/(4/5), height/2);
            inputButton.setSize(width/(1/5), height/2);
    
            textOutputField.setEditable(false);
            textOutputField.setBackground(Color.GRAY);
    
            textInputField.setBackground(Color.white);
        }

    It gives this:

    BorderLayout Sizes-screenshot-2013-02-01-12-42-53.png

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

    Default Re: BorderLayout Sizes

    Put the JTextArea in the CENTER; wrap the JTextField and JButton in another JPanel with any suitable layout (you have several options here) and add that to the SOUTH position.

    Or learn to use GridBagLayout, with which your requirements can very easily be met.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: BorderLayout Sizes

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: BorderLayout Sizes

    Is this right?
    Because it gives me a messed up layout again.

    Java Code:
        public GUI(int width, int height){
    
            BorderLayout layout = new BorderLayout();
            
            textOutputField = new JTextArea();
            textInputField = new JTextField();
            JButton inputButton = new JButton();
            JPanel inputPanel = new JPanel();
    
            setLayout(layout);
            setVisible(true);
            setSize(width, height);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            
            inputPanel.add(textInputField);
            inputPanel.add(inputButton);
            
            
            add(textOutputField, BorderLayout.CENTER);
            add(inputPanel, BorderLayout.SOUTH);
            
            textOutputField.setSize(width, height/2);
            textInputField.setSize(width/(4/5), height/2);
            inputButton.setSize(width/(1/5), height/2);
    
            textOutputField.setEditable(false);
            textOutputField.setBackground(Color.GRAY);
    
            textInputField.setBackground(Color.white);
        }

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: BorderLayout Sizes

    BorderLayout Sizes-screenshot-2013-02-01-13-58-11.png


    No matter what I do now, the JPanel at the bottom has that height and the JTextField has no width


    Note: Ignore the exception too, that's just part of the bigger program. It doesn't have anything to do with the layout (The exception occurs after the GUI)
    Last edited by kkid; 02-01-2013 at 03:01 PM.

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: BorderLayout Sizes

    Ah, just found the issue.

    it was simply becaase I had not set any character size for the JTextField and the Button had no text inside

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

    Default Re: BorderLayout Sizes

    You should never need setSize(...) and in fact the layout manager will ignore it.

    I would use a BorderLayout for the inner JPanel, with the JTextField at CENTER and the JButton at EAST.

    (Not really; I would use a GridBagLayout and wouldn't need an inner JPanel)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to use BorderLayout
    By Daman12 in forum New To Java
    Replies: 15
    Last Post: 10-27-2011, 05:30 AM
  2. Layouts and Sizes
    By Bagzli in forum New To Java
    Replies: 2
    Last Post: 07-01-2011, 05:24 PM
  3. Setting sizes to JButtons in grid layout
    By sarah jain in forum AWT / Swing
    Replies: 8
    Last Post: 02-22-2011, 04:08 AM
  4. Changing JPanel sizes
    By Basit in forum Java Applets
    Replies: 2
    Last Post: 08-12-2009, 06:48 PM
  5. Help with BorderLayout
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07:26 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
  •