Ben Renton
Ben Renton

Reputation: 3

FocusGained and FocusLost not working?

Here is my code in the event methods:

Code

private void txtCampusSearchFocusGained(java.awt.event.FocusEvent evt) {                                            
    this.txtCampusSearch.setText(null);
}                                           

private void txtCampusSearchFocusLost(java.awt.event.FocusEvent evt) {                                          
    this.txtCampusSearch.setText("Search...");
}                                         

private void txtBuildingSearchFocusGained(java.awt.event.FocusEvent evt) {                                              
    this.txtCampusSearch.setText(null);
}                                             

private void txtBuildingSearchFocusLost(java.awt.event.FocusEvent evt) {                                            
    this.txtCampusSearch.setText("Search...");
}  

However when I actually run the application these methods don't seem to be activated when focus is gained / lost. Application Running

Upvotes: 0

Views: 694

Answers (1)

Si mo
Si mo

Reputation: 989

You have to add a focus listener to your textfield.

JTextField textField = new JTextField("A TextField");
 textField.addFocusListener(new MyFocusListener());

https://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html

Upvotes: 2

Related Questions