JToolbar Insert Left and Right
Asked Answered
R

2

5

I created a JToolbar component and added it to a Frame. The toolbar uses the BorderLayout.

I add three buttons to the toolbar and they show just fine except I want them to be added to the right side of the toolbar. Right align.

Then whenever I add other buttons to the toolbar, I want them to add to the left side.

How can I do this?

I did the following but what happens is that the buttons appear on top of eachother :S The three to the right are all on eachother and the two to the left are all on eachother..

public class Toolbar extends JToolBar {

    private JToggleButton Screenshot = null;
    private JToggleButton UserKeyInput = null;
    private JToggleButton UserMouseInput = null;
    private CardPanel cardPanel = null;

    public Toolbar() {
        setFloatable(false);
        setRollover(true);
        setLayout(new BorderLayout());

        //I want to add these three to the right side of my toolbar.. Right align them :l
        Screenshot = new JToggleButton(new ImageIcon());
        UserKeyInput = new JToggleButton(new ImageIcon());
        UserMouseInput = new JToggleButton(new ImageIcon());
        cardPanel = new CardPanel();

        add(Screenshot, BorderLayout.EAST);
        add(UserKeyInput, BorderLayout.EAST);
        add(UserMouseInput, BorderLayout.EAST);
        addListeners();
    }

    public void addButtonLeft() {        
        JButton Tab = new JButton("Game");
        Tab.setFocusable(false);
        Tab.setSize(50, 25);

        Tab.setActionCommand(String.valueOf(Global.getApplet().getCanvas().getClass().hashCode()));
        Tab.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardPanel.jumpTo(Integer.valueOf(e.getActionCommand()));
            }
        });

        add(Tab, BorderLayout.WEST);
    }
}
Roesch answered 26/4, 2013 at 6:38 Comment(1)
You code sample would be more useful if it were an SSCCE, then we could run it and immediately see your problem.Rosabella
S
17

They're on top of eachother because you're putting them all in the same two places - namely BorderLayout.EAST and BorderLayout.WEST.

You can achieve your desired effect without using a BorderLayout but instead using JToolBar's default layout.

 add(tab);
 // add other elements you want on the left side 

 add(Box.createHorizontalGlue());

 add(Screenshot);
 add(UserKeyInput);
 add(UserMouseInput);
 //everything added after you place the HorizontalGlue will appear on the right side

EDIT (based on your comment):

Create a new JPanel and add it to the toolbar before the glue:

 JPanel leftPanel = new JPanel();
 add(leftPanel);

 add(Box.createHorizontalGlue());

 add(Screenshot);
 add(UserKeyInput);
 add(UserMouseInput);

Then have your addButtonLeft() method add new buttons to the panel, rather than directly to the toolbar.

Schecter answered 26/4, 2013 at 6:48 Comment(3)
Hmm but what if I want to go from right to left instead of left to right. The reason I'm asking is because the three buttons above I wanted on the right then anything after that, on the left.Roesch
Why can't you just add them above where you create the glue?Schecter
Because addButtonLeft creates them dynamically.Roesch
G
1

Everyone having a similar problem can have a look at http://helpdesk.objects.com.au/java/right-align-component-in-a-jtoolbar. It provides a very simple example using horizontal glue which spares the necessity of changing the default layout.

These are the code lines copied from the above link:

JToolBar toolbar = new JToolBar();

// These buttons will be left aligned by default
toolbar.add(new JButton("Open"));
toolbar.add(new JButton("Save"));

// add some glue so subsequent items are pushed to the right
toolbar.add(Box.createHorizontalGlue());

// This Help button will be right aligned
toolbar.add(new JButton("Help"));
Gangue answered 20/7, 2017 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.