简体   繁体   中英

Why is my JTextArea append not working

Today, I wanted to pick up java, by making something to list all files in a directory (so that I can easily see what cards I miss in a digital card collection).
I got the file walker to work, however, it doesn't append the file names to the JTextArea I made.

I have this code:

package finlaydag33k.swing.gui;

import java.awt.EventQueue;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

public class Gui extends JFrame {

    private JPanel contentPane;
    static JTextArea filePanel = new JTextArea();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Gui frame = new Gui();
                    frame.setVisible(true);
                    try(Stream<Path> paths = Files.walk(Paths.get("/home/finlay/Pictures"))) {
                        paths.forEach(filePath -> {
                            if (Files.isRegularFile(filePath)) {
                                //System.out.println(filePath);
                            filePanel.append("Hii");
                            }
                        });
                    }
                    System.out.println(System.currentTimeMillis() - System.currentTimeMillis());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Gui() {
        setAlwaysOnTop(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new MigLayout("", "[][grow]", "[][grow]"));

        JTextArea filePanel =  new JTextArea();
        contentPane.add(filePanel, "cell 1 1,grow");
    }

}

On line 34 , the issue seems to happen (I just add the hii as a placeholder there so I can figure out the issue).
I don't get any error (only some warning saying The serializable class Gui does not declare a static final serialVersionUID field of type long , but this shouldn't cause it right?).
The GUI just loads up with the textArea empty. I hope somebody will be able to help me :)

Please forgive me for any cringe that you might get when reading the code :)
Cheers!

You have two instance with the same name but in different scope :

public class Gui extends JFrame {

    static JTextArea filePanel = new JTextArea();

And in the constructor :

public Gui() {
    ...
    JTextArea filePanel =  new JTextArea();

Which mean that you are adding the one declared in the constructor to the frame but are appending the static one in the main. Not the same instance at all.

Remove the static (because this doesn't make sense)

public class Gui extends JFrame {

     JTextArea filePanel;

Remove the second declaration, to initiliaze the variable in the constructor

public Gui() {
        ...
        filePanel =  new JTextArea();

and use the variable instance in the main to access it

frane.filePanel.append("Hii");

For more explanation, search about shadowing field in Java. This is simply what you are doing. You hide an variable by declaring the same variable in a different scope.

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