2013-04-11 45 views
0

我有一个程序使用JFileChooser。简而言之,完整的程序是一个GUI,它允许用户操纵PNG和JPG。我想让JFileChooser立即打开图片目录(窗口)。当用户打开他们的JFileChooser时,它会直接打开图片库C:\ Users \(USER)\图片在指定的目录启动JFileChooser,只显示特定类型的文件

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

以下内容是指GUI的“浏览”按钮,用户在其中选择要编辑的图片并将其显示在屏幕上。

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(); 
    } 

谢谢。

回答

8

您需要构建您的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); 

本示例过滤以“.png”或“.jpg”结尾的文件。

+0

不错,但有没有办法让它直接进入C:\ Users \(user)\ Pictures目录? – corvid 2013-04-11 17:21:49

+0

在构造函数的'new File'中指定它。代码表示“文件开始”。像新的'文件(System.getProperty(“user.home”),“图片”)'应该工作。 – 2013-04-11 17:22:56

+0

Pictures目录的位置与系统高度相关,所以没有真正的Java方法来找到它。也就是说,您可以使用'FileSystemView'中的方法http://docs.oracle.com/javase/6/docs/api/javax/swing/filechooser/FileSystemView.html来查找用户的主目录。从那里它是一个猜谜游戏基于Windows XP/7/8/9/10/etc ... – 2013-04-11 17:24:16

4

阅读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

+0

你的比我更漂亮,+1。 – 2013-04-11 17:14:15

1

将所有内容都放到一个简洁的表格中,这里是一个灵活的文件选择器例程。它指定了初始目录和文件类型,并将结果作为文件或完整路径名称提供。您可能还想将整个程序设置为本地界面模式,方法是将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(); 
} 
相关问题