Cows42
Cows42

Reputation: 307

JMenuItem set alignment & maximumsize

I'm trying to make a help button on a JMenuBar. Currently I can make it align to right using this

helpItem.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

The issue is that it takes up the entire space of the JMenuBar so you can basically press anywhere on the empty space of the JMenuBar and it will press that button. I fixed that by overriding the size of the JMenuItem using this

    JMenuItem helpItem = new JMenuItem() {
        @Override
        public Dimension getMaximumSize() {
            Dimension d1 = super.getPreferredSize();
            Dimension d2 = super.getMaximumSize();
            d2.width = d1.width;
            return d2;
        }
    };

However, after I override getMaximumSize, setComponentOrientation doesn't align the JMenuItem to the right.

EDIT (Current code):

private void createMenuBar() {
        JMenuBar newMenuBar = new JMenuBar();
        newMenuBar.setName("");
        JMenu newMenu = new JMenu("Menu");
        JMenuItem updateItem = new JMenuItem("Update");
        JMenuItem aboutMe = new JMenuItem("About");
        JMenuItem exitItem = new JMenuItem("Exit");
        JMenuItem helpItem = new JMenuItem();
        URL iconPath = getClass().getResource("/help.png");
        helpItem.setIcon(new ImageIcon(iconPath));
        addMenuItemActionListeners(updateItem, aboutMe, exitItem, helpItem);
        newMenu.add(updateItem);
        newMenu.add(aboutMe);
        newMenu.add(exitItem);
        newMenuBar.add(newMenu);
        newMenuBar.add(Box.createHorizontalGlue());
        newMenuBar.add(helpItem);
        this.setJMenuBar(newMenuBar);
 }

Upvotes: 0

Views: 287

Answers (1)

Ansharja
Ansharja

Reputation: 1133

There is no need to manually set the component size, avoid it because it's a bad practice and could break your layout.

If you just want to align the help menu on the right, you can make use of Box.createHorizontalGlue method.

Yuo can add all the other menus you're gonna use (of course, if you need), then add the glue, and then add all the other menus you want to have aligned to the right side.

This is an example:

enter image description here

And this is a MCVE to achieve the above result:

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;
public class GlueMenuBarTest
{
    public static void main (String [] a) {
        SwingUtilities.invokeLater (new Runnable () {
            @Override public void run () {
                createAndShowGUI ();
            }
        });
    }
    private static void createAndShowGUI () {
        JFrame frame = new JFrame ("Test");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar (createGlueMenuBar ());
        frame.setSize (500, 250); // just for convenience, use pack () in a real app.
        frame.setLocationRelativeTo (null);
        frame.setVisible (true);
    }
    private static JMenuBar createGlueMenuBar () {
        JMenuBar menuBar = new JMenuBar ();
        menuBar.add (new JMenu ("File"));
        menuBar.add (new JMenu ("Edit"));
        menuBar.add (new JMenu ("Search"));
        menuBar.add (Box.createHorizontalGlue ());
        menuBar.add (new JMenu ("Help"));
        return menuBar;
    }
}

If you comment the lines where i add other menus on the left, it will work as well.

Upvotes: 1

Related Questions