I suspect this is a pretty trivial question. I wish to create a component MyTextField which extends JTextField; The component should respond to gaining focus by changing color and respond to losing focus by changing to one of two colors depending on whether entry is valid. How to do?
You can make your MyTextField
class extend JTextField
and implement FocusListener
Option# 1
public class MyTextField extends JTextField implements FocusListener {
public MyTextField (){
addFocusListener(this);
}
@Override
public void focusGained(FocusEvent event) {
}
@Override
public void focusLost(FocusEvent event) {
}
}
Option# 2
public class MyTextField extends JTextField {
public MyTextField (){
addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
}
@Override
public void focusGained(FocusEvent arg0) {
}
});
}
}