I have a TilePane with a imageView items, each imageview displaying an image got from a directory in my pc. When any of the images in the tilePane is clicked a message is printed onto console with the direcory path address to the folder where the image resides, for example:
You clicked: ImageResources/wp.png
I would like to extend this further so that when a particular image is clicked the folder in which the image resides opens.
My implementation below only prints to console the message below, but no directory/ folder gets opened. The message is:
File Not Found
How can I get it to work so that the folder|directory opens? Thank you all in advance.
Also, of the directory could open with the clicked image selected, then that would be an extra, but is not a priority right now.
My implementation so far goes as follows:
public class TilePaneExample extends Application {
@Override
public void start(Stage primaryStage) {
VBox root = new VBox(30);
String[] imageResources = new String[]{
//loading images
"ImageResources/facebook.png",
"ImageResources/faviicon.png",
"ImageResources/jquery-logo.png",
"ImageResources/linkedin_32.png",
"ImageResources/loading1.png",
"ImageResources/twitter.png",
"ImageResources/twitter_32.png",
"ImageResources/wp.png",};
// Pane
TilePane tilePane = new TilePane();
tilePane.setHgap(5);
tilePane.setVgap(5);
for (final String imageResource : imageResources) {
Image image = new Image(getClass().getResourceAsStream(imageResource));
ImageView imageView = new ImageView(image);
imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
File f = new File(imageResource);
String absolutePath = f.getAbsolutePath();
String folderPath = absolutePath.
substring(0, absolutePath.lastIndexOf(File.separator));
try {
Desktop.getDesktop().open(new File(folderPath));
} catch (IllegalArgumentException iae) {
System.out.println("File Not Found");
} catch (IOException ex) {
Logger.getLogger(TilePaneExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
tilePane.getChildren().add(imageView);
}
root.getChildren().addAll(tilePane);
primaryStage.setTitle("TilePane Example");
Scene scene = new Scene(root, 300, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
folderPathin runtime?"ImageResources/faviicon.png", prints its folder path asD:\standAloneDev\java\workingDir\Jive\TilePaneExample\ImageResources, @PphoenixIllegalArgumentException iaeshows you what the problem is. tryiae.printStackTrace()instead of your sysout.