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);
	}
}

Recommended Answers

All 4 Replies

Why does the actionPerformed only deal with Final variables ??

Your question was already answered more that two months ago, in this thread started by you.
I remember cause I was one of the repliers. Please do not ask the same questions again and again.

ok sorry .. but it wasnt resolved for me at that time and i simply avoided what i was doing then.
this post can be deleted if considered as multiple posting.

Even if it wasn't resolved then, you were definitely aware that we had given you the information you needed, all you had to do was recheck that thread so it still doesn't justify you posting the same question again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.