Javafx Apply CSS to element of Custom-Menu-Item - css

I have a context menu with a single custom-menu-item. The custom-menu-item consists of a pane which in turns holds a vbox.
My goal is to get rid of the blue highlighting. I have successfully got rid of blue highlighting with the context-menu. But cannot access the layer below it (the pane or vbox). The context menu has been assigned .context as a class name.
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/resources/fxml/ThePane.fxml"));
Pane pane = (Pane) fxmlLoader.load();
customMenuItem = new CustomMenuItem(pane);
.context .custom-menu-item:focused{
-fx-background-color:white;
}
.context .custom-menu-item{
-fx-background-color:white;
}

Related

JavaFX CSS for nested SplitPanes

I'm tasked with styling/skinning a JavaFX application. I'm trying to do as much in CSS stylesheets as possible rather than in the code. I've got a question about nested SplitPanes: when the application is rendered, is there anything that is created surrounding the inner SplitPane that gets in between the outer SplitPane and the inner SplitPane in the parent/child hierarchy of nodes.
Here's a little test application that mimics my full application:
BorderPane root = new BorderPane();
root.setId("root");
Label mws_header = new Label("This is the header");
root.setTop(mws_header);
Label sidebar = new Label("This is the sidebar");
root.setLeft(sidebar);
SplitPane outer = new SplitPane();
outer.setId("mws_pano_360_split");
outer.setOrientation(Orientation.VERTICAL);
root.setCenter(outer);
Label bigGraphic = new Label("This is the top");
outer.getItems().add(bigGraphic);
SplitPane inner = new SplitPane();
inner.setId("flat_data_split");
inner.setOrientation(Orientation.HORIZONTAL);
Label label1 = new Label("This is the left");
Label label2 = new Label("This is the right");
inner.getItems().addAll(label1, label2);
outer.getItems().add(inner);
In the CSS, what I'd like to do is get to the inner SplitPane using direct child syntax:
BorderPane > SplitPane > SplitPane {
/* all kinds of styling of background colors */
}
But instead, what I have to do is treat it as a descendant:
BorderPane > SplitPane SplitPane {
/* all kinds of styling of background colors */
}
So what might be getting in between the outer SplitPane and the inner SplitPane? I'd love to have a tool that lets you inspect the parent/child nodes in the hierarchy but so far haven't found one.

JavaFX accessing stackpane within borderpane

The center of my BorderPane has a stackpane called designView (an FXML defined stackpane). I'm trying to get a draggable pane in the designView. If I add that pane to the rootView (my BordePane) all is well. If however I try to add it to the designView like this:
// This works fine except the pnae is on the wrong place obviously
....
Pane p = displayField.createDraggablePane(800.0, 800.0, 400.0, 300.0);
rootView.getChildren().add(p);
// Now the pane is in the right place, but it's 'stuck'
....
rootView.setCenter(designView);
Pane p = displayField.createDraggablePane(800.0, 800.0, 400.0, 300.0);
designView.getChildren().add(p);
The pane appears correctly in the designView, BUT it is no longer draggable. The MouseEvents fire, but the position of the pane is not updated. I think the problem is with the fact that layoutX, getSceneX, layoutXProperty etc. have no reference to designView, but how do I get that? Can anyone help?
I found a solution. Instead of adding a StackPane to my BorderPane center, I have added a Pane and all is well :-) So my draggable Pane is added to a Pane instead of added to a StackPane.
Don't quite understand why this should change the behaviour of the draggable pane, but practice shows that it does.

JavaFX: How can I horizontally center an ImageView inside a ScrollPane?

viewScroll.setContent(new ImageView(bigimg));
double w = viewScroll.getContent().getBoundsInLocal().getWidth();
double vw = viewScroll.getViewportBounds().getWidth();
viewScroll.getContent().setTranslateX((vw/2)-(w/2));
viewScroll.toFront();
I set an ImageView with some Image inside the ScrollPane but the ImageView always goes to the far left corner. Here I'm trying to manually offset the difference, but it doesn't work well. The ImageView goes too far to the right plus it only updates once because it's inside the eventhandler for a button.
Here is an example using a label without the need for listeners:
public void start(Stage primaryStage) throws Exception {
ScrollPane scrollPane = new ScrollPane();
Label label = new Label("Hello!");
label.translateXProperty().bind(scrollPane.widthProperty().subtract(label.widthProperty()).divide(2));
scrollPane.setContent(label);
Scene scene = new Scene(scrollPane);
primaryStage.setScene(scene);
primaryStage.setWidth(200);
primaryStage.setHeight(200);
primaryStage.show();
}
I am not sure if you are familiar with properties or not, but in the code above, when I bind the translateXProperty to the formula, every time one of the dependencies changes, such as the ScrollPane's widthProperty or the Label's widthProperty, the formula is recalculated and translateXProperty is set to the result of the formula.
I am not sure, but in your previous code, it appears that the calculation code would be in a resize listener. This is not required when dealing with properties as they update whenever dependencies changed (note the bind() and not set()).

JavaFX: Menu items only shown as three dots when in BorderPane

I am trying to learn JavaFX and I've run into a problem with the Menus in my MenuBar. Here is a minimal example:
public void start(Stage mainStage) throws Exception {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 1200, 1000, Color.WHITE);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("_File");
menuBar.getMenus().add(menuFile);
MenuItem add = new MenuItem("_New");
menuFile.getItems().add(add);
root.getChildren().add(menuBar);
menuBar.prefWidthProperty().bind(mainStage.widthProperty());
mainStage.setScene(scene);
mainStage.show();
}
This application starts, but the Menu in the MenuBar is only shown as three dots (...). It does open however when I press ALT+F, so it is there.
From what I understand, a Menu item has no width or similar attribute, so that can't be set. I suspect it has something to do with my root node being a BorderPane, because in every other example I found that works, the root is either a VBox or something else. I seem to get the desired results when I place a Vbox as my root node, and then add the MenuBar and the BorderPane` to the root - but that seems like a strange and uneccesary workaround to me.
So what am I missing here? Is it true that a MenuBar will only ever look like it should in certain containers? What is the difference between a BorderPane and a VBox in this regard? If someone could explain or point me to a part of the documentation that I've missed, I would be very thankful.
You are using a BorderPane and using getChildren().add() to add MenuBar in it, which is incorrect. BorderPane unlike VBox, can't accept any number of children and is divided into 5 specific positions :
top
left
right
bottom
center
The children goes into any one of these positions. Please go through the documentation for BorderPane.
You need to add the Menubar to the top of the BorderPane using :
root.setTop(menuBar);

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);

Resources