使用jfilechooser选择目录

时间:2016-06-02 10:55:46

标签: java image jfilechooser

我遇到了filechooser的问题,将目录路径完全带入了图像查看器。我想从所选目录中获取所有图像。我在行

中得到了NullPointerException
String[] imagesList = selectedDirectory.list();

以下是代码:

JFileChooser wybierz = new JFileChooser();
File selectedDirectory;
String path;

int pos = 0;

public String[] getImages() {
    String[] imagesList = selectedDirectory.list();        
    return imagesList;
}

public void showImage(int index) {
    String[] imagesList = getImages();
    String imageName = imagesList[index];
    ImageIcon icon = new ImageIcon(getClass().getResource(path));
    Image image = icon.getImage().getScaledInstance(jLabel_Image.getWidth(), jLabel_Image.getHeight(), Image.SCALE_SMOOTH);
    jLabel_Image.setIcon(new ImageIcon(image));
}

public void fileChooser() {
    wybierz.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    wybierz.setAcceptAllFileFilterUsed(false);
    wybierz.addChoosableFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png", "tif"));
    int v = wybierz.showOpenDialog(null);
    if (v == JFileChooser.APPROVE_OPTION) {
        File selectedDirectory = wybierz.getSelectedFile();
        showImage(pos);
    }
}

问题是什么?

0 个答案:

没有答案