Results 1 to 5 of 5

Thread: Keybinding

  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Keybinding

    Hi I have been trying to solve this for a day, and I may be overlooking things. I dont know why shortcut key does not work. It should print the selected list if I press INSERT key, or Exit/End the application if ESCAPE key is pressed but nothing happens.
    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CurSymbolForm extends JDialog
    {
            JButton btnCopy, btnClose;
            JPanel pnlForm, pnlButton;
            JList lstCurrencies;
            JScrollPane scrList;
    
            public CurSymbolForm()
            {   setModalityType(ModalityType.APPLICATION_MODAL);
    
                btnCopy = new JButton("<html><center><b> COPY <font color = rgb(255,0,0)><u>[ INSERT ]</u></font> </b></center></html>");
                btnCopy.addActionListener(new ActionListener()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCopyActionPerformed(e);
                    }
                });
    
                btnClose = new JButton("<html><center><b> CLOSE <font color = rgb(255,0,0)><u>[ ESC ]</u></font> </b></center></html>");
                btnClose.addActionListener(new ActionListener()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCloseActionPerformed(e);
                    }
                });
    
                pnlButton = new JPanel();
                pnlButton.setVisible(true);
                pnlButton.setLayout(new BorderLayout());
                pnlButton.add(btnCopy, BorderLayout.LINE_START);
                pnlButton.add(btnClose, BorderLayout.LINE_END);
    
                Object[] listValue = {"Hello", "Java", "World"};
                lstCurrencies = new JList(listValue);
                lstCurrencies.setVisible(true);
                lstCurrencies.setVisibleRowCount(20);
                lstCurrencies.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                lstCurrencies.setSelectedIndex(0);
                scrList = new JScrollPane(lstCurrencies);
    
                pnlForm = new JPanel();
                pnlForm.setVisible(true);
                pnlForm.setLayout(new BorderLayout());
                pnlForm.add(pnlButton, BorderLayout.PAGE_START);
                pnlForm.add(scrList, BorderLayout.PAGE_END);
    
                this.setSize(new Dimension(400, 470));
                this.setLayout(new BorderLayout());
                this.add(pnlForm, BorderLayout.CENTER);
    
                int intW = Toolkit.getDefaultToolkit().getScreenSize().width;
                int intH = Toolkit.getDefaultToolkit().getScreenSize().height;
                int intX = (intW - this.getWidth())/2;
                int intY = (intH - this.getHeight())/2;
                this.setLocation(intX, intY);
    
                this.setVisible(true);
    
                keyBind();
                pack();
            }
    
            private void btnCopyActionPerformed(ActionEvent ae)
            {   int selRow = -1;
                selRow = lstCurrencies.getSelectedIndex();
                if(selRow > -1)
                {   String selItem = lstCurrencies.getSelectedValue().toString();
                    String[] curCode = selItem.split(" - ");
                    System.out.println(curCode[0]);
                }
                this.dispose();
            }
            private void btnCloseActionPerformed(ActionEvent ae)
            {   this.dispose();
            }
    
            private void keyBind()
            {   ActionMap am = pnlButton.getActionMap();
                am.put("copy_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCopyActionPerformed(e);
                        System.out.println("COPY CLICKED!");
                    }
                });
                am.put("close_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCloseActionPerformed(e);
                        System.out.println("CLOSE CLICKED!");
                    }
                });
    
                InputMap im = pnlButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                im.put(KeyStroke.getKeyStroke("insert"), "copy_key");
                im.put(KeyStroke.getKeyStroke("escape"), "close_key");
            }
    
            public static void main(String[] args)
            {	CurSymbolForm csf = new CurSymbolForm();

    Thanks in advance :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Big problem: you're making your JDialog application modal, and setting the key binding AFTER calling setVisible. Don't do it this way but instead set the binding before you call a block on program flow via setVisible.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thanks for that, but still it does not do anything. I tried to add CTRL+F key to see if that will work. And CTRL+F works fine, only INSERT and ESCAPE button does not work.

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CurSymbolForm extends JDialog
    {
            JButton btnCopy, btnClose;
            JPanel pnlForm, pnlButton;
            JList lstCurrencies;
            JScrollPane scrList;
    
            public CurSymbolForm()
            {  
                setModalityType(ModalityType.APPLICATION_MODAL);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                
                btnCopy = new JButton("<html><center><b> COPY <font color = rgb(255,0,0)><u>[ INSERT ]</u></font> </b></center></html>");
                btnCopy.addActionListener(new ActionListener()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCopyActionPerformed(e);
                    }
                });
                
                btnClose = new JButton("<html><center><b> CLOSE <font color = rgb(255,0,0)><u>[ ESC ]</u></font> </b></center></html>");
                btnClose.addActionListener(new ActionListener()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCloseActionPerformed(e);
                    }
                });
    
                pnlButton = new JPanel();
                pnlButton.setVisible(true);
                pnlButton.setLayout(new BorderLayout());
                pnlButton.add(btnCopy, BorderLayout.LINE_START);
                pnlButton.add(btnClose, BorderLayout.LINE_END);
    
                Object[] listValue = {"Hello", "Java", "World"};
                lstCurrencies = new JList(listValue);
                lstCurrencies.setVisible(true);
                lstCurrencies.setVisibleRowCount(20);
                lstCurrencies.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                lstCurrencies.setSelectedIndex(0);
                scrList = new JScrollPane(lstCurrencies);
    
                pnlForm = new JPanel();
                pnlForm.setVisible(true);
                pnlForm.setLayout(new BorderLayout());
                pnlForm.add(pnlButton, BorderLayout.PAGE_START);
                pnlForm.add(scrList, BorderLayout.PAGE_END);
    
             [b]keyBind();[/b]//I did what you said
    
                this.setSize(new Dimension(400, 470));
                this.setLayout(new BorderLayout());
                this.add(pnlForm, BorderLayout.CENTER);
    
                int intW = Toolkit.getDefaultToolkit().getScreenSize().width;
                int intH = Toolkit.getDefaultToolkit().getScreenSize().height;
                int intX = (intW - this.getWidth())/2;
                int intY = (intH - this.getHeight())/2;
                this.setLocation(intX, intY);
    
             [b]this.setVisible(true);[/b]
                pack();
            }
    
            private void btnCopyActionPerformed(ActionEvent ae)
            {   int selRow = -1;
                selRow = lstCurrencies.getSelectedIndex();
                if(selRow > -1)
                {   String selItem = lstCurrencies.getSelectedValue().toString();
                    String[] curCode = selItem.split(" - ");
                    System.out.println(curCode[0]);
                }
                this.dispose();
            }
            private void btnCloseActionPerformed(ActionEvent ae)
            {   this.dispose();
            }
    
            private void keyBind()
            {   ActionMap am = pnlButton.getActionMap();
                am.put("copy_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCopyActionPerformed(e);
                        System.out.println("COPY CLICKED!");
                    }
                });
                am.put("close_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCloseActionPerformed(e);
                        System.out.println("CLOSE CLICKED!");
                    }
                });
                [b]
                am.put("F_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   System.out.println("control + F  is working");
                    }
                });[/b] //I add ctrl+F key to try if it will work
    
                InputMap im = pnlButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                im.put(KeyStroke.getKeyStroke("insert"), "copy_key");
                im.put(KeyStroke.getKeyStroke("escape"), "close_key");
             [b]im.put(KeyStroke.getKeyStroke("control F"), "F_key");[/b]
            }
    
            public static void main(String[] args)
            {	CurSymbolForm csf = new CurSymbolForm();
            }
        }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's an example of a functioning key binding:

    Java Code:
    import javax.swing.*;
    
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CurSymbolForm extends JDialog {
       private static final String COPY_KEY = "copy_key";
       private static final String CLOSE_KEY = "close_key";
       private static final KeyStroke INSERT_KEY_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0);
       private static final KeyStroke CLOSE_KEY_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
       JButton btnCopy, btnClose;
       JPanel pnlForm, pnlButton;
       JList lstCurrencies;
       JScrollPane scrList;
    
    
       public CurSymbolForm(JFrame frame) {
          super(frame, "Foo", true);
          setModalityType(ModalityType.APPLICATION_MODAL);
    
          btnCopy = new JButton(
                   "<html><center><b> COPY <font color = rgb(255,0,0)><u>[ INSERT ]</u></font> </b></center></html>");
          btnCopy.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                btnCopyActionPerformed(e);
             }
          });
    
          btnClose = new JButton(
                   "<html><center><b> CLOSE <font color = rgb(255,0,0)><u>[ ESC ]</u></font> </b></center></html>");
          btnClose.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                btnCloseActionPerformed(e);
             }
          });
    
          pnlButton = new JPanel();
          pnlButton.setVisible(true);
          pnlButton.setLayout(new BorderLayout());
          pnlButton.add(btnCopy, BorderLayout.LINE_START);
          pnlButton.add(btnClose, BorderLayout.LINE_END);
    
          Object[] listValue = {"Hello", "Java", "World"};
          lstCurrencies = new JList(listValue);
          lstCurrencies.setVisible(true);
          lstCurrencies.setVisibleRowCount(20);
          lstCurrencies.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          lstCurrencies.setSelectedIndex(0);
          
          InputMap lstCurInMap = lstCurrencies.getInputMap(JComponent.WHEN_FOCUSED);
    //      lstCurInMap.put(INSERT_KEY_STROKE, "none");
    //      lstCurInMap.put(CLOSE_KEY_STROKE, "none");
          
          scrList = new JScrollPane(lstCurrencies);
    
          pnlForm = new JPanel();
          pnlForm.setVisible(true);
          pnlForm.setLayout(new BorderLayout());
          pnlForm.add(pnlButton, BorderLayout.PAGE_START);
          pnlForm.add(scrList, BorderLayout.PAGE_END);
    
          this.setSize(new Dimension(400, 470));
          this.setLayout(new BorderLayout());
          this.add(pnlForm, BorderLayout.CENTER);
    
          int intW = Toolkit.getDefaultToolkit().getScreenSize().width;
          int intH = Toolkit.getDefaultToolkit().getScreenSize().height;
          int intX = (intW - this.getWidth()) / 2;
          int intY = (intH - this.getHeight()) / 2;
          this.setLocation(intX, intY);
    
          keyBind();
          pack();
          this.setVisible(true);
       }
    
       private void btnCopyActionPerformed(ActionEvent ae) {
          int selRow = -1;
          selRow = lstCurrencies.getSelectedIndex();
          if (selRow > -1) {
             String selItem = lstCurrencies.getSelectedValue().toString();
             String[] curCode = selItem.split(" - ");
             System.out.println(curCode[0]);
          }
          this.dispose();
       }
    
       private void btnCloseActionPerformed(ActionEvent ae) {
          this.dispose();
       }
    
       private void keyBind() {
          ActionMap am = pnlButton.getActionMap();
          am.put(COPY_KEY, new AbstractAction() {
             public void actionPerformed(ActionEvent e) {
                btnCopyActionPerformed(e);
                System.out.println("COPY CLICKED!");
             }
          });
          am.put(CLOSE_KEY, new AbstractAction() {
             public void actionPerformed(ActionEvent e) {
                btnCloseActionPerformed(e);
                System.out.println("CLOSE CLICKED!");
             }
          });
    
          InputMap im = pnlButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
          //!!im.put(KeyStroke.getKeyStroke("insert"), "copy_key");
          //!! im.put(KeyStroke.getKeyStroke("escape"), "close_key");
          im.put(INSERT_KEY_STROKE, COPY_KEY);
          im.put(CLOSE_KEY_STROKE, CLOSE_KEY);
       }
    
       public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          CurSymbolForm csf = new CurSymbolForm(frame);
          
          frame.dispose();
    
       }
    }

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thanks, that solve it.

    If you dont mind, I have some more question.

    Why getKeyStroke(String s) method recognize the string "control F" as CTRL+F key but string "insert" is not recognized as INSERT key?
    Is string "insert" is not the correct keyword for INSERT key?
    Do you know any site that has a list of keyword (in string) for getKeyStroke(String s) method?

    Here is the updated code.
    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CurSymbolForm extends JDialog
    {
            JButton btnCopy, btnClose;
            JPanel pnlForm, pnlButton;
            JList lstCurrencies;
            JScrollPane scrList;
    
            public CurSymbolForm()
            {
                setModalityType(ModalityType.APPLICATION_MODAL);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
                btnCopy = new JButton("<html><center><b> COPY <font color = rgb(255,0,0)><u>[ INSERT ]</u></font> </b></center></html>");
                btnCopy.addActionListener(new ActionListener()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCopyActionPerformed(e);
                    }
                });
    
                btnClose = new JButton("<html><center><b> CLOSE <font color = rgb(255,0,0)><u>[ ESC ]</u></font> </b></center></html>");
                btnClose.addActionListener(new ActionListener()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCloseActionPerformed(e);
                    }
                });
    
                pnlButton = new JPanel();
                pnlButton.setVisible(true);
                pnlButton.setLayout(new BorderLayout());
                pnlButton.add(btnCopy, BorderLayout.LINE_START);
                pnlButton.add(btnClose, BorderLayout.LINE_END);
    
                Object[] listValue = {"Hello", "Java", "World"};
                lstCurrencies = new JList(listValue);
                lstCurrencies.setVisible(true);
                lstCurrencies.setVisibleRowCount(20);
                lstCurrencies.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                lstCurrencies.setSelectedIndex(0);
                scrList = new JScrollPane(lstCurrencies);
    
                pnlForm = new JPanel();
                pnlForm.setVisible(true);
                pnlForm.setLayout(new BorderLayout());
                pnlForm.add(pnlButton, BorderLayout.PAGE_START);
                pnlForm.add(scrList, BorderLayout.PAGE_END);
    
                keyBind();
    
                this.setSize(new Dimension(400, 470));
                this.setLayout(new BorderLayout());
                this.add(pnlForm, BorderLayout.CENTER);
    
                int intW = Toolkit.getDefaultToolkit().getScreenSize().width;
                int intH = Toolkit.getDefaultToolkit().getScreenSize().height;
                int intX = (intW - this.getWidth())/2;
                int intY = (intH - this.getHeight())/2;
                this.setLocation(intX, intY);
    
                this.setVisible(true);
                pack();
            }
    
            private void btnCopyActionPerformed(ActionEvent ae)
            {   int selRow = -1;
                selRow = lstCurrencies.getSelectedIndex();
                if(selRow > -1)
                {   String selItem = lstCurrencies.getSelectedValue().toString();
                    String[] curCode = selItem.split(" - ");
                    System.out.println(curCode[0]);
                }
                this.dispose();
            }
            private void btnCloseActionPerformed(ActionEvent ae)
            {   this.dispose();
            }
    
            private void keyBind()
            {   ActionMap am = pnlButton.getActionMap();
                am.put("copy_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCopyActionPerformed(e);
                        System.out.println("COPY CLICKED!");
                    }
                });
                am.put("close_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   btnCloseActionPerformed(e);
                        System.out.println("CLOSE CLICKED!");
                    }
                });
    
                am.put("F_key", new AbstractAction()
                {   public void actionPerformed(ActionEvent e)
                    {   System.out.println("control + F  is working");
                    }
                });
    
                InputMap im = pnlButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                [b]//im.put(KeyStroke.getKeyStroke("insert"), "copy_key");
                //im.put(KeyStroke.getKeyStroke("escape"), "close_key");
                [u]//Here is what I change based on the example that you gave[/u]
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0), "copy_key");
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close_key");[/b]
                im.put(KeyStroke.getKeyStroke("control F"), "F_key");
            }
    
            public static void main(String[] args)
            {	CurSymbolForm csf = new CurSymbolForm();
    	csf.dispose();
            }
        }
    Once again, Thank you

Similar Threads

  1. KeyBinding key released doesn't work so well -.-
    By Addez in forum New To Java
    Replies: 1
    Last Post: 09-22-2010, 01:09 PM
  2. KeyBinding Help
    By Lil_Aziz1 in forum New To Java
    Replies: 12
    Last Post: 07-27-2010, 04:58 PM
  3. Replies: 1
    Last Post: 03-08-2010, 05:14 PM
  4. Replies: 0
    Last Post: 01-05-2009, 11:48 PM
  5. ZombieEscape: Multiple KeyBinding at one moment?
    By Unome in forum Java Applets
    Replies: 0
    Last Post: 11-17-2008, 07:51 PM

Posting Permissions

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