Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between x_axis and line_axis of boxlayout in java?

Both x_axis and line_axis arranges components from left to right. Then what's the difference between them?

This question is from Java Swing boxlayout layout manager.

like image 475
Vijay Kr Vishwakarma Avatar asked Jul 09 '13 13:07

Vijay Kr Vishwakarma


People also ask

How do you specify the orientation of a BoxLayout?

You simply substitute X for Y, height for width, and so on. Version note: Before JDK version 1.4, no constants existed for specifying the box layout's axis in a localizable way. Instead, you specified X_AXIS (left to right) or Y_AXIS (top to bottom) when creating the BoxLayout .

What is group layout in Java?

GroupLayout is a LayoutManager that hierarchically groups components in order to position them in a Container . GroupLayout is intended for use by builders, but may be hand-coded as well. Grouping is done by instances of the Group class. GroupLayout supports two types of groups.

What is JPanel Swing Java?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.

How do you create a box in Java?

Box box = new Box(BoxLayout. X_AXIS); box. add(button1); box.


3 Answers

From the official documentation:

X_AXIS - Components are laid out horizontally from left to right.

LINE_AXIS - Components are laid out the way words are laid out in a line, based on the container's ComponentOrientation property. If the container's ComponentOrientation is horizontal then components are laid out horizontally, otherwise they are laid out vertically. For horizontal orientations, if the container's ComponentOrientation is left to right then components are laid out left to right, otherwise they are laid out right to left. For vertical orientations components are always laid out from top to bottom.

like image 154
Daniel H. Avatar answered Oct 31 '22 17:10

Daniel H.


I hope the following code example, might be able to provide more insight into what the Java Docs has to say on BoxLayout.LINE_AXIS :


LINE_AXIS - Components are laid out the way words are laid out in a line, based on the container's ComponentOrientation property. If the container's ComponentOrientation is horizontal then components are laid out horizontally, otherwise they are laid out vertically. For horizontal orientations, if the container's ComponentOrientation is left to right then components are laid out left to right, otherwise they are laid out right to left. For vertical orientations components are always laid out from top to bottom.


X_AXIS - Components are laid out horizontally from left to right.


Do watch the last two rows, how Buttons are added to the second last JPanel from RIGHT tO LEFT and from __LEFT to RIGHT_ to the last JPanel

import java.awt.*;
import java.util.Locale;
import javax.swing.*;

public class BoxLayoutExample
{
    private JPanel topPanel;
    private JPanel middlePanel;
    private JPanel bottomPanel;
    private JPanel addedBottomPanel;
    private JButton[] button;

    public BoxLayoutExample()
    {
        button = new JButton[12];
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("Box Layout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(4, 1, 5, 5));

        topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(
                            topPanel, BoxLayout.X_AXIS));
        for (int i = 0; i < 3; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            topPanel.add(button[i]);
        }

        middlePanel = new JPanel();
        middlePanel.setLayout(new BoxLayout(
                            middlePanel, BoxLayout.LINE_AXIS));
        for (int i = 3; i < 6; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            middlePanel.add(button[i]);
        }

        bottomPanel = new JPanel();     
        bottomPanel.setComponentOrientation(
                        ComponentOrientation.RIGHT_TO_LEFT);        
        bottomPanel.setLayout(new BoxLayout(
                            bottomPanel, BoxLayout.LINE_AXIS));
        for (int i = 6; i < 9; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            bottomPanel.add(button[i]);
        }

        addedBottomPanel = new JPanel();        
        addedBottomPanel.setComponentOrientation(
                        ComponentOrientation.RIGHT_TO_LEFT);        
        addedBottomPanel.setLayout(new BoxLayout(
                            addedBottomPanel, BoxLayout.X_AXIS));
        for (int i = 9; i < 12; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            addedBottomPanel.add(button[i]);
        }

        contentPane.add(topPanel);
        contentPane.add(middlePanel);
        contentPane.add(bottomPanel);
        contentPane.add(addedBottomPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                new BoxLayoutExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

OUTPUT :

BoxLayoutExample

like image 41
nIcE cOw Avatar answered Oct 31 '22 17:10

nIcE cOw


X_AXIS is always horizontal. LINE_AXIS can be both based on the container's ComponentOrientation property.

Source:

https://docs.oracle.com/en/java/javase/15/docs/api/java.desktop/javax/swing/BoxLayout.html

like image 45
But I'm Not A Wrapper Class Avatar answered Oct 31 '22 18:10

But I'm Not A Wrapper Class