Java Swing文件和文件夹选择

问题描述 投票:-1回答:1

嗨,我是新手,我需要一些帮助。我已经构建了一个文件处理项目,在那里我读取了项目中的输入文件和一些其他现有文件,做了很多检查和解析并生成了csv和xlsx文件。到现在为止,我用这些输入

JTextField csvpath = new JTextField();
    JTextField csvfile = new JTextField();
    JTextField xmlpath = new JTextField();
    JTextField xmlfile = new JTextField();
    JTextField excpath = new JTextField();
    JTextField excfile = new JTextField();
    Object[] message = {
        "Enter the path of the CSV file to be created:", csvpath,
        "Enter the CSV file name:", csvfile,
        "Now enter the XML path to be read:", xmlpath,
        "Also enter the XML file name:", xmlfile,  
        "Please enter the Excel file path to be created:", excpath,
        "Finally enter the Excel file name:", excfile     
    };

    int option = JOptionPane.showConfirmDialog(null, message, "Convert XML File to CSV File", JOptionPane.OK_CANCEL_OPTION);
    if (option == JOptionPane.OK_OPTION) {
        String csvPath = csvpath.getText();
        String csvFileName = csvfile.getText();
        String xmlPath = xmlpath.getText();
        String xmlFileName = xmlfile.getText();
        String excPath = excpath.getText();
        String excFileName = excfile.getText();
        String FullcsvPath = csvPath + "\\" + csvFileName + ".csv";
        String FullxmlPath = xmlPath + "\\" + xmlFileName + ".xml";
        String excelPath = excPath + "\\" + excFileName + ".xlsx";
            .
            .
        parsing/creating starts...

因为将来这个项目将被我想要创建文件选择器的其他人使用,并将文件/文件夹路径作为字符串来读取输入和创建等...我已经为路径和时创建了一个类和公共字符串我称它为程序不会像以前一样停止,在Frame打开时继续运行而没有得到我想要的路径。我的新课程是:

public static void SelectFiles() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {

    final JFrame window = new JFrame("Parse for manufacturers - Developed by Aris M");
    JPanel topPanel = new JPanel();
    JPanel midPanel = new JPanel();
    JPanel botPanel = new JPanel();
    final JButton openFolderChooser = new JButton("Select Folder to save csv - xlsx results");
    final JButton openFileChooser = new JButton("Select the File to be parsed");
    final JButton closeButton = new JButton("OK");
    window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    final JFileChooser fc = new JFileChooser();

    openFolderChooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            fc.setCurrentDirectory(new java.io.File("user.home"));
            fc.setDialogTitle("This is a JFileChooser");
            fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            if (fc.showOpenDialog(openFolderChooser) == JFileChooser.APPROVE_OPTION) {
                JOptionPane.showMessageDialog(null, fc.getSelectedFile().getAbsolutePath());
                System.out.println(fc.getSelectedFile().getAbsolutePath());
                csv = fc.getSelectedFile().getAbsolutePath();
                xlsx = csv;
                System.out.println(csv);                    
            }                
        }
    });
    openFileChooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            fc.setCurrentDirectory(new java.io.File("user.home"));
            fc.setDialogTitle("This is a JFileChooser");
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (fc.showOpenDialog(openFileChooser) == JFileChooser.APPROVE_OPTION) {
                JOptionPane.showMessageDialog(null, fc.getSelectedFile().getAbsolutePath());
                System.out.println(fc.getSelectedFile().getAbsolutePath());
                xml = fc.getSelectedFile().getAbsolutePath();
                System.out.println(xml);  
            }
        }
    });
    closeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {   
                window.dispose();              
        }
    });
    window.add(BorderLayout.NORTH, topPanel);
    window.add(BorderLayout.CENTER, midPanel);
    window.add(BorderLayout.SOUTH, botPanel);
    topPanel.add(openFolderChooser);
    midPanel.add(openFileChooser);
    botPanel.add(closeButton);
    window.setSize(500, 150);
    window.setVisible(true);
    window.setLocationRelativeTo(null);            
}
java swing jframe jfilechooser
1个回答
0
投票

Swing代码在称为事件派发线程的单独线程中运行。在框架可见时,只需让主线程忙。将以下代码添加到SelectFiles()方法的末尾:

        while (window.isVisible()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.