in th following code, i cant use a Text Field in the actionPerformed methode .. the textFiled is declared outside that method.
i get the following error:
Cannot refer to a non-final variable serverHostField inside an inner class defined in a different method
How can i solve this???
Thank you for your help
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestActionPerformed extends JFrame
{
public TestActionPerformed()
{
Container c= getContentPane();
JPanel panel= new JPanel();
JTextField serverHostField= new JTextField("localHost");
JButton loginB= new JButton("Login");
panel.add(serverHostField);
c.add(panel);
loginB.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String server=serverHostField.getText(); // ERROR here
}
});
}
public static void main(String [] args)
{
TestActionPerformed f= new TestActionPerformed();
f.setVisible(true);
}
}