Set StackPane background image - javafx

I want to set background image autoresized with StackPane.
I tried this:
StackPane stackPane = new StackPane();
Image im = new Image(Dashboardpanel.class.getResource("/images/11.jpg").toExternalForm());
stackPane.setBackground(new Background(new BackgroundImage(im, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));
But the image is not auto-resized when I resize the StackPane. The image is repeated to fill the gap. How I can resize the image just as the Stackpane no matter how I extend the StackPane?

Don't use the default BackgroundSize, create a new BackgroundSize and use the contain and cover arguments of the BackgroundSize constructor.
For example (and I just wrote this by reading the javadoc, I didn't test it):
new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, false, true);
I advise using a CSS stylesheet for this kind of styling rather than coding the style in Java.

Related

BackgroundImage Javafx Combobox

is there a way to set an Image (like setGraphic() for Button) for a ComboBox?
file = new File(IMG_DIR + "load.png");
BackgroundImage bg = new BackgroundImage(new Image(file.toURI().toString()),BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
BackgroundSize.DEFAULT);
comboBox.setBackground(new Background(bg));
That's what i tried, but it only results in a black background.
As comparison, what i do with my buttons:
File file = new File(IMG_DIR + "save.png");
ImageView imageView = new ImageView(new Image(file.toURI().toString()));
buttonSave.setGraphic(imageView);
what it currently looks like:
I want a load image on the ComboBox positioned like the images on the Buttons. Is that possible?
Thanks!

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

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?

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.

How to set image to a scene background in JavaFX?

The title says it all, I guess.
How to set a .jpg (or any other supported image format) image to a Scene background?
I somewhat achieved this by using a HBox, an Image and an ImageView, like this:
String url = ...
HBox box= new HBox();
Image x = new Image(url);
ImageView iv = new ImageView(x);
box.getChildren().add(iv);
box.setVisible(true);
Then I add that box to the Scene first, and everything else afterwards.
I'm not complaining about that piece of code - it works for my purposes - but is there a proper way to set a background?
An ImagePattern can be used as fill of the Scene:
ImagePattern pattern = new ImagePattern(myImage);
scene.setFill(pattern);
Just call setBackground on the root node of the scene. For example:
Pane root = ... ; // probably some pane subclass...
String url = ... ;
Image img = new Image(url);
BackgroundImage bgImg = new BackgroundImage(img,
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false));
// put stuff in root as normal....
Scene scene = new Scene(root);
See the Javadocs for the parameters for the BackgroundImage constructor, etc.

Put text in background of JavaFX node

This stackoverflow answer explains how to put a background image inside a TextField. However, I would like to know how to put arbitrary text as background, and do it without CSS, if possible. My first idea was to take a snapshot of Text node and use it as a background image, as follows (I am doing this inside a constructor of a class extending TextField):
Text text = new Text("TEST");
text.setFill(Color.RED);
WritableImage image = text.snapshot(new SnapshotParameters(), null);
this.setBackground(new Background(
new BackgroundImage(
image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
new BackgroundPosition(Side.LEFT, 3D, false, Side.TOP, 50, true),
new BackgroundSize(image.getWidth(), image.getHeight(), false, false, false, false))));
However, it doesn't work. Background gets completely grayed out, and even the border disappears. Any idea how to do this? CSS solution is also acceptable.
I figured it out. Sometimes it helps to just put the question here, it gets you thinking. :-)
There were three problems. First, I had to use existing background as a base for new background. Second, I used vertical position incorrectly, I thought if you set it to 50% it will center it vertically, but apparently not. And finally, you can't do this in constructor because background doesn't exist yet, so I had to wrap it inside Platform.runLater(). Here is the revised code.
Platform.runLater(new Runnable() {
#Override
public void run() {
Text text = new Text("TEST");
text.setFill(Color.RED);
WritableImage image = text.snapshot(new SnapshotParameters(), null);
double imageVPos = (getHeight() - image.getHeight()) / 2;
BackgroundImage backImage = new BackgroundImage(
image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
new BackgroundPosition(Side.LEFT, 7D, false, Side.TOP, imageVPos, false),
new BackgroundSize(image.getWidth(), image.getHeight(), false, false, false, false));
List<BackgroundImage> images = new ArrayList<BackgroundImage>();
images.add(backImage);
setBackground(new Background(getBackground().getFills(), images));
}
});

Resources