从JFileChooser获取目录路径到照片查看器

时间:2016-06-01 14:23:44

标签: java image directory jfilechooser

将照片目录放入图像查看器时遇到了一些问题。我使用JFileChooser,用户只能选择一个目录。你能告诉我在代码中设置什么而不是“/ obrazy / obrazysm”吗?

JFileChooser wybierz = new JFileChooser();

public String[] getImages() {
    File file = new File(getClass().getResource("/obrazy/obrazysm").getFile());
    String[] imagesList = file.list();
    return imagesList;
}

public void fileChooser(){
    wybierz.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    wybierz.setAcceptAllFileFilterUsed(false);
    int v = wybierz.showOpenDialog(null);
}

1 个答案:

答案 0 :(得分:0)

永远不会调用

fileChooser()。此外,您需要检索选定的值。 试试这个:

JFileChooser wybierz = new JFileChooser();

public String[] getImages() {
    File file = fileChooser();  
    String[] imagesList = file.list();
    return imagesList;
}

public File fileChooser() {
   File selectedDirectory = null;
   wybierz.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
   wybierz.setAcceptAllFileFilterUsed(false);

   //result of select files (yes/no/cancel)
   int ync = wybierz.showOpenDialog(null);

   //if 'ok' has been pressed
   if (ync == JFileChooser.APPROVE_OPTION) {
        selectedDirectory = wybierz.getSelectedFile();            
   }
   return selectedDirectory;
}