How do I turn on / off automatic rendering of mnemonics in Swing?

When I use the default L&F, mnemonics are displayed directly. When I use Windows L&F, the mnemonics are only visible if I press the Alt key. Is it possible to control this feature (enable Windows default L&F L&F behavior)?

Here is the code to reproduce (probably only works 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 is the default L&F image (mnemonics are visible by default)

enter image description here

Here is a picture for Win L&F (default mimics are invisible)

enter image description here

But they become visible when I press the "Alt" key

enter image description here

+3


source to share


1 answer


Not sure if it works for everyone Windows

, but you can 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();
    }
  }
}

      

+2


source







All Articles