Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: password field, with focused "Enter" button

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default password field, with focused "Enter" button

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JPasswordField;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.ImageIcon;
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JComponent;
    import javax.swing.KeyStroke;
     
     
    public class PasswordField extends JFrame {
     
        private JPanel passwordPanel;
     
        private JPasswordField passwordField;
     
        private JLabel passwordLabel;
     
        private JButton userConfirmationBtn;
        private JButton submenu;
     
        private Image icon;
     
        private ImageIcon frameBackground;
     
        public PasswordField() {
     
            icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
            frameBackground = new ImageIcon("c:/pics/passwordbck2.jpg");
            passwordField = new JPasswordField();
            passwordLabel = new JLabel("Password: ");
            userConfirmationBtn = new JButton("Enter");
            submenu = new JButton("Submenu");
     
            initComponents();
        }
     
        private void initComponents() {
     
     
            // set the properties of this label
            passwordLabel.setBounds(12, 13, 80, 20);
            passwordLabel.setFont(new Font("Monospaced", Font.BOLD, 12));
            passwordLabel.setForeground(Color.BLACK);
     
            // set the properties and actions of this text field
            passwordField.setBounds(88, 13, 185, 20);
     
            // set the properties and actions of this button
            Action pass = new AbstractAction() {
     
                public void actionPerformed(ActionEvent e) {
     
                    // retrive the password data from the password field
                    char[] passwordChars = passwordField.getPassword();
     
                    String passwordStr = "";
     
                    // convert the returned password characters as string
                    for (int x = 0; x <= passwordChars.length - 1; x++) {
     
                        passwordStr = passwordStr + passwordChars[x];
                    }
     
                    // check if the password entered is valid
                    if (Password.isPasswordAuthentic(passwordStr)) {
     
     
                       // do something here
                    }
                    else if (passwordStr.equals("")) {
     
                        // do something here
                    }
                    else {
     
                       // do something here
                    }
                }
            };
            userConfirmationBtn.addActionListener(pass);
            userConfirmationBtn.setMnemonic(KeyEvent.VK_E);
            userConfirmationBtn.setBounds(88, 55, 90, 20);
            userConfirmationBtn.setFont(new Font("Monospaced", Font.BOLD, 11));
            userConfirmationBtn.setBackground(Color.LIGHT_GRAY);
            userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                                                       JComponent.WHEN_IN_FOCUSED_WINDOW);
            userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                                                       JComponent.WHEN_IN_FOCUSED_WINDOW);
     
     
            passwordPanel = new JPanel() {
     
                @Override
                protected void paintComponent(Graphics g) {
     
                    // Scale image to size of component
                    Dimension d = getSize();
     
                    g.drawImage(frameBackground.getImage(), 0, 0, d.width, d.height, null);
     
                    super.paintComponent(g);
                }
            };
            passwordPanel.setOpaque(false);
     
            // set this panel's layout to null for absolute positioning of
            // components
            passwordPanel.setLayout(null);
     
            // add the components to this panel
            passwordPanel.add(passwordLabel);
            passwordPanel.add(passwordField);
            passwordPanel.add(userConfirmationBtn);
     
     
            // add the panel to the container
            getContentPane().add(passwordPanel);
     
            setTitle(" User Confirmation");
            setIconImage(icon);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(288, 120);
            setResizable(false);
            setLocationRelativeTo(null);
            setVisible(true);
        }
     
        /**
         * Invoke this method if this window doesnt have any more use for this
         * current event.
         */
        private void disableWindow() {
     
            setVisible(false);
        }
     
        public static void main(String[] args) {
     
            // Schedule a job for the event-dispatching thread:
            // creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
     
                public void run() {
     
                    new PasswordField();
                }
            });
        }
    }

    the focus is on the "Enter" button so you dont have to press tab or click the button to be the focused component...


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: password field, with focused "Enter" button

    Code
    if (Password.isPasswordAuthentic(passwordStr)) {
    isn't valid.

Similar Threads

  1. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Swing Tutorials
    Replies: 2
    Last Post: March 14th, 2012, 06:49 AM
  2. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  3. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM
  4. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM