bulb
bulb

Reputation: 178

JToolbar, Stop it going horizontal

I have a JToolbar. I was wondering if there was way to stop it going horizontal. I would prefer it to only go vertical. I do want it floating.

I have tried making a property changed listener and changing the orientation back to vertical but no luck. Thanks

Upvotes: 1

Views: 858

Answers (3)

user4718768
user4718768

Reputation:

You need to add the line:

toolBar.setOrientation(javax.swing.SwingConstants.VERTICAL);

Code Example:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;


class VerticalJToolbar
{
    public static void main(String args[])
    {
        JFrame frame = new JFrame("VerticalJToolbarTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(480, 480);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);

        JPanel panelOrange = new JPanel();
        panelOrange.setBackground(Color.ORANGE);
        panelOrange.setPreferredSize(new Dimension(100, 100));

        JPanel panelBlue = new JPanel();
        panelBlue.setBackground(Color.BLUE);
        panelBlue.setPreferredSize(new Dimension(100, 100));

        JPanel panelGreen = new JPanel();
        panelGreen.setBackground(Color.GREEN);
        panelGreen.setPreferredSize(new Dimension(100, 100));

        JPanel panelRed = new JPanel();
        panelRed.setBackground(Color.RED);
        panelRed.setPreferredSize(new Dimension(100, 100));

        JMenu menu1 = new JMenu("Menu 1");
        JMenu menu2 = new JMenu("Menu 2");
        JMenu menu3 = new JMenu("Menu 3");
        JMenu menu4 = new JMenu("Menu 4");

        JToolBar toolBarHorizontal = new JToolBar();
        toolBarHorizontal.add(menu1);
        toolBarHorizontal.add(menu2);
        toolBarHorizontal.add(menu3);
        toolBarHorizontal.add(menu4);

        JToolBar toolBarVertical = new JToolBar();
        toolBarVertical.setOrientation(SwingConstants.VERTICAL);
        toolBarVertical.add(panelOrange);
        toolBarVertical.add(panelBlue);
        toolBarVertical.add(panelGreen);
        toolBarVertical.add(panelRed);

        JPanel panelCenter = new JPanel();
        panelCenter.setBackground(Color.WHITE);

        frame.add(toolBarHorizontal, java.awt.BorderLayout.NORTH);
        frame.add(toolBarVertical, java.awt.BorderLayout.WEST);
        frame.add(panelCenter, java.awt.BorderLayout.CENTER);

        frame.setVisible(true);
    }
}

Screen capture:

http://img11.hostingpics.net/pics/207806801.png

Upvotes: 2

Johnny
Johnny

Reputation: 1895

Have a look at this https://bugs.openjdk.java.net/browse/JDK-4203039, it has an example how you can filter where the JToolbar is allowed to dock.

Upvotes: 2

Frank
Frank

Reputation: 33

Just use an other layout than the default floating one. And use a vertical layout.

JToolbar toolbar= new JToolbar();
toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.Y_AXIS));

You can also use no layout at all

toolbar.setLayout(null);

But this will be horizontal by default.

Upvotes: 2

Related Questions