#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    69
    Rep Power
    0

    Post Help Opening New Class


    please help me how to open
    the new window class that i create in this program
    and i want to change the keystroke("M") that i use in (ALT+F1) key please help me thanks


    Code:
    public class MainClass {
    
      public static void main(final String args[]) {
        final JFrame frame = new JFrame("Frame Key");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        Action actionListener = new AbstractAction() {
          public void actionPerformed(ActionEvent actionEvent) {
          new window();   // open new class file
          }
        };
        
        JPanel content = (JPanel) frame.getContentPane();
        KeyStroke stroke = KeyStroke.getKeyStroke("M");
        //KeyStroke stroke = KeyStroke.getKeyStroke("VK_ALT");    
        InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(stroke, "OPEN");
        content.getActionMap().put("OPEN", actionListener);
       
        frame.setSize(300, 300);
        frame.setVisible(true);
      }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Try this:
    KeyStroke.getKeyStroke(KeyEvent.VK_F1, ActionEvent.ALT_MASK );
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    69
    Rep Power
    0
    thanks for this sir .. i use it .... for now .. in my project ..
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    139
    Rep Power
    0

    Try this demo code


    Hi try this demo:

    Code:
    import*java.awt.event.ActionEvent;
    import*java.awt.event.InputEvent;
    import*java.awt.event.KeyEvent;
    import*javax.swing.AbstractAction;
    import*javax.swing.Action;
    import*javax.swing.ActionMap;
    import*javax.swing.InputMap;
    import*javax.swing.JButton;
    import*javax.swing.JComponent;
    import*javax.swing.JFrame;
    import*javax.swing.KeyStroke;
    public*class*KeyStrokeSampleVKF4Shift*{
    **public*static*void*main(String[]*a)*{
    ****String*ACTION_KEY*=*"theAction";
    ****JFrame*frame*=*new*JFrame("KeyStroke*Sample");
    ****frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ****JButton*buttonA*=*new*JButton("<html><center>ANCESTOR<br>VK_F4+SHIFT_MASK");
    ****Action*actionListener*=*new*AbstractAction()*{
    ******public*void*actionPerformed(ActionEvent*actionEvent)*{
    ********JButton*source*=*(JButton)*actionEvent.getSource();
    ********System.out.println(source.getText());
    ******}
    ****};
    ****KeyStroke*shiftF4*=*KeyStroke.getKeyStroke(KeyEvent.VK_F4,*InputEvent.SHIFT_MASK);
    ****InputMap*inputMap*=*buttonA.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    ****inputMap.put(shiftF4,*ACTION_KEY);
    ****ActionMap*actionMap*=*buttonA.getActionMap();
    ****actionMap.put(ACTION_KEY,*actionListener);
    ****buttonA.setActionMap(actionMap);
    ****frame.add(buttonA);
    ****frame.setSize(400,*200);
    ****frame.setVisible(true);
    **}
    }
    Note:
    KeyStroke*shiftF4*=*KeyStroke.getKeyStroke(KeyEvent.VK_F4,*InputEvent.SHIFT_MASK);
    Last edited by diyaots; February 20th, 2013 at 03:20 AM.

IMN logo majestic logo threadwatch logo seochat tools logo