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