Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By jashburn

Thread: BorderLayout: How to decrease the height of CENTER position?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default BorderLayout: How to decrease the height of CENTER position?

    Normally, the height of the CENTER component (or position) is greater than that of the NORTH and SOUTH components in a BorderLayout.

    Is it possible to increase the size of the SOUTH component and decrease the size of the CENTER position because the jcomponents I want to add in the SOUTH component are bigger? or shall I have to change the layout and use some other layout?

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: BorderLayout: How to decrease the height of CENTER position?

    It is certainly possible for the height of the NORTH and/or SOUTH components to be greater than the CENTER component. Quoting from How to Use BorderLayout (The Java Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container),
    If the window is enlarged, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space.
    In other words, space in the NORTH (or PAGE_START) and SOUTH (or PAGE_END) areas are determined by the size of its contents, while the space in the CENTER area is controlled by the size of the window.

    Here's an example to illustrate this:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    class BorderLayoutExample extends JFrame {
    
        private static final int STANDARD_WIDTH = 640;
        
        BorderLayoutExample() {
            setTitle("BorderLayout Example");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        void setUpGUI() {
            setLayout(new BorderLayout());
        
            JButton topButton = new JButton("PAGE_START (height:50)");
            topButton.setPreferredSize(new Dimension(STANDARD_WIDTH, 50));
            add(topButton, BorderLayout.PAGE_START);
        
            JButton middleButton = new JButton("CENTER (height:100)");
            middleButton.setPreferredSize(new Dimension(STANDARD_WIDTH, 100));
            add(middleButton, BorderLayout.CENTER);
        
            JButton bottomButton = new JButton("PAGE_SOUTH (height:200)");
            bottomButton.setPreferredSize(new Dimension(STANDARD_WIDTH, 200));
            add(bottomButton, BorderLayout.PAGE_END);
        }
        
        void display() {
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
        
                @Override
                public void run() {
                    BorderLayoutExample example = new BorderLayoutExample();
                    example.setUpGUI();
                    example.display();
                }
            });
        }
    }
    The key to the above is to set the components' preferred sizes, and when displaying the GUI, not set an explicit size for the window. Instead, use pack() so that the window is "sized to fit the preferred size and layouts of its subcomponents." (See Window (Java Platform SE 7 ))
    Zarah likes this.

Similar Threads

  1. Decrease array size without losing data
    By lorreTheGreat in forum New To Java
    Replies: 7
    Last Post: 02-27-2013, 03:24 PM
  2. Replies: 1
    Last Post: 08-16-2012, 03:51 PM
  3. Replies: 4
    Last Post: 07-03-2012, 02:40 PM
  4. Replies: 3
    Last Post: 01-16-2010, 07:14 PM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 07:22 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
  •