在指定的目录中启动一个JFileChooser,只显示特定类型的文件(Starting a JFil

2019-08-22 00:02发布

我利用一个JFileChooser的程序。 要简短,完整的程序是一个图形用户界面,允许用户操作PNG格式和JPG格式。 我想,这样JFileChooser中立即打开图片目录(窗口)做出来。 当用户打开他们的JFileChooser中,它会直接开到图片库C:\ Users \(用户)\图片

此外,这将是很好,只显示特定类型(PNG格式和JPG格式)的文件。 许多程序似乎是能够做到这一点; 只允许特定文件的选择。 JFileChooser的是否允许这样的事情? 目前,我使用的是大量不可靠的,周围的方法运行拒绝非的PNG / JPG图片。

下面指的是图形用户界面,其中用户将选择其用于编辑图片,并将其显示在屏幕上的“浏览”按钮。

    try {
       int val = filec.showOpenDialog(GridCreator.this);
       if(val==JFileChooser.APPROVE_OPTION) {
          File unfiltered_picture = filec.getSelectedFile();
          //get the extension of the file
          extension=unfiltered_picture.getPath();
          int index=extension.indexOf(".");
          extension=extension.substring(index+1, extension.length());
          //if the file is not jpg, png, or jpeg, reject it and send a message to the user.
          if(!extension.matches("[jJ][pP][gG]") && !extension.matches("[pP][nN][gG]") && !extension.matches("[jJ][pP][eE][gG]")) {
             JOptionPane.showMessageDialog(null,
                                           "cannot load file. File must be of type png, jpeg, or jpg. \n Your file is of type " + extension,
                                            "Error: improper file",
                                            JOptionPane.OK_OPTION);
           //if the file is of the proper type, display it to the user on the img JLabel.
           } else {
              finalImage = ImageIO.read(unfiltered_picture);
              ImageIcon imgIcon = new ImageIcon();
              imgIcon.setImage(finalImage);
              img.setIcon(imgIcon);
              img.invalidate();
              h_divide.setValue(0);
              v_divide.setValue(0);
           }
       }
   } catch(IOException exception) {
        exception.printStackTrace();
   }

谢谢。

Answer 1:

你需要构建你JFileChooser与要启动,然后通过一个目录FileFilter设置可见之前进去。

    final JFileChooser fileChooser = new JFileChooser(new File("File to start in"));
    fileChooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            final String name = f.getName();
            return name.endsWith(".png") || name.endsWith(".jpg");
        }

        @Override
        public String getDescription() {
            return "*.png,*.jpg";
        }
    });
    fileChooser.showOpenDialog(GridCreator.this);

这个例子过滤器中的“巴纽”或“.JPG”结尾的文件。



Answer 2:

阅读API: http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html

在javadoc的页面的最顶端是近正是你想做的事的例子:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}

你是在一般寻找类FileFilter ,这是抽象的。 请参阅的javadoc: http://docs.oracle.com/javase/6/docs/api/javax/swing/filechooser/FileFilter.html



Answer 3:

把所有到一个简洁的形式,这里是一个灵活的文件选择程序。 它指定初始目录和文件类型,它的配料,结果既作为一个文件或一个完整的路径名。 您可能还希望通过在主入口点你的程序放置setLookAndFeel命令,整个程序设置为本地接口模式。

String[] fileChooser(Component parent, String dir, String typeFile) {
    File dirFile = new File(dir);
    JFileChooser chooser = new JFileChooser();
    // e.g. typeFile = "txt", "jpg", etc.
    FileNameExtensionFilter filter = 
        new FileNameExtensionFilter("Choose a "+typeFile+" file",
            typeFile); 
    chooser.setFileFilter(filter);
    chooser.setCurrentDirectory(dirFile);
    int returnVal = chooser.showOpenDialog(parent);

    String[] selectedDirFile = new String[2];
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        // full path
        selectedDirFile[0] = chooser.getSelectedFile().getPath();
        // just filename
        selectedDirFile[1] = chooser.getSelectedFile().getName();
    }

    return selectedDirFile;
 }

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
    e.printStackTrace();
}


文章来源: Starting a JFileChooser at a specified directory and only showing files of a specific type