rikirandon
rikirandon

Reputation: 1

JAVAFX two stages beside bug

I'm trying to display two Stages perfectly beside each other without space between them but I have this:

screen capture

I tried some alternatives but got the same problem. It seems that the second stage don't fill all the space provided. Any ideas?

public class Main extends Application {
    static final double STEP=10;
    static final double X_DIM=500;
    static final double Y_DIM=300;
    static final double SPACING=10;
    static final double RADIUS=50;
    static final Color FILL1= Color.BLUE;
    static final Color FILL2= Color.GREEN;
    static final Color FILL3= Color.RED;

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

    @Override
    public void start(Stage primarystage) throws Exception {
        Button moveCircle= new Button("Move Circle 1");
        Button addCircle= new Button("Add Circle 2");
        Button reset= new Button("Reset");
        Button exit= new Button("exit");
        reset.setDisable(true);

        Circle circle1= new Circle(X_DIM/2,Y_DIM/2,RADIUS,FILL1);
       
        StackPane root = new StackPane(circle1);
        VBox buttons= new VBox(moveCircle,addCircle,reset,exit);
        buttons.setPadding(new Insets(SPACING,SPACING,SPACING,SPACING));
        buttons.setSpacing(SPACING);
        buttons.setAlignment(Pos.CENTER);

        primarystage.setTitle("LAB4");
        primarystage.setScene(new Scene(root, 500, 300));
        primarystage.show();

        Stage stage2= new Stage();
        stage2.setTitle("stage2");
        stage2.setX(primarystage.getX()+primarystage.getWidth());
        stage2.setY(primarystage.getY());
        stage2.setScene(new Scene(buttons));
        stage2.show();
        stage2.setMinWidth(200);
    }
}

Upvotes: 0

Views: 45

Answers (1)

Abra
Abra

Reputation: 20914

Refer to the documentation for widthProperty
(Paraphrased excerpts.)

The width of this Window. This value includes any and all decorations which may be added by the Operating System such as resizable frame handles.

When you want to widen the stage by dragging its border with the mouse, the mouse pointer changes to a double-headed arrow which extends beyond the border and that is why you have a gap between the borders of the two Stages. Try using the Scene width, rather than the Stage width.

Consider the following code:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {
    static final double STEP=10;
    static final double X_DIM=500;
    static final double Y_DIM=300;
    static final double SPACING=10;
    static final double RADIUS=50;
    static final Color FILL1= Color.BLUE;
    static final Color FILL2= Color.GREEN;
    static final Color FILL3= Color.RED;

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

    @Override
    public void start(Stage primarystage) throws Exception {
        Button moveCircle= new Button("Move Circle 1");
        Button addCircle= new Button("Add Circle 2");
        Button reset= new Button("Reset");
        Button exit= new Button("exit");
        reset.setDisable(true);

        Circle circle1= new Circle(X_DIM/2,Y_DIM/2,RADIUS,FILL1);
       
        StackPane root = new StackPane(circle1);
        VBox buttons= new VBox(moveCircle,addCircle,reset,exit);
        buttons.setPadding(new Insets(SPACING,SPACING,SPACING,SPACING));
        buttons.setSpacing(SPACING);
        buttons.setAlignment(Pos.CENTER);

        primarystage.setTitle("LAB4");
        primarystage.setScene(new Scene(root, 500, 300));
        primarystage.show();

        Stage stage2= new Stage();
        stage2.setTitle("stage2");
        stage2.setX(primarystage.getX() + primarystage.getScene().getWidth()); // CHANGE HERE
        stage2.setY(primarystage.getY());
        stage2.setScene(new Scene(buttons));
        stage2.show();
        stage2.setMinWidth(200);
    }
}

This is what I get when I run the above code...

screen capture

Upvotes: 4

Related Questions