是否可以在 JFileChooser 窗口中选择文件名? [英] Is it possible to select the file name in a JFileChooser window?

查看:31
本文介绍了是否可以在 JFileChooser 窗口中选择文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以设置默认文件名:在 JFileChooser 窗口中使用:

I can set the default File Name: in a JFileChooser window using:

fileChooser.setSelectedFile();

我想知道是否也可以选择它,以便如果您想将文件另存为其他内容,您可以立即开始改写它.感谢您对此的任何帮助.

I was wondering if it is also possible to select it, so that if you want to save the file as something else you can immediately start to overtype it. Thanks for any help on this.

package filetest;

import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

@SuppressWarnings("serial")
class Editor {

    public static class TextClass extends JTextArea {

        FileClass fileClass = new FileClass();

        public void setKeyboardShortcuts() {
            fileClass.setKeyboardShortcuts();
        }

        private class FileClass {

            private File directory;
            private String filepath = "";
            private String filename = "";

            private void setKeyboardShortcuts() {

                Action ctrlo = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            openFile();
                        } catch (UnsupportedEncodingException e1) {
                        }
                    }
                };
                getInputMap().put(KeyStroke.getKeyStroke("ctrl O"), "ctrlo");
                getActionMap().put("ctrlo", ctrlo);

                Action ctrls = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            saveFile();
                        } catch (UnsupportedEncodingException e1) {
                        }
                    }
                };
                getInputMap().put(KeyStroke.getKeyStroke("ctrl S"), "ctrls");
                getActionMap().put("ctrls", ctrls);
            }

            private String selectFile(String fileaction) throws FileNotFoundException { 
                JFileChooser filechooser = new JFileChooser();
                if (directory != null) {
                    filechooser.setCurrentDirectory(directory);
                } else {
                    filechooser.setCurrentDirectory(new File("."));
                }
                filechooser.setSelectedFile(new File(filepath));
                int r = 0;
                if (fileaction.equals("openfile"))
                    r = filechooser.showDialog(new JPanel(), "Open file");
                else
                    r = filechooser.showDialog(new JPanel(), "Save file");
                if (r == JFileChooser.APPROVE_OPTION) {
                    try {
                        directory = filechooser.getSelectedFile().getParentFile();
                        filename = filechooser.getSelectedFile().getName();
                        return filename;
                    } catch (Exception exception) {
                        return "";
                    }
                } else {
                    return "";
                }
            }

            private void openFile() throws UnsupportedEncodingException {
                try {
                    String filestr = selectFile("openfile");
                    if (filestr.equals(""))
                        return;
                    else
                        filepath = filestr;
            } catch (FileNotFoundException ex) {
                    Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            private void saveFile() throws UnsupportedEncodingException {   
                try {
                    String filestr = selectFile("savefile");
                    if (filestr.equals(""))
                        return;
                    else
                        filepath = filestr;
                } catch (FileNotFoundException ex) {
                        Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            javax.swing.UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                    java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                    java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                }
                createAndShowGui();
            }
        });
    }

    private static void createAndShowGui() {

        JFrame frame = new JFrame();
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        JTextArea textArea = new TextClass();
        frame.add(textArea);
        ((TextClass) textArea).setKeyboardShortcuts();
        frame.setVisible(true);
    }
}

推荐答案

问题似乎是由以下几行引起的:

The problem seems to be caused by these lines:

try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            javax.swing.UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

如果它们被删除,文件名会像 Unai Vivi 的例子一样突出显示.

If they are removed, the file name is highlighted as in Unai Vivi's example.

这篇关于是否可以在 JFileChooser 窗口中选择文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆