Pregunta

This is a program which uses the JFileChooser:

package execute;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class FCDemo extends JFrame
{
    JFileChooser fc = new JFileChooser();
    private String fileName;

    public FCDemo(String title)
    {
        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel pnl = new JPanel();
        pnl.setLayout(new GridLayout(2, 1));

        JButton btn = new JButton("JFileChooser.showOpenDialog() Demo");
        ActionListener al;
        al = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                switch (fc.showOpenDialog(FCDemo.this))
                {
                case JFileChooser.APPROVE_OPTION:
                    fileName=fc.getSelectedFile().getName();
                    JOptionPane.showMessageDialog(FCDemo.this, "Selected: "+fc.getSelectedFile(),"FCDemo",JOptionPane.OK_OPTION);
                    break;

                case JFileChooser.CANCEL_OPTION:
                    JOptionPane.showMessageDialog(FCDemo.this, "Cancelled","FCDemo",JOptionPane.OK_OPTION);
                    break;

                case JFileChooser.ERROR_OPTION:
                    JOptionPane.showMessageDialog(FCDemo.this, "Error","FCDemo",JOptionPane.OK_OPTION);
                }

            }   
        };
        btn.addActionListener(al);
        pnl.add(btn);

        setContentPane(pnl);

        pack();
        setVisible(true);
    }

    public String get_fileName(){
        return fileName;
    }


    public static void main(String[] args)
    {

        FCDemo demo=new FCDemo("filechooser");
        System.out.println("the file name is= "+demo.get_fileName());
    }
}

for some reason, the directory of the file selected is not being stored in the string variable called fileName. when I print the fileName to the console, I get null can anyone help me fix this?

¿Fue útil?

Solución

Your main thread is executed before the file chosen. That's why file name is being printed as null. To see your selected file try to sleep the main thread for 10 seconds. And choose your file. After that you can see the selected file in main. Use:

public static void main(String[] args)
{
    FCDemo demo=new FCDemo("filechooser");
    try {
          Thread.sleep(10000);
    } catch (Exception e) {
        // TODO: handle exception
    }
    System.out.println("the file name is= "+demo.get_fileName());
}

Otros consejos

The filename is stored correctly. Try to observe the console before you press the button in your initial JFrame. The output occurrs before you even see the FileChooser. Most likely Swing uses an additional Thread to paint the UI. The output is printed in parallel while the JFrame is painted.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top