Results 1 to 4 of 4
Thread: KeyPress for my Button
- 01-26-2010, 02:17 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
KeyPress for my Button
hi i need some help how to resolve this problem,
i dont know how to make an action for this button,Java Code:public class PopUpDialog { private JDialog popUpDialog; private JPanel popUpPanel; private JLabel errorMessage; private Image icon; private ImageIcon background; private JButton okButton; private int popUpWindowWidth; public PopUpDialog() { popUpDialog = new JDialog(); icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0); background = new ImageIcon("c:/pics/popupbck5.jpg"); okButton = new JButton("Ok"); errorMessage = new JLabel(); popUpWindowWidth = 300; } public void setComponents(Container cont) { popUpPanel = new JPanel() { @Override public void paintComponent(Graphics g) { // Scale image to size of component Dimension d = getSize(); g.drawImage(background.getImage(), 0, 0, d.width, d.height, null); super.paintComponent(g); } }; popUpPanel.setOpaque(false); // set this panel's layout to null for absolute positioning of // components popUpPanel.setLayout(null); // set the properties and actions of this button Action closeAction = new AbstractAction() { public void actionPerformed(ActionEvent ae) { popUpDialog.dispose(); } }; okButton.addActionListener(closeAction); okButton.setBounds(105, 38, 75, 20); okButton.setFont(new Font("Monospaced", Font.BOLD, 11)); okButton.setBackground(Color.LIGHT_GRAY); okButton.setMnemonic(KeyEvent.VK_ENTER); // UNSUPPORTED Hot Key // set the properties of this label errorMessage.setText("Please Check The Specified Field."); errorMessage.setBounds(10, 3, 500, 30); errorMessage.setFont(new Font("Monospaced", Font.BOLD, 12)); popUpPanel.add(errorMessage); popUpPanel.add(okButton); // add the panel to the container cont.add(popUpPanel); } public void createPopUpFrame() { setComponents(popUpDialog.getContentPane()); popUpDialog.setIconImage(icon); popUpDialog.setTitle(" Unauthorized Data Entry"); popUpDialog.setSize(popUpWindowWidth, 100); popUpDialog.setLocationRelativeTo(null); popUpDialog.setResizable(false); popUpDialog.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { PopUpDialog pU = new PopUpDialog(); public void run() { pU.createPopUpFrame(); } }); } }
instead of two keys (alt+enter) which is setMnemonic(KeyEvent.VK_ENTER);. I want to make a keypress "Enter" only... what method should i use to implement this?
-
Have you tried setting the root container's default button to the OK button? For e.g.,
An easier alternative though is to simply use one of the JOptionPanes.Java Code:public PopUpDialog() { popUpDialog = new JDialog(); okButton = new JButton("Ok"); // *** add this: popUpDialog.getRootPane().setDefaultButton(okButton); errorMessage = new JLabel(); popUpWindowWidth = 300; }
Much luck!
- 01-26-2010, 02:33 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
i tried but it doesnt work sir..
- 01-26-2010, 04:27 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
tnx sir.. i solved the problem.. and i would like to share what i gathered
you can use this if you want to customize or define your own dialog..Java Code:public class JButtonWithEnterKeyPress { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); // set this panel's layout to null for absolute positioning of components panel.setLayout(null); JButton button = new JButton(); button.registerKeyboardAction(button.getActionForKeyStroke( KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED); button.registerKeyboardAction(button.getActionForKeyStroke( KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), JComponent.WHEN_FOCUSED); button.setText("Press The Enter Key"); button.setBounds(0, 0, 200, 120); panel.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(panel); frame.setVisible(true); frame.setSize(200, 120); } }
this is a part of my school project so im desperate for looking this algorithm for my button..Last edited by bigj; 01-26-2010 at 04:34 PM.
Similar Threads
-
how to disabled button...?
By mlibot in forum New To JavaReplies: 0Last Post: 10-09-2009, 08:36 AM -
Button
By Tb0h in forum New To JavaReplies: 6Last Post: 07-22-2009, 01:28 AM -
button coordinates
By jacline in forum AWT / SwingReplies: 2Last Post: 04-05-2009, 10:58 PM -
Using SWT Button
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks