简体   繁体   中英

why the append function doesn't work within ActionListener?

After choosing a file in JFileChooser I want JTextField to be appended with the path to it. I wrote the following code:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

public class Program extends JFrame{


    public static void main(String[] args){

        new Program();

    }

    public Program(){

        this.setSize(500,500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setTitle("Projekt Java");

        JPanel thePanel = new JPanel();

        thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        JButton button1 = new JButton("Wybierz plik:");
        JButton button2 = new JButton("Dodaj");
        JTextField text = new JTextField(23);
        JTextArea textArea1 = new JTextArea(10,30);

        thePanel.add(button1);
        thePanel.add(text);
        thePanel.add(button2);
        thePanel.add(textArea1);
        this.add(thePanel);

        button1.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent ae) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                  File selectedFile = fileChooser.getSelectedFile();
                  System.out.println(selectedFile.getName());
                  String directory = fileChooser.getCurrentDirectory().toString();

                  text.append(directory); // doesn't work
                }
              }
            });

        this.setVisible(true);

    }

    private static void createFileChooser(final JFrame frame) {

        String filename = File.separator+"tmp";
        JFileChooser fileChooser = new JFileChooser(new File(filename));

        // pop up an "Open File" file chooser dialog
        fileChooser.showOpenDialog(frame);

        System.out.println("File to open: " + fileChooser.getSelectedFile());

        // pop up an "Save File" file chooser dialog
        fileChooser.showSaveDialog(frame);

        System.out.println("File to save: " + fileChooser.getSelectedFile());

    }       
}

However, the append function doesn't work here. Could someone explain why is that? It works unless it isn't in the action listener. My errors are:

Cannot refer to a non-final variable text inside an inner class defined in a different method The method append(String) is undefined for the type JTextField

I changed to text.append(directory); text.setText(directory); and modified JTextfield to be final and it worked.

Use text.setText(directory) instead of text.append(directory) . The append() method is for JTextArea , JTextField doesn't have it.

And you have to declare JTextField text as final JTextField text .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM