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

3

5

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.

Riddance answered 9/7, 2013 at 13:37 Comment(1)
Never used BoxLayout, though AFAIU, the former (LINE_AXIS), can be changed depending on the Component's Orientation, if you change the Orientation of the Component to Right to Left, this will change too, though the latter will always add components from Left to Right, no metter what...Ilse
E
6

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.

Euterpe answered 9/7, 2013 at 13:44 Comment(0)
I
4

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

Ilse answered 10/7, 2013 at 6:4 Comment(0)
B
2

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

Busby answered 9/7, 2013 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.