A layman's question on the definition and use of variables:
I need to make a Java GUI that gets user's input and stores it within a text file. However this writing has to be done inside an Actionlistener class (ie, user is clicking the button and text file is created and stored). This means that I have to define a variable in one class (public class) and use it in another (the one that defines the Actionlistener).
How can I do this? Are global variables the only way?
In my code I first define 'textfield' as JTextField and then I want it to be read (as 'text') and stored (in 'text.txt').
import javax.swing.*;
//...
import java.io.BufferedWriter;
public class Runcommand33
{
  public static void main(String[] args)
  {
final JFrame frame = new JFrame("Change Backlight");
   // ...
   // define frames, panels, buttons and positions
    JTextField textfield = new JTextField();textfield.setBounds(35,20,160,30);
    panel.add(textfield);
    frame.setVisible(true);
    button.addActionListener(new ButtonHandler());
  }
}
    class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent event){
    String text = textfield.getText();
        textfield.setText("");
        new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close();
    // Afterwards 'text' is needed to run a command
              }
            }
When I compile I get
Runcommand33.java:45: error: cannot find symbol
                String text = textfield.getText();
                              ^
  symbol:   variable textfield
  location: class ButtonHandler
Without lines String text = to new BufferedWriter the code compiles.
Note that I have tried the suggestions of this Get variable in other classes and this How do I access a variable of one class in the function of another class? but they didn't work.
Any suggestions?
 
     
     
     
    