0
Я хочу установить фоновое изображение панели на изображение, которое пользователь выбирает через filechooser в javafx. Кто-нибудь знает как это сделать? Вот мой код:изменение фонового изображения во время выполнения с javafx
ImageView backgroundImageView = new ImageView();
backgroundImageView.setId("backgroundImageView");
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(new File(new File("src\\backgrounds").getAbsolutePath()));
fileChooser.setTitle("select background image");
Button openButton = new Button("select background image...");
openButton.setOnAction(
e -> {
File file = fileChooser.showOpenDialog(main.getPrimaryStage());
if (file != null) {
try {
root.setStyle("-fx-background-image: url(\'" + file.toURI().toURL().toString() + "\');-fx-background-position: center center;-fx-background-repeat: stretch;");
//root.setBackground(new Background(new BackgroundImage(new Image(file.toURI().toURL().toString()))));//terrible errors!
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
});
vBox.getChildren().add(openButton);