Searching...
Sunday, June 1, 2014

BoxLayout

June 01, 2014
The Box Layout is a jack of all trades. You can stack your widgets on top of each other...Along with the alignment, there are three techniques to assist you in placement of widgets in a BoxLayout. 1) You can place a rigid area to force a space between widgets. Obviously, the space will stay the same no matter how the window is resized. 2) You can also use 'glue' to place a stretchy space between the widgets. These spaces will expand and retract in relation to the size of the window. 3) You can create a special rigid area called a Filler to keep the GUI from becoming too small without re-sizing all your widgets. To give an example of each of these and how to set up a BoxLayout, we'll go back to our JPanel example of the coloured panels. We've taken out the titles and added a few additional panels to let you see all the different ways of spacing the panels. Bear in mind, this is all done on the horizontal but the same can be done on the vertical too. BoxLayout.java
package java9r.blogspot.com;

import javax.swing.*;
import org.omg.CORBA.FREE_MEM;
import java.awt.*;

public class BoxLayout{

    public JPanel createContentPane (){

        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();
       
        // We set the layout of the main JPanel to be BoxLayout.
        // LINE_AXIS sets them left to right, PAGE_AXIS sets them
        // from top to bottom.
     //   totalGUI.setLayout(new BoxLayout(totalGUI, BoxLayout.LINE_AXIS));

        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.red);
        redPanel.setMinimumSize(new Dimension(50, 50));
        redPanel.setPreferredSize(new Dimension(50, 50));
        totalGUI.add(redPanel);
       
        // This is the first spacer. This creates a spacer 10px wide that
        // will never get bigger or smaller.
        totalGUI.add(Box.createRigidArea(new Dimension(10,0)));

        JPanel yellowPanel = new JPanel();
        yellowPanel.setBackground(Color.yellow);
        yellowPanel.setPreferredSize(new Dimension(50, 50));
        totalGUI.add(yellowPanel);

        // This spacer takes any spare space and places it as part of the spacer
        // If you drag the window wider, the space will get wider.
        totalGUI.add(Box.createHorizontalGlue());

        JPanel greenPanel = new JPanel();
        greenPanel.setBackground(Color.green);
        greenPanel.setPreferredSize(new Dimension(50, 50));
        totalGUI.add(greenPanel);
       
        // This spacer is a custom spacer.
        // The minimum size acts like a rigid area that
        // will not get any smaller than 10 pixels on the x-axis (horizontal)
        // and not get any smaller than 50 pixels on the y axis (vertical).
        // The way the maximum size is set up means the spacer acts like glue
        // and will expand to fit the available space.
       
        Dimension minSize = new Dimension(10, 50);
        Dimension prefSize = new Dimension(10, 50);
        Dimension maxSize = new Dimension(Short.MAX_VALUE, 50);
        totalGUI.add(new Box.Filler(minSize, prefSize, maxSize));

        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        bluePanel.setPreferredSize(new Dimension(50, 50));
        totalGUI.add(bluePanel);

        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("-------- BoxLayout Demonstration!----------");

        //Create and set up the content pane.
        BoxLayout demo = new BoxLayout();
        frame.setContentPane(demo.createContentPane());
       
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(350, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Output
BoxLayout_Extended  BoxLayout_Extended.java
package java9r.blogspot.com;

import javax.swing.*;
import java.awt.*;
import java.awt.Color;

public class BoxLayout_Extended{

    public JPanel createContentPane (){

        // We create a bottom JPanel to place everything on.
        // We set the layout of the main JPanel to be BoxLayout.
        // LINE_AXIS sets them left to right, PAGE_AXIS sets them
        // from top to bottom.
        JPanel totalGUI = new JPanel();
       // totalGUI.setLayout(new BoxLayout(totalGUI, BoxLayout.LINE_AXIS));

        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.red);
        redPanel.setMinimumSize(new Dimension(40, 40));
        redPanel.setMaximumSize(new Dimension(40, 40));
        redPanel.setPreferredSize(new Dimension(40, 40));
        totalGUI.add(redPanel);

        // This is the first spacer. This creates a spacer 10px wide that
        // will never get bigger or smaller.
        totalGUI.add(Box.createRigidArea(new Dimension(10,0)));

        JPanel yellowPanel = new JPanel();
        yellowPanel.setBackground(Color.yellow);
        yellowPanel.setMinimumSize(new Dimension(60, 60));
        yellowPanel.setMaximumSize(new Dimension(60, 60));
        yellowPanel.setPreferredSize(new Dimension(60, 60));
        totalGUI.add(yellowPanel);

        // This spacer takes any spare space and places it as part of the spacer
        // If you drag the window wider, the space will get wider.
        totalGUI.add(Box.createHorizontalGlue());

        JPanel greenPanel = new JPanel();
        greenPanel.setBackground(Color.green);
        greenPanel.setMinimumSize(new Dimension(80, 80));
        greenPanel.setMaximumSize(new Dimension(80, 80));
        greenPanel.setPreferredSize(new Dimension(80, 80));
        totalGUI.add(greenPanel);

        // This spacer is a custom spacer.
        // The minimum size acts like a rigid area that
        // will not get any smaller than 10 pixels on the x-axis (horizontal)
        // and not get any smaller than 50 pixels on the y axis (vertical).
        // The way the maximum size is set up means the spacer acts like glue
        // and will expand to fit the available space.

        Dimension minSize = new Dimension(10, 50);
        Dimension prefSize = new Dimension(10, 50);
        Dimension maxSize = new Dimension(Short.MAX_VALUE, 50);
        totalGUI.add(new Box.Filler(minSize, prefSize, maxSize));

        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        bluePanel.setMinimumSize(new Dimension(100, 100));
        bluePanel.setMaximumSize(new Dimension(100, 100));
        bluePanel.setPreferredSize(new Dimension(100, 100));
        totalGUI.add(bluePanel);
       
        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("---------BoxLayout Exercises ----------");

        BoxLayout_Extended demo = new BoxLayout_Extended();
        frame.setContentPane(demo.createContentPane());
       
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(350, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Output 

0 comments:

Post a Comment

ads2