How add padding or margin for ImageView in JavaFX by using code (not FXML) - javafx

I want add white border around ImageView.
One solution is to wrap the images in a Button.
And I try use StackPane:
StackPane stackPaneforImageActivity = new StackPane();
Image activityImage = new Image(file.toURI().toString());
ImageView imv = new ImageView(activityImage);
newActivityHBox.getChildren().add(stackPaneforImageActivity);
stackPaneforImageActivity.getChildren().add(imv);
stackPaneforImageActivity.setPadding(new Insets(10));
stackPaneforImageActivity.setStyle("-fx-border-color:white;-fx-background-color: black;");
imv.setFitHeight(30);
imv.setFitWidth(30);
But
Are there other solutions?
But the image is outside the StackPane. Why?

Related

JavaFX Layout issue within hbox

I want to add an statusbar to my application (the root pane is a vbox and the statusbar is a hbox with a fix height). On this statusbar I have a label with something like "2 processes running". As soon as the mouse hoovers this label, I want to add a Pane above this label with some details about the processes (like IntelliJ or Eclipse).
My problem in the moment is how to create this pane and position this pane above the label.
I create a simple example of my problem
The green area is a StackPane on the root VBox with VGow = Always. The red area is the Hbox with a fix height of 30 pixel. Then I added the yellow VBox the the HBox and put the minHeight to 300.
The problem is, that the yellow area should be above the red area (over the green area) and not outside the window.
What is the best way to achive something like that?
The only way I figured out was using a negative top margin amount (- max/min height). But then the window gets stretched because the yellow pane (or the green bordered pane is not on top of the other elements). The red crossed area shouldn't be there. The green boxed area should be above the other content. Probably I can't use a vbox as my root element?
Update 1
Here is an example - strange thing is, that it is working in this standalone example. But is that the way I should do something like that?
VBox rootBox = new VBox();
rootBox.setMaxHeight(500);
rootBox.setStyle("-fx-background-color: lightgreen");
StackPane contentPane = new StackPane();
contentPane.getChildren().add(new Button("Dont click me"));
contentPane.setStyle("-fx-background-color: lightblue");
VBox.setVgrow(contentPane, Priority.ALWAYS);
HBox statusbar = new HBox();
statusbar.setMinHeight(30);
statusbar.setMaxHeight(30);
statusbar.setStyle("-fx-background-color: red");
VBox processIndicatorBox = new VBox();
processIndicatorBox.setMinHeight(30);
processIndicatorBox.setMaxHeight(30);
HBox.setMargin(processIndicatorBox, new Insets(-300, 0, 0, 0));
StackPane processListPane = new StackPane();
processListPane.setStyle("-fx-background-color: yellow");
processListPane.setMinHeight(300);
processListPane.setMaxHeight(300);
processListPane.setMinWidth(150);
processListPane.setMaxWidth(150);
processListPane.setVisible(false);
Label label = new Label("Show processes");
label.setOnMouseEntered(mouseEvent -> processListPane.setVisible(true));
label.setOnMouseExited(mouseEvent -> processListPane.setVisible(false));
processIndicatorBox.getChildren().addAll(processListPane, label);
statusbar.getChildren().add(processIndicatorBox);
rootBox.getChildren().addAll(contentPane, statusbar);
Scene scene = new Scene(rootBox, 600, 500);
primaryStage.setScene(scene);
primaryStage.show();
Many greetings
Hauke
Did you consider using GridPane? ( https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/GridPane.html ). Gives you more control to layout elements with different sizes. VBox and HBox are from my experience good when all elements have similar sizes.
Here a pseudo-pseudo code
GridPane grid = new GridPane();
Pane greenBox = new Pane(); //your green box
Pane redBox = new Pane(); //your red box
Pane yellowBox = new Pane(); //your yellow box
// add the panes to the grid pane and define where they are
grid.add(greenBox, 0, 0); set the green box in column 0 and row 0
grid.add(redBox, 0, 1); set the red box in column 0 and row 1
grid.add(yellowBox, 1, 1); set the yellowbox in column 1 and row 1
// size of the boxes
GridPane.setColumnSpan(greenBox,2); //The green box should span over 2 columns
GridPane.setRowSpan(yellowBox,2); //The red box should span over 2 rows
The boxes are now only empty panes, which will have a minimum size without content. Replace the Pane() boxes with the content you want to put in or put the content in the Pane() objects.

To make a pane scrollable in JavaFX

Can you please tell me, how can I make a pane scrollable? I am able to make an image scrollable, like this:
VBox root = new VBox();
ScrollPane sp = new ScrollPane();
sp.setContent(new ImageView(new Image(getClass().getResourceAsStream("test.png"))));
root.getChildren().add(sp);
But when I am trying to make a whole pane scrollable, which consists of some Buttons and a small animation, there happens nothing regarding scrollbars. There are no scrollbars. How can I solve that?
Thank you in advance.
I tried to do it by myself, and it is because your scroll pane has fixed width and height, by resizing window, your scroll pane doesn't resize on itself. You can use anchor pane so scroll pane will resize with anchor pane. You could do something like this:
ImageView image = new ImageView(new Image("wp1951596.jpg"));
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(image);
scrollPane.setPrefSize(400, 400);
AnchorPane.setTopAnchor(scrollPane, 0.);
AnchorPane.setRightAnchor(scrollPane, 0.);
AnchorPane.setBottomAnchor(scrollPane, 0.);
AnchorPane.setLeftAnchor(scrollPane, 0.);
rootPane.getChildren().add(scrollPane);
I would also recommend you using JavaFX with .fxml files. Then you can create your layout in SceneBuilder and it will make everything easier and faster.

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.

JavaFX borderpane.setCenter replaces entire scene

I'm trying to load a FXML into a FXML. The main FXML has a borderpane, and the second contains a VBox. I load both of these with the FXMLLoader. When I try to set the center of the boarderpane, the entire screen gets replaced with the second FXML. Here is the code
BorderPane riskAnalysis = new BorderPane((BorderPane) FXMLLoader.load(getClass().getResource("./proposaldevelopment/riskAnalysis.fxml")));
VBox center = new VBox((VBox) FXMLLoader.load(getClass().getResource("./proposaldevelopment/openRiskAssessmentVbox.fxml")));
riskAnalysis.setCenter(center);
root = riskAnalysis;
stage.getScene().setRoot(root);
Any ideas that could cause this? I also tried making a new label and putting that into the borderpane center with the same result.
Try as
BorderPane riskAnalysis = (BorderPane) FXMLLoader.load(getClass().getResource("./proposaldevelopment/riskAnalysis.fxml"));
VBox center = (VBox) FXMLLoader.load(getClass().getResource("./proposaldevelopment/openRiskAssessmentVbox.fxml"));
riskAnalysis.setCenter(center);
stage.getScene().setRoot(riskAnalysis);

How can I copy an image from a stackPane and then add it to another stackpane?

I need to copy an image from a stackPane and then need to add that particular image to another stackpane with help of ClipBoard.
You can do it with SnapshotParameters and with a ImageView:
...
SnapshotParameters snapParams = new SnapshotParameters();
snapParams.setFill(Color.RED); // see documentation
// with your describtion YOURNODE is your stackpane...
imageview.setImage(YOURNODE.snapshot(snapParams, null));
Now you can add imageview to the other stackpane with:
otherStackpane.getChildren().add(imageview);

Resources