The FileChooser
class will allow you to display a dialog that allows the user of your application to select a specific File
, without having to specify a long path like C:\\Some\\Really\\Long\\Path\\Because\\You\\Nest\\folders.jpg
. This may simplify your lives when trying to determine where your files are located, because you can specifically select them (though this is not always the best practice).
package edu.govschool;
import java.io.File;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class FCExample extends Application
{
private static Button btn;
private static FileChooser fc;
private static Image image;
private static ImageView imageView;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
btn = new Button("I'm here for no reason!");
fc = new FileChooser();
imageView = new ImageView();
fc.setTitle("Choose photo!");
File file = fc.showOpenDialog(primaryStage);
if (file != null) {
image = new Image(file.toURI().toString());
imageView.setImage(image);
}
Scene scene = new Scene(new VBox(imageView, btn));
primaryStage.setScene(scene);
primaryStage.setTitle("FileChooser Example");
primaryStage.show();
}
}