Facing problem inside the implementation of ActionListener
Hi, I don't understand what the problem is. Please go through the following code. The explanation of the problem is documented.
Any kind of help is appreciated.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
/**
* Class Test is intended for receiving a String input from the user and
* manipulating the input string later in some other methods. So the input
* string has to be stored for later use.
*
* Whenever an action takes place in JTextField or JButton (OK button) component,
* the corresponding ActionListener's actionPerformed() method assigns the
* user input to 'userInput' referene variable. This was assured when I placed
* JOptionPane.showMessageDialog(frame, userInput);
* right after the assignment statement. But when I cut and pasted the above
* statement right after the getUserInput() method call (in constructor's body)
* it showed null string.
*
* WHY & HOW this happens?
*
* I could have implemented this application with the help of JOptionPane's
* showInputDialog() method. But I just want to know what the problem is if
* I have tried in this way.
*/
publicclass Test{
/**
* String that the user types
*/
private String userInput;
/**
* Text field that contains the user input.
*/
private JTextField inputTextField;
/**
* Frame that contains all other components
*/
private JFrame frame;
public Test(){
prepareFrame();
getUserInput();
// shows null, why?
JOptionPane.showMessageDialog(frame, userInput);
// some other methods goes here that uses the input string....
// methodA();
// methodB();
}
/**
* Creates the frame and sets some of its properties.
*/
privatevoid prepareFrame(){
frame =new JFrame("Experiment with ActionListener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(false);
}
/**
* Receives user input and stores the reference to user input in userInput.
*/
privatevoid getUserInput(){
// Create sub-components
JLabel promptLabel =new JLabel("Enter a string: ");
inputTextField =new JTextField(20);
JButton okButton =new JButton("OK");
JButton cancelButton =new JButton("Cancel");
// Add listeners
inputTextField.addActionListener(new TextFieldHandler());
okButton.addActionListener(new OkButtonHandler());
cancelButton.addActionListener(new CancelButtonHandler());
// Create a panel to contain the sub-components
JPanel panel =new JPanel();
// Add the sub-components to the panel
panel.add(promptLabel);
panel.add(inputTextField);
panel.add(okButton);
panel.add(cancelButton);
frame.add(panel, BorderLayout.NORTH);
frame.validate();
frame.setVisible(true);
}
/**
* Event handler for text field that contains user input.
*/
privateclass TextFieldHandler
implements ActionListener{
publicvoid actionPerformed(ActionEvent event){
userInput = event.getActionCommand();
// works fine if the user input is shown here.
// JOptionPane.showMessageDialog(frame, userInput);
}
}
/**
* Event handler for OK button
*/
privateclass OkButtonHandler
implements ActionListener{
publicvoid actionPerformed(ActionEvent event){
userInput = inputTextField.getText();
// works fine if the user input is shown here.
// JOptionPane.showMessageDialog(frame, userInput);
}
}
/**
* Event handler for Cancel button
*/
privateclass CancelButtonHandler
implements ActionListener{
publicvoid actionPerformed(ActionEvent event){
System.exit(0);
}
}
publicstaticvoid main(String[] args){
new Test();
}
}

