22

This small JavaFX test application

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ApplicationWithNonResizableStage extends Application {

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

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final Rectangle rectangle = new Rectangle(200, 100, Color.POWDERBLUE);
        final BorderPane pane = new BorderPane(rectangle);
        final Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}

produce a window with unwanted padding:

stage with margin

Removing the call primaryStage.setResizable(false) also removes the effect:

stage without margin

What is going wrong?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • Have you been able to find a solution to this? It's affecting other components as well: http://stackoverflow.com/questions/22620919/combobox-options-receive-margin-upon-making-the-stage-resizable – tvkanters Mar 24 '14 at 22:04
  • @TVK: I could only resolve the issue for the stage. – Jens Piegsa Mar 26 '14 at 09:10

3 Answers3

46

As already commented, this different behaviour of !/resizable smells like a bug (somebody might consider filing an issue ;-)

A shorter (than sizing manually) way around is to explicitly fit the stage to the scene:

primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.sizeToScene();

Just noticed that this works for jdk8, but not jdk7.

For convenience, a bug update: the original report filed by jewelsea was closed as a duplicate of (in new coordinates) https://bugs.openjdk.java.net/browse/JDK-8089008 - still open, commented to be win-only.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 4
    Created bug report [RT-36413 Initial stage size set incorrectly for non-resizable stages](https://javafx-jira.kenai.com/browse/RT-36413). – jewelsea Mar 27 '14 at 17:20
2

Although this is not explanation, it solves the problem:

@Override
public void start(final Stage primaryStage) throws Exception {
    final Dimension d = new Dimension(210, 110);
    final Rectangle rectangle = new Rectangle(d.width, d.height, Color.POWDERBLUE);
    final BorderPane pane = new BorderPane(rectangle);
    pane.maxWidth(d.height);
    pane.maxWidth(d.width);
    final Scene scene = new Scene(pane, d.width, d.height);
    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    primaryStage.setWidth(d.width);
    primaryStage.setHeight(d.height);
    primaryStage.show();
}

Key is setting width and height of the Stage at the right time.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
0

Try creating a dimension for the size you want the frame- Dimension name = new Dimension(height,width)- then set that Dimension size to the rectangle BorderPane and Scene. You do want everything to be the same size correct?

  • Thanks for your reply. Setting the sizes manually did not solve the problem as the stage also becomes larger than it should when introducing `setResizable(false)`. – Jens Piegsa Dec 22 '13 at 19:38
  • Try switching the order of 'setResizable(false)' and 'setScene(Scene)' it shoudln't matter but sometimes it does. – Mikhail_Bjornson Dec 22 '13 at 22:35