Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

How can I turn on/off automatic visualization of mnemonics in Swing?

When I use default L&F the mnemonics are shown directly. When I use Windows L&F the mnemonics go visible only if I press "Alt" key. Is it possible to control this feature (turn on the behaviour of Windows L&F for default L&F)?

Here is the code to reproduce (probably works only for Windows)

import java.util.Locale;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class OptionPaneTest {

    public static void main(String[] args) throws Exception {
        Locale.setDefault(Locale.ENGLISH);
        JOptionPane.showConfirmDialog(null, "Test Message", "Test title", JOptionPane.YES_NO_CANCEL_OPTION);
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // don't know whether it works for Mac
        JOptionPane.showConfirmDialog(null, "Test Message", "Test title", JOptionPane.YES_NO_CANCEL_OPTION);

    }

}

Here ist the image for default L&F (mnemonics are visible by default)

enter image description here

Here is the image for Win L&F (mnemonics are invisible by default)

enter image description here

But they go visible when I press "Alt" key

enter image description here

Upvotes: 3

Views: 161

Answers (1)

aterai
aterai

Reputation: 9833

Not sure if it works on all Windows but you could try UIManager.put("Button.showMnemonics", true);:

import java.awt.EventQueue;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class OptionPaneTest2 {
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    try {
      Locale.setDefault(Locale.ENGLISH);
      JOptionPane.showConfirmDialog(
          null, "Message", "title", JOptionPane.YES_NO_CANCEL_OPTION);

      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      JOptionPane.showConfirmDialog(
          null, "Message", "title", JOptionPane.YES_NO_CANCEL_OPTION);

      UIManager.put("Button.showMnemonics", true);
      JOptionPane.showConfirmDialog(
          null, "Message", "title", JOptionPane.YES_NO_CANCEL_OPTION);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Upvotes: 2

Related Questions