简体   繁体   中英

BorderPane JavaFX resize automatically when loading an image

This code:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));

        primaryStage.setTitle("Load an Image");
        primaryStage.setScene(new Scene(root));

        BorderPane mainBorderPane = (BorderPane) primaryStage.getScene().lookup("#mainBorderPane");
        mainBorderPane.setPadding(new Insets(8));

        HBox controls = new HBox();
        Button loadBtn = new Button("Load Image");
        Button sobelEdgeDetectionBtn  = new Button("Sobel Edge Detection");
        loadBtn.setMaxWidth(Double.MAX_VALUE);
        sobelEdgeDetectionBtn.setMaxWidth(Double.MAX_VALUE);

        setBrowseFileAction(loadBtn);

        controls.setSpacing(8);
        controls.getChildren().addAll(loadBtn,sobelEdgeDetectionBtn);
        mainBorderPane.setBottom(controls);

        primaryStage.sizeToScene();
        primaryStage.show();
    }

    private void setBrowseFileAction(Button loadBtn) {
        loadBtn.setOnAction((event) -> {
            Object source = event.getSource();
            Scene scene = ((Node) source).getScene();
            Stage stageOfEvent = (Stage) ((Node) source).getScene().getWindow();
            BorderPane mainBorderPane = (BorderPane) scene.lookup("#mainBorderPane");
            ImageView imageView = new ImageView();

            FileChooser fileChooser = new FileChooser();
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("JPG","*.jpg"),
                    new FileChooser.ExtensionFilter("JPEG","*.jpeg")
            );
            fileChooser.setTitle("Choose file...");
            fileChooser.setInitialDirectory(
                    new File(System.getProperty("user.home"))
            );
            File file = fileChooser.showOpenDialog(stageOfEvent);

            if(file != null) {
                Image image = new Image("file:"+file.getAbsolutePath());
                imageView.setImage(image);
                mainBorderPane.setCenter(imageView);
                stageOfEvent.sizeToScene();

            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}

main.fxml file:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>

<BorderPane id="mainBorderPane" />

..not doing what I want.

I want to load an image and then resize the window and center it on the screen regardless of the image size.

Spent too much time on it. Any easy quick solutions ? I could do it by adding loads of code which I am sure the poor documentation for JavaFX already has a method that does what I want.

At the end of the if-statement in setBrowseFileAction add:

            stageOfEvent.setWidth(image.getWidth()+40);
            stageOfEvent.setHeight(image.getHeight()+40);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM