3

I have a JMenuBar with the standard items and shortcuts. But I noticed that the shortcut description is left-aligned, which looks ugly. Is there a way to right-align it?

PS: "Umschalt" means shift. Is there a way to force it to say shift instead of Umschalt?

[UPDATE: Locale.setDefault(Locale.ENGLISH); fixes the problem, but a solution to only affect specific components would be better.. ]

PSPS: With UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); I have set the look and feel to be OS default. But now I would like to make some small adjustments to the look on top of the standard OS Look. For example I would like to make the JMenuBar black. The interwebs told me to use UIManager.put("tMenuBar.background", Color.BLACK); but it doesn't seem to do anything..

[UPDATE: It seems like this is not possible with Windows Look and feel :/]

enter image description here

Here the code:

    private JMenuBar tMenuBar;
    private JMenu mbEdit;
    private JMenuItem mCut, mCopy, mPaste, mDo, mUndo;

    tMenuBar = new JMenuBar();
    mbEdit = new JMenu("Edit");
    tMenuBar.add(mbEdit);

    // EDIT
    mUndo = new JMenuItem("Undo");
    mDo = new JMenuItem("Redo");
    mCut = new JMenuItem("Cut");
    mCut.setIcon(iCut);
    mCopy = new JMenuItem("Copy");
    mCopy.setIcon(iCopy);
    mPaste = new JMenuItem("Paste");
    mPaste.setIcon(iPaste);
    mbEdit.add(mUndo);
    mbEdit.add(mDo);
    mbEdit.addSeparator();
    mbEdit.add(mCut);
    mbEdit.add(mCopy);
    mbEdit.add(mPaste);

    // Undo
    mUndo.setAccelerator(KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    // Redo
    mDo.setAccelerator(KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_Z, ((Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | java.awt.event.InputEvent.SHIFT_MASK))));
    // Cut
    mCut.setAccelerator(KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    // Copy
    mCopy.setAccelerator(KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    // Paste
    mPaste.setAccelerator(KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

Already tried:

applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

enter image description here

Haeri
  • 621
  • 1
  • 12
  • 27
  • `but the main question about the alignment still remains..)` this is part of the UI for the JMenuItem. So you will likely need to change the UI layout yourself. – camickr Oct 25 '14 at 16:34
  • Yeah.. I have already started looking into the documentation of JMenuItem.. but I can't find "Layout" anywhere... But why would someone implement the shortcut display left-aligned anyway?? – Haeri Oct 25 '14 at 16:40

2 Answers2

1

I would like to make the JMenuBar black.

It should be (with the "t")

UIManager.put("MenuBar.background", Color.BLACK);

You need to set the UIManager properties "before" you create the component.

Also, the property may not be supported on all LAF's. Check out UIManager Defaults for more information and a list of the properties support by LAF.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • ok. I thought I had to use the object I had created.. But still no luck. I had already downloaded that tool and yes Windows LAF does support MenuBar.background. And putting it before creating the component doesn't seem to fix it. `UIManager.put("MenuBar.background", Color.BLACK); JMenuBar tMenuBar = new JMenuBar();` – Haeri Oct 25 '14 at 15:36
  • Interesting fact: If I remove the part where I set Look&Feel, the UIManager.put method actually work... (This is odd, because the UIManagerDefaults app says that "Windows L&F" should have a ManuBar.background method.... – Haeri Oct 25 '14 at 16:08
  • @Haeri, yes there can be inconsistencies. The UIManager may have the information, but the LAF can ignore it. – camickr Oct 25 '14 at 16:15
  • Ok. Thank you. Well this was anyway a PSPS question xD. – Haeri Oct 25 '14 at 16:22
1

Take a look at this post regarding the german words. I do realize that this was probably a comment rather than an answer but I'm still unable to do those due to the lack of reputation and I want to help.

Community
  • 1
  • 1
Iootu
  • 344
  • 1
  • 6
  • Oh thanks! That helped. But it changed the entire application to be in English rather than system default... Would be cool to be able to only change specific parts... But this is great too. (Would love to mark your answer as the final answer, but the main question about the alignment still remains..) – Haeri Oct 25 '14 at 16:05
  • I'm glad it helped, regarding the change of the whole application, you can try to use the setLocale() method on only the GUI objects but I'm not sure this works. – Iootu Oct 25 '14 at 16:11
  • Regarding the alignment, does this [link](http://stackoverflow.com/questions/13140964/how-i-can-change-jmenuitem-alignment-to-right-align) help? – Iootu Oct 25 '14 at 16:20
  • Hehe. I do infarct do some research before I post questions. And Yes. I have already tried that. It only affects the Label of the Item but not the Shortcut. It looks something like this: [....Open Ctrl+O] Instead of: [Open.... Ctrl+O] But +1 for the effort ;) – Haeri Oct 25 '14 at 16:28
  • Can you try this one mUndo.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); please? – Iootu Oct 25 '14 at 16:32
  • Please discard this last comment, I'm pretty sure that it's not what you are after. – Iootu Oct 25 '14 at 16:37
  • @lootu Yeah already tried that... looks "cool" though xD (see OP) – Haeri Oct 25 '14 at 16:43
  • @Haeri lol, believe it or not I didn't realized before I post that the shortcut and the description had switched their places. – Iootu Oct 25 '14 at 16:51