The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Help Opening New Class
Discuss Help Opening New Class in the Java Help forum on Dev Shed. Help Opening New Class Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 11th, 2013, 02:12 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
|
|
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);
}
}

|

January 11th, 2013, 01:51 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Try this:
KeyStroke.getKeyStroke(KeyEvent.VK_F1, ActionEvent.ALT_MASK );
|

January 24th, 2013, 09:04 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
|
|
|
thanks for this sir .. i use it .... for now .. in my project ..
|

February 20th, 2013, 03:15 AM
|
|
Contributing User
|
|
Join Date: Mar 2011
Posts: 119
Time spent in forums: 1 Day 40 m 11 sec
Reputation 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|