I was writing a program, and then I saw this on some website that is using the this keyword in this code, and I was wondering what it’s purpose, it can process a Jbutton, or JTextField, it can show message using the this keyword, what happened to getSource()?
Here’s the code
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
public class TextPassWordEvent extends JFrame {
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JPasswordField passwordField;
private JButton button;
public TextPassWordEvent(){
super("Title");
setLayout(new FlowLayout());
textField1 = new JTextField(10);
add(textField1);
textField2 = new JTextField("Enter your Text Here");
add(textField2);
textField3 = new JTextField("Uneditable Text field");
textField3.setEditable(false);
add(textField3);
passwordField = new JPasswordField("Password");
add(passwordField);
button = new JButton("Submit");
add(button);
TextHandler handler = new TextHandler();
textField1.addActionListener(handler);
textField2.addActionListener(handler);
textField3.addActionListener(handler);
passwordField.addActionListener(handler);
button.addActionListener(handler);
}
private class TextHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(TextPassWordEvent.this, String.format("Message: %s",event.getActionCommand()));
}
}
}
In java this refers to the current object instance.
In the example above, the place where ‘this’ is used is in some code within an inner class. If this code used ‘this’ without any context, then it would be referring to the instance of the inner class.
Java provides the notation of OuterClassName.this as a way of referring to the instance of the outer class that this inner class was instantiated in.
So its simply referring to the instance of the TextPasswordEvent class.
TextPasswordEvent is a container and when calling showMessageDialogue you need to pass a container within which the dialog will be displayed, so the TextPasswordEvent.this is saying “pass the instance of the TextPasswordEvent object” to the showMessageDialogue method. If it didn’t use the qualification and just passed “this” it would be passing a reference to the TextHandler inner class instance instead which is not the desired behaviour.
edit:more info
TextHandler is an inner class that implements the ActionListener interface. In this interface there is a method defined called actionPerformed.
The outer class is creating various controls (buttons, text field etc.) and then creating one instance of the TextHandler class and setting it as the action listener on these controls. These controls then call the actionPerformed method when the user hits a button or the return key (depending on the control).
Inside the actionPerformed method of the inner class its showing a dialog (showMessageDialogue) and passing two params – the container to show the dialog inside and the message to display in it.
The message it is showing is including the command that was passed. This is obtained by calling getActionCommand() on the ActionEvent object that is passed to the actionPerformed() method. The controls create and pass this ActionEvent object when they make the call to actionPerformed, after a user has done the action (pressed the button, hit the return key etc.).
Edit 2:
Add a new inner class definition for another ActionListener that will only be added to your button component:
Then back in the TextPassWordEvent constructor code: