Results 1 to 4 of 4
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default KeyPress for my Button

    hi i need some help how to resolve this problem,

    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();
                }
            });
        }
    }
    i dont know how to make an action for this button,
    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?

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

    Default

    Have you tried setting the root container's default button to the OK button? For e.g.,
    Java Code:
      public PopUpDialog() {
        popUpDialog = new JDialog();
        okButton = new JButton("Ok");
        
        // *** add this:
        popUpDialog.getRootPane().setDefaultButton(okButton);
        
        errorMessage = new JLabel();
        popUpWindowWidth = 300;
      }
    An easier alternative though is to simply use one of the JOptionPanes.

    Much luck!

  3. #3
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    i tried but it doesnt work sir..

  4. #4
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    tnx sir.. i solved the problem.. and i would like to share what i gathered

    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);
        }
    }
    you can use this if you want to customize or define your own dialog..
    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

  1. how to disabled button...?
    By mlibot in forum New To Java
    Replies: 0
    Last Post: 10-09-2009, 08:36 AM
  2. Button
    By Tb0h in forum New To Java
    Replies: 6
    Last Post: 07-22-2009, 01:28 AM
  3. button coordinates
    By jacline in forum AWT / Swing
    Replies: 2
    Last Post: 04-05-2009, 10:58 PM
  4. Using SWT Button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:05 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
  •