I am new to JApplet.I can't figure out how to access JPasswordField object from inner class inside a method actionPerformed.I want to add a PasswordField to my JFrame BioReader and then I want to compare truePassword with input in JPasswordField.I get error as "Password cannot resolve to variable'.
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BioReader extends JFrame{
public BioReader(){
super("BioTech Inc.");
setLayout(new FlowLayout());
JPasswordField Password = new JPasswordField(10);
add(Password);
BioReader.theHandler eventHandler = new BioReader.theHandler();
Password.addActionListener(eventHandler);
}
private class theHandler implements ActionListener {
public void actionPerformed(ActionEvent event){
if(event.getSource()==Password) //error
String.format("You typed: %s",event.getActionCommand());
}//actionPerformed ended
}//class theHandler ended
}//class BioReader ended
First of all variable names should NOT start with an upper case character. Some of your variable names are correct and some are not. Be consistent and follow Java conventions!
You can access the source of the ActionEvent, which is the component that generated the event:
Edit:
I didn't look at the video so I don't know what that code does, but in your problem is that you defined the password field as a local variable, not an instance variable.
That is you defined "password" in the constructor so only code in the constructor can access the variable.
If you want other methods to be able to access the variable then you need define it as an instance variable of the class: