单击jFileChooser中的打开按钮后如何打开文件?

Avi*_*Raj 0 java swing jframe

这是我的 jFileChooser 摆动代码,它在单击打开按钮后打开另一个窗口。然后我们需要在第二个窗口中选择相应的文件,以便实际打开该文件。我需要对第一个窗口本身进行所有操作。

import com.opencsv.CSVReader;

import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/*
 * Created by JFormDesigner on Tue Sep 22 13:24:36 IST 2015
 */



/**
 * @author Avinash Raj
 */
public class FileChooser extends JFrame {
    private JFileChooser fileChooser1;
    public FileChooser() {
        initComponents();
    }

    private void fileChooser1ActionPerformed(ActionEvent e) {

        int returnVal = fileChooser1.showOpenDialog(FileChooser.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //File file = fc.getSelectedFile();
            setVisible(false);
            String path=fileChooser1.getSelectedFile().getAbsolutePath();
            String filename=fileChooser1.getSelectedFile().getName();
            //This is where a real application would open the file.

            System.out.println("Opening: " + path + "\n");
            CSVReader reader = null;
            try {
                reader = new CSVReader(new FileReader(path), ',', '\'', 1);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            String [] nextLine;
            try {
                while ((nextLine = reader.readNext()) != null) {
                    // nextLine[] is an array of values from the line
                    int no_cols = nextLine.length;
                    System.out.println(nextLine[0] + nextLine[1] + nextLine[2] );
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        } else {
            setVisible(false);
            System.out.println("Open command cancelled by user." + "\n");
        }
    }



    private void initComponents() {
      fileChooser1 = new JFileChooser();

        setLayout(new BorderLayout());
        setSize(700,500);

        fileChooser1.addActionListener(e -> fileChooser1ActionPerformed(e));
        fileChooser1.setFileSelectionMode(JFileChooser.FILES_ONLY);
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "csv files only", "csv");
        fileChooser1.setFileFilter(filter);
        add(fileChooser1, BorderLayout.CENTER);

       setVisible(true);
        setLocationRelativeTo(getOwner());

    }


    public  static  void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                FileChooser f = new FileChooser();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*ord 5

我通常不会将选择器嵌入顶层 Frame 或添加动作侦听器,而是在框架中添加按钮或菜单选项以触发何时显示选择器,然后等待 showOpenDialog() 返回然后调用getSelectedFile()。

我通常做的是这样的:

public class MyJFrame extends javax.swing.JFrame {

    public MyJFrame() {
        JButton btn = new JButton("open file");
        add(btn);
        btn.addActionListener(e -> {
            selectFile();
        });
        pack();
        setVisible(true);
    }

    public void selectFile() {
        JFileChooser chooser = new JFileChooser();
        // optionally set chooser options ...
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File f = chooser.getSelectedFile();
            // read  and/or display the file somehow. ....
        } else {
            // user changed their mind
        }
    }
}
Run Code Online (Sandbox Code Playgroud)