0

I have sucessfully made the components responsive, then get bigger and smaller when the window is stretched. But the problem is whenever the window is resized, the components also gets resized and they look horrible. Hereby I have posted my code. These lines actually scale the components to the size of window:

contentRootRegion.scaleXProperty().bind(scene.widthProperty().divide(origW));
contentRootRegion.scaleYProperty().bind(scene.heightProperty().divide(origH));

Is there an way to make the javafx components responsive and also save their preserved ration or their maximum size? Thank you!

public class ResizingButtons extends Application {
    Controller controller;
    Scene scene;
    Stage stage;

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        controller = (Controller) replaceSceneContent(
                "Test.fxml");
    }

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

    public Initializable replaceSceneContent(String fxml) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = this.getClass().getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(this.getClass().getResource(fxml));

        Pane contentRootRegion;
        try {
            contentRootRegion = (Pane) loader.load();
        } finally {
            in.close();
        }
        double origW = 452;
        double origH = 526;
        if (contentRootRegion.getPrefWidth() == Pane.USE_COMPUTED_SIZE)
            contentRootRegion.setPrefWidth(origW);
        else
            origW = contentRootRegion.getPrefWidth();
        if (contentRootRegion.getPrefHeight() == Pane.USE_COMPUTED_SIZE)
            contentRootRegion.setPrefHeight(origH);
        else
            origH = contentRootRegion.getPrefHeight();
        // Wrap the resizable content in a non-resizable container (Group)
        Group group = new Group(contentRootRegion);
        // Place the Group in a StackPane, which will keep it centered
        StackPane rootPane = new StackPane();
        rootPane.getChildren().add(group);

        // Scene scene = new Scene(page);

        final Scene scene = new Scene(rootPane, 300, 700);

        // Bind the scene's width and height to the scaling parameters on the group
        //These two line of codes make the change its height and 
        // width when the frame is resized 
        contentRootRegion.scaleXProperty().bind(scene.widthProperty().divide(origW));
        contentRootRegion.scaleYProperty().bind(scene.heightProperty().divide(origH));
        stage.setScene(scene);
        stage.centerOnScreen();
        stage.show();
        scene.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable,
                                Number oldValue, Number newValue) {
                try {
                    System.out.println("Scene Width Property is " + scene.getWidth());
                    System.out.println(controller);
                    controller.resizeComponents();
                } catch (Exception e) {
                    System.out.println("Error in width listener of scene : " + e.getMessage());
                }
            }
        });
        return (Initializable) loader.getController();
    }
}
Vy Do
  • 46,709
  • 59
  • 215
  • 313
viper
  • 714
  • 1
  • 11
  • 26
  • The controls don't get re-sized on re-sizing the Stage, only Layouts are re-sized. Please clarify your question and try to create a [MVCE](http://stackoverflow.com/help/mcve) which depicts your problem – ItachiUchiha Sep 17 '14 at 10:33
  • no I am not saying that the controls are resized, the components are resized. While resizing the frame, the buttons inside it resizes accordingly I want to make the buttons resize but also save their height and width ratio – viper Sep 17 '14 at 11:05
  • Take a look at [JavaFX automatic resizing and button padding](http://stackoverflow.com/questions/23229149/javafx-automatic-resizing-and-button-padding) or [JavaFX fullscreen (scaling)](http://stackoverflow.com/questions/16606162/javafx-fullscreen). Even with your code, I'm not exactly sure what you are asking and why you are using scaling to resize the scene elements rather than using layout managers to relocate your items responsively to the scene size. – jewelsea Sep 17 '14 at 21:06
  • Yes I can use layout managers to make the components responsive but while stretching, it will look ridiculuous Here are the images of my application one is normal and another is when the jframe is resized horiziontally. This kind of responsiveness is fine but I want to preserve the ratio of the buttons. How do I do that. Original size : https://imagizer.imageshack.us/v2/318x383q90/540/i5gG3N.png Stretched size : https://imagizer.imageshack.us/v2/611x383q90/902/iUcPUi.png – viper Sep 18 '14 at 05:29

0 Answers0