Spinner within gridpane has strange resize effects - javafx

My goal is to have a GridPane where every item is grown to its max size. When I use the Spinner component, however, there are some unexplainable resize effects: whenever the Spinner component gets focus, or the value changes, it grows a bit, until the other component is on its minimum size.
Code snippet:
ChoiceBox<String> box1 = new ChoiceBox<>();
box1.getItems().add("Item 1");
box1.setMaxWidth(Double.MAX_VALUE);
Spinner spinner = new Spinner(1, 365, 1);
spinner.setMaxWidth(Double.MAX_VALUE);
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.setPadding(new Insets(5));
gridPane.addRow(0, box1, spinner);
GridPane.setHgrow(box1, Priority.ALWAYS);
GridPane.setHgrow(spinner, Priority.ALWAYS);
Scene scene = new Scene(gridPane, 1000, 400);
primaryStage.setTitle("Spinner in GridPane");
primaryStage.setScene(scene);
primaryStage.show();
Can anyone explain me why this is happening and how I can get rid of it?

According to the JDKBug, it is fixed and going to be available in JAVA9

Related

JavaFX undecorated Window with shadow

I am trying to figure out, how to create an undecorated window with its regular shadow behaivor (like for all windows when using Windows).
I read several articles but non of them really worked.
As far as I understood, I have to create the main stage with some kind of padding. This stage has to have a transparent background. The actual content needs to be placed on some kind of other node.
So I took a stackpane for my root element and placed a vbox on it. The vbox should by the actual main stage area (I colored that area in green).
But I tried to use the StageStyle to transparent, I tried to fill out the stackpane with a transparent background but non of them worked. Also the shadow doesn't work as expected (I removed the shadow experiment in my example).
private void createPopup2() {
StackPane stackePane = new StackPane();
VBox rootPane = new VBox();
rootPane.setStyle("-fx-background-color: green; -fx-border-color: black; -fx-border-width: 1px;");
stackePane.getChildren().add(rootPane);
stackePane.setPadding(new Insets(20, 20, 20, 20));
Scene scene = new Scene(stackePane);
final Stage stage = new Stage();
stage.initStyle(StageStyle.TRANSPARENT);
stage.setWidth(600);
stage.setHeight(350);
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
}
I am quite confused and have no idea how to fix that.
Firstly I am not sure what type of shadow you are expecting. It will be helpful if you can provide the example for shadow you tried, so that we may know the actual issue.
Having said that, have you tired using -fx-effect on VBox.? The below code creates a shadow effect around the green box.
StackPane stackePane = new StackPane();
stackePane.setStyle("-fx-background-color:transparent");
VBox rootPane = new VBox();
rootPane.setStyle("-fx-background-color: green; -fx-border-color: black; -fx-border-width: 1px;-fx-effect: dropshadow(gaussian, rgba(0, 0, 0, .75), 20, 0.19, 0, 0);");
stackePane.getChildren().add(rootPane);
stackePane.setPadding(new Insets(20, 20, 20, 20));
Scene scene = new Scene(stackePane, Color.TRANSPARENT);
final Stage stg = new Stage();
stg.initStyle(StageStyle.TRANSPARENT);
stg.setWidth(600);
stg.setHeight(350);
stg.setScene(scene);
stg.initModality(Modality.APPLICATION_MODAL);
stg.show();
You can tweak the parameters of the drop-shadow to get your desired effect.
The doc related to parameters is as below :

Javafx Scrollpane is being shown even when it's not needed

I have this Javafx element
HBox dosWrap = new HBox();
dosWrap.setMinHeight(400);
dosWrap.setMaxHeight(452);
dosWrap.setMinWidth(900);
VBox todo = new VBox();
todo.setMinWidth(300);
todo.setMinHeight(400);
ScrollPane sp = new ScrollPane();
sp.setContent(todo);
sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
dosWrap.getChildren().add(sp);
The scroller is being shown and works,but my problem is that scroller is being shown even when it's not needed.How do i stop that?

LineChart JavaFX -saving to png - How to control the size

I cannot figure out how to control the size of the LineChart saved to png. The chart looks great on the screen, in it's own window, but it gets scrunched vertically when saved. I've tried to figure out how to control the size, but to no avail. It must be a simple setting, but...
Any help would be appreciated.
With the following code, excluding the code that sets the data values:
LineChart<Number, Number> xyChart = new LineChart<>(xAxis, yAxis);
StackPane layout = new StackPane();
layout.setPadding(new Insets(10, 10, 10, 10));
xyChart.prefWidthProperty().bind(window.widthProperty());
xyChart.prefHeightProperty().bind(window.heightProperty());
layout.getChildren().add(xyChart);
VBox mainVBox = new VBox();
mainVBox.getChildren().addAll(menuHBox, layout);
Scene scene = new Scene(mainVBox, 800, 600);
window.setScene(scene);
xyChart.setAnimated(false);
WritableImage snapShot = scene.snapshot(null);
try {
ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null), "png", new File("test3.png"));
} catch (IOException e) {
}
The chart as displayed on the screen is
window grab
whereas the png file that is written is
upload of the png file
Try saving the image after the window is shown, as layouts and sizes are computed on a required basis, and may not give the same values before.
Another solution would - of course - be to explicitly set the preferred size of the node.
xyChart.setPrefWidth(...);
xyChart.setPrefHeight(...);
The reason why bindings don't help is because the window itself doesn't have the computed size yet, as previously said.

Resizeable Gridpane or other container

Hi I'm trying to create a simple layout that looks like this using JavaFX.
I would like the user to be able to drag/resize the middle bar. I've tried to make this using a GridPane. But I can't figure out how to get the middle resized. Perhaps GridPane is not the way to go. Both panes will contain a lot of other program code later on. Thanks!
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stageRef.setMaxWidth(primaryScreenBounds.getWidth());
stageRef.setMaxHeight(primaryScreenBounds.getHeight());
GridPane gridPane = new GridPane();
gridPane.setGridLinesVisible(true);
gridPane.setPadding(new Insets(10));
Scene scene = new Scene(gridPane);
VBox vBoxMain = new VBox();
vBoxMain.setPrefWidth((primaryScreenBounds.getWidth()/5)*4);
vBoxMain.setPrefHeight(primaryScreenBounds.getHeight());
TextArea textArea = new TextArea();
textArea.setWrapText(true);
textArea.setPrefHeight(primaryScreenBounds.getHeight());
vBoxMain.getChildren().addAll(textArea);
vBoxMain.isResizable();
VBox vBoxSide = new VBox();
vBoxSide.setPrefWidth((primaryScreenBounds.getWidth()/5));
vBoxSide.setPrefHeight(primaryScreenBounds.getHeight());
vBoxSide.isResizable();
gridPane.add(vBoxSide, 1,1,1,1);
gridPane.add(vBoxMain, 2,1,4,1);
stageRef.setScene(scene);
stageRef.show();
You could use a SplitPane:
A control that has two or more sides, each separated by a divider,
which can be dragged by the user to give more space to one of the
sides, resulting in the other side shrinking by an equal amount.
Then you add two others containers to this pane allowing the user to change the position of the divider. You can also set minimum widths for each component within the pane or set the position of each divider within your code.

MenuBar to top of the app

I'm quite new to JavaFX and I have two methods - one returning a grid layout and the other one returning a HBox with a MenuBar, but fot the life of me I can't make it, so it's not overlapping (I want the grid to be a few pixels lower). I have this code in my start method:
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup);
rootGroup.getChildren().add(addBar(stage.widthProperty()));
rootGroup.getChildren().add(addGridPane());
stage.setScene(scene);
stage.show();
How do I fix this?
You may use another Layout Manager as root. For example:
final VBox rootGroup = new Vbox();
Then all its children will be aligned vertically.

Resources