0

I have a simple login screen. I want UserName and Password to show up in the first and second text fields until they are clicked on. This functionality is working. However the first text field is always focused when the app launches and therefore shows up as "" until it loses focus. I tried to set a default button and request focus to no avail. It looks like the button is defaulting correctly, but it is not receiving focus for some reason. Anyone know how to fix this?

public class Basics implements ActionListener{

private JFrame frmBasics;
private JTextField userNameFeild;
private JTextField passwordFeild;
private JButton btnSignIn;
private JButton btnSignUp;

/**
 * Launch the application.
 */
public static void main(String[] args) {



    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Basics window = new Basics();
                window.frmBasics.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}//end main

/**
 * Create the application.
 */
public Basics() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmBasics = new JFrame();
    frmBasics.setTitle("Welcome to the POOPalace!!!");
    frmBasics.setBounds(100, 100, 511, 344);
    frmBasics.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmBasics.getContentPane().setLayout(null);

    userNameFeild = new JTextField("UserName");
    userNameFeild.setBounds(148, 79, 214, 20);
    userNameFeild.addFocusListener(new FocusedClass());
    frmBasics.getContentPane().add(userNameFeild);
    userNameFeild.setColumns(10);

    passwordFeild = new JTextField("Password");
    passwordFeild.setBounds(148, 126, 214, 20);
    passwordFeild.addFocusListener(new FocusedClass());
    frmBasics.getContentPane().add(passwordFeild);
    passwordFeild.setColumns(10);

    btnSignIn = new JButton("Sign In");
    btnSignIn.setBounds(148, 182, 89, 23);
    btnSignIn.addActionListener(this);
    frmBasics.getContentPane().add(btnSignIn);

    btnSignUp = new JButton("Sign Up");
    btnSignUp.setBounds(273, 182, 89, 23);
    btnSignUp.addActionListener(this);
    frmBasics.getContentPane().add(btnSignUp);

    //from what I've been reading these 2 lines should be the solution
    //but the request focus seems to not be working 
    frmBasics.getRootPane().setDefaultButton(btnSignIn);;
    btnSignIn.requestFocus();


}

@Override
public void actionPerformed(ActionEvent e) {

    //frmBasics.getContentPane().removeAll();
    //frmBasics.repaint();
    System.out.println(userNameFeild.getText());
    System.out.println(passwordFeild.getText());

}//actionPerformed


private class FocusedClass implements FocusListener {

    @Override
    public void focusGained(FocusEvent arg0) {

        if(arg0.getSource().equals(userNameFeild) && userNameFeild.getText().compareTo("UserName") == 0){
            userNameFeild.setText("");
            }
        if(arg0.getSource().equals(passwordFeild) && passwordFeild.getText().compareTo("Password") == 0){
            passwordFeild.setText("");
        }

    }

    @Override
    public void focusLost(FocusEvent arg0) {

        if(userNameFeild.getText().compareTo("") == 0){
            userNameFeild.setText("UserName");
        }

        if(passwordFeild.getText().compareTo("") == 0){
            passwordFeild.setText("Password");
        }

        frmBasics.getContentPane().repaint();
    }

}



}//class
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 02 '16 at 04:32
  • Have you solved the problem? For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 02 '16 at 14:56
  • As an aside `private JTextField passwordFeild;` should be `private JPasswordField passwordFeild;` with appropriate changes to the rest of the code. – Andrew Thompson Sep 03 '16 at 05:14

1 Answers1

1

Requesting focus works only after the layout of the window is complete. We need to call the requestFocusInWindow() in one of the three specific situations:

  1. In the windowOpened() method.
  2. In the EventQueue's invokeLater(), which will run after all pending events are processed.
  3. In the overriden JFrame's setVisible() method.

The first option:

//btnSignIn.requestFocusInWindow();

frmBasics.addWindowListener(new WindowAdapter() {
    @Override
    public void windowOpened(WindowEvent e) {
        btnSignIn.requestFocusInWindow();
    }
});

Also note that requestFocusInWindow() is more portable than requestFocus().

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77