Results 1 to 4 of 4
  1. #1
    noviceJava is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default setMnemonic for for options

    I'm trying to see if there is an easier way to use the setMnemonic for different menu selections the performing the follwoing lines of code


    /* Bind the keyboard key combination Alt+V+Y to "Font Style" */
    fontStyleMenu.setMnemonic(KeyEvent.VK_Y);
    /* Underline the character 'Y' */
    fontStyleMenu.setDisplayedMnemonicIndex(7);

    /* Bind the keyboard key combination Alt+V+Y to "Font Style" */
    fontStyleMenu.setMnemonic(KeyEvent.VK_F);
    /* Underline the character 'Y' */
    fontStyleMenu.setDisplayedMnemonicIndex(1);

    I have three different key words
    bold
    medium
    demi

    and I would like to underline the first char of each but I was infomred instead of using the above code I can do the above in fewer lines of code, any help

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: setMnemonic for for options

    I would have to research this by reading the documentation or doing a web search. But first, your code doesn't match your comments. And second, why not have the person who informed you show you how to do it?

    Also, you are not being specific about what short cut you are talking about. If you have quite a few menu items you could use the KeyEvent as a key to a hash map and the index as the value. Then just iterate over the hash map setting the values for the menu.

    Regards,
    Jim
    Last edited by jim829; 01-16-2015 at 05:19 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: setMnemonic for for options

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: setMnemonic for for options

    You don't have to set the displayedMnemonicIndex. The menuitem will figure that out itself. Here's a little example to show you how easy it is:

    Java Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    
    public class MnemonicsTest {
    
        public static void main(String[] args) {
            JFrame f = new JFrame("Mnemonic test");
            f.setSize(400, 400);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setupMenu(f);
            f.setVisible(true);
        }
    
        private static void setupMenu(final JFrame f) {
            JMenuBar bar= new JMenuBar();
    
            JMenu file = new JMenu("File");
            file.setMnemonic('f');
    
            JMenuItem noo = new JMenuItem(new AbstractAction("New") {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(f, "JMenuItem \"New\" fired!");
                }
            });
            noo.setMnemonic('n');
            noo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
    
            bar.add(file);
            file.add(noo);
    
            f.setJMenuBar(bar);
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Portfolio options
    By adjit in forum New To Java
    Replies: 9
    Last Post: 01-05-2012, 03:36 PM
  2. JPanel Options Help
    By hego555 in forum AWT / Swing
    Replies: 2
    Last Post: 06-05-2011, 11:15 AM
  3. problem in setMnemonic for JMenu in JApplet
    By doron70 in forum AWT / Swing
    Replies: 2
    Last Post: 05-24-2011, 09:40 AM
  4. setMnemonic
    By newbie123 in forum Java Software
    Replies: 1
    Last Post: 04-20-2010, 05:05 PM
  5. VM options + applet
    By ProgrammingPup in forum Java Applets
    Replies: 1
    Last Post: 12-24-2009, 05:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •