Search code examples
javaswingalignmentlayout-managerboxlayout

Gluing buttons to the far left of the screen in a vertical BoxLayout in Swing


I've been having some trouble finding documentation for a specific trouble. You see, I wish to create a vertically aligned group of buttons that is glued to the far left of the screen, as shown in this edited photo.

Edited screen to show desired result:

https://i.sstatic.net/Csvur.png

However, I've no clue of how to achieve this. The documentation for BoxLayout makes mention of X-axis alignment, and having tested this, it seems focused on aligning components to one another, not components to relative portions of the screen.

My code is here:

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.*;

public class TabbedPanes extends JFrame implements ActionListener  
{
    JTabbedPane tabs;
    JButton but1, but2, but3, but4, but5, but6, but7, but8, but9, but10, but11, but12;
    String string1, string2; 
    JPanel pan1, pan2, pan3, pan4, pan5, pan6, pan7, pan8;
    JFrame frame;
    JScrollPane scroll;

    public void Tabs()
    {
        Box theBox = Box.createVerticalBox();

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        but1 = new JButton("" + string1);
            but1.addActionListener(this);
        but3 = new JButton("" + string1);
        but4 = new JButton("" + string1);
        but5 = new JButton("" + string1);
        but6 = new JButton("" + string1);
        but7 = new JButton("" + string1);
        but8 = new JButton("" + string1);
        but9 = new JButton("" + string1);
        but10 = new JButton("" + string1);
        but11 = new JButton("" + string1);
        but12 = new JButton("" + string1);

        pan1 = new JPanel();                                                                   //instantiate the panel
        pan1.add(theBox);                                                                      //add the layout manager to the panel
            theBox.add(but1);                                                                  //add components to the layout manager 
                but1.setAlignmentX(Component.LEFT_ALIGNMENT);      
            theBox.add(but3);
            theBox.add(but4);
            theBox.add(but5);
            theBox.add(but6);
            theBox.add(but7);
            theBox.add(but8);
            theBox.add(but9);
            theBox.add(but10);
            theBox.add(but11);
            theBox.add(but12);

        JScrollPane scroll = new JScrollPane(pan1);                                            //instantiate the JScrollPane with a parameter = the panel
            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        pan2 = new JPanel();
        but2 = new JButton("" + string2);
        but2.addActionListener(this);
        pan2.add(but2);

        pan3 = new JPanel();

        pan4 = new JPanel();

        pan5 = new JPanel();

        pan6 = new JPanel();

        pan7 = new JPanel();

        pan8 = new JPanel();

        tabs = new JTabbedPane();
        Container pane = frame.getContentPane();
        pane.add(tabs);
        tabs.add("Mon", scroll);
        tabs.add("Tue", pan2);
        tabs.add("Wed", pan3);
        tabs.add("Thu", pan4);
        tabs.add("Fri", pan5);
        tabs.add("Sat", pan6);
        tabs.add("Sun", pan7);
        tabs.add("Notes", pan8);

        frame.setSize(400,400);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == but1);
        {
            if(string1 == "Hello!")
            {
                string1 = "Goodbye.";
                but1.setText(string1);
            }

            else 
            {
                string1 = "Hello!";
                but1.setText(string1);
            }
        }

        if(e.getSource() == but2);
        {
            if(string2 == "Hello but in the other tab now!")
            {
                string2 = "I think you can see where this is going.";
                but2.setText(string2);
            }

            else 
            {
                string2 = "Hello but in the other tab now!";
                but2.setText(string2);
            }
        }
    }

    public static void main(String[] args)
    {
        TabbedPanes TrueTabs = new TabbedPanes();
        TrueTabs.Tabs();    
    }   
}

And it produces this screen:

What the code currently produces:

https://i.sstatic.net/SZfki.png

Any help would be greatly appreciated!


Solution

  • You are adding the Box to a panel which by default uses a FlowLayout with center alignment.

    So you could do:

    //pan1 = new JPanel();
    pan1 = new JPanel( new FlowLayout(FlowLayout.LEFT) );
    

    Or you don't even need the additional panel.

    Just add the Box to the scroll pane:

    //JScrollPane scroll = new JScrollPane(pan1);
    JScrollPane scroll = new JScrollPane(theBox);