How do I set the background image of a pane in JavaFX? - java

I tried giving the pane a background image through SceneBuilder CSS but that simply didn't work.
File NavImg = new File("Navigation.png");
Image NavigationImage = new Image(NavImg.toURI().toURL().toExternalForm());
BackgroundImage NavigationBack = new BackgroundImage(NavigationImage, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT);
Navigation.setBackground(new Background(NavigationBack));
Now I tried this code but it doesn't work either, what do I do?
Navigation is correctly selected in the Controller class. It being the fx:id of the Pane I want to change the background image.
#FXML
private Pane Navigation;

Related

Adding buttons to a scroll pane

I am currently using Scene Builder and to design my FXML UI in Java. However when loading the scene, I would like to loop through a user's devices and display them as buttons inside a scroll pane:
public DeviceSelectorController() {
for (Device device : OrderBird.getInstance().getProfileManager().getActiveProfile().getDevices()) {
Label label = new Label(device.getName());
Button button = new Button(device.getName());
listView.getItems().add(device.getName());
}
}
I cannot do this through Scene Builder as far as I know because I cannot access the cached HashMap.

Using TabPane in JavaFX

I'm switching from using a flowpane to a tabpane. I have no idea how to.
I want to use a canvas and a pane within the tabpane is this possible?
Below is my code without all the styling etc.
public View(TabPane root) {
this.root = root;
tab1 = new Tab();
root.getTabs().add(tab1);
tab2 = new Tab();
root.getTabs().add(tab2);
ui = new Pane();
canvas = new Canvas(600,600);
//gc is graphics context
gc = canvas.getGraphicsContext2D();
//this is where the problem is??
//i have no idea how to add ui and canvas to my tab1
tab1.getChildren().addAll(ui, canvas);
}
Tab is not a subclass of Pane, so it has no getChildren() method.
Instead, it has a content property whose value is the node displayed in the tab (note there is only one node in a tab).
So you can display the canvas in the tab with
tab1.setContent(canvas);
and of course if you have two things to display, you would put them in some other container and set the content of the tab to the container:
VBox vbox = new VBox(ui, canvas);
tab1.setContent(vbox);

JavaFX: Creating a button resets my stage/scene/panes background color

I'm making a Javafx game with a pane to hold my elements. The background color is set to black (instead of the default white) using the Scene.setFill(); method.
public class backgroundColor extends Application{
#Override
public void start(Stage stage) throws Exception {
Pane background = new Pane();
Scene scene = new Scene(background, 800, 600);
scene.setFill(Color.BLACK);
//Button button = new Button("Just a button");
stage.setScene(scene);
stage.show();
}
}
This works fine and the background is displayed as black... However, when I un-comment the button line, the fill color is suddenly displayed as white. Notice that the button is only initialized, it isn't being used, eclipse even gives me the warning that the data field "button" is unused.
Is this a weakness of Scene.setFill()? Or is this a unintended feature of creating a controller?
I am working on a game where I switch between different displays by changing the scene's "root" property using Scene.setRoot(), and before I created a button the color was fine no matter how many times I set different panes as the scene's new root node. However, after I created the button I discovered that I had to use a significantly more complicated workaround:
background.setBackground(new Background(new BackgroundFill(Color.BLACK, null, null)));
to fix the issue. Is there a better way to fix the background for the scene, or do I really have to set the background manually for all panes?

Why image disappears from titled pane after mouse event?

I have a titled pane, and I'm displaying an image in this pane, but the image is very big so I resize it, to fit the pane. Since the image becomes smaller, the user might want to view it in original size, so I implemented a method on mouse click opening the original image in new bigger window.
imgViewPicture.setFitWidth(titledPanePicture.getPrefWidth());
imgViewPicture.setPreserveRatio(true);
imgViewPicture.setSmooth(true);
imgViewPicture.setCache(true);
titledPanePicture.setContent(imgViewPicture);
The problem is, when I click on titledPanePicture the small image disappears, its content is cleared. Why? For the solution I added a reloading of the content on mouseclick event. But I dont like it, I think the content should stay the same for titledPanePicture.
Here is my mouseclick event:
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
Group root = new Group();
Scene scene = new Scene(root);
scene.setFill(Color.BLACK);
HBox box = new HBox();
imgViewPicture.setFitWidth(0);
box.getChildren().add(imgViewPicture);
root.getChildren().add(box); // before tPane
stage.setTitle("Picture Original Size");
stage.setScene(scene);
stage.sizeToScene();
stage.show();
reloadContentofTitledPane(); // This line is the fix but I find it unnecessary
Tips would be appreciated.
You seem to use the same ImageView in both scenes. However a Node can only be placed at a single place in a single scene graph.
Create a new node for your new Scene.
ImageView newImageView = new ImageView(imgViewPicture.getImage());
...
box.getChildren().add(newImageView);

Javafx BorderPane Force Overlap

I have this code (simplified/shortend/pseudo):
StackPane background = new StackPane();
GridPane overlay = new GridPane();
BorderPane pane = new BorderPane(background);
pane.setLeft(overlay);
I want the background/Stack pane to cover the entire window (in the background, surprise), and the overlay/Grid to cover a part of the background. The issue is that when I add an ImageView to the StackPane, the image shows up and nothing else. The overlay is not visible. I have tried overlay.toFront, overlay.preferredHeight, and background.toBack, with no success.
Try putting the border pane into the stack pane, instead of the other way around, and then making sure you add the image as the first element of the stack pane's children.
GridPane overlay = new GridPane();
BorderPane pane = new BorderPane();
pane.setLeft(overlay);
StackPane background = new StackPane(pane);
// ...
ImageView imageView = ... ;
background.getChildren().add(0, imageView);
// ...
scene.setRoot(background);
Alternatively, just use a BorderPane as the root and add the nodes in the usual way. Then, in an external style sheet, do
.root {
-fx-background-image: url(relative/path/to/image);
}

Categories