JavaFX CSS type based selectors - css

I'm having difficultly understanding JavaFX CSS type selectors. I've had a good look at the official documentation
If I want to customize the spacing of layout elements such as HBox and VBox using the -fx-spacing rule do I have to add a custom CSS class to them all manually before I can reference them??
hBox.getStyleClass().add("hbox")
vBox.getStyleClass().add("vbox")
There doesn't seem to be a type based selection like this:
hbox {
-fx-spacing: 5
}
HBox and VBox are documented as "Style class: empty by default" I'm used to HTML where you can select by element type, e.g. div, button etc. I've got a lot of layouts and I don't want to have to go through them all.

Have you tried with capital letters?
HBox {
-fx-spacing:10px;
}

Related

round control in javafx

I have a question. How can I make a circle checkbox in JavaFX? "-fx-background-radius" doesn`t work for me, also I want to when mouse is over checkbox to display a short describe for what this checkbox is create. How can I solved this? Thanks for helps.
For a CheckBox with rounded corners you would use CSS. It is helpful to look at the JavaFX CSS Reference Guide when you need to know what you can style from CSS. For instance, the section for CheckBox tells us there is a substructure; this includes a StackPane with the styleclass of box. It is this StackPane that is the "physical box" in the UI and what you want to round the corners of. In you're CSS file you'd have something like:
.check-box .box {
-fx-background-radius: /* enter you're desired radius here */;
}
Then you'd add the CSS file to your Scene via getStylesheets().add(...).
You also want to know how to display some information about the CheckBox when the mouse hovers over it, correct? To do this you would use a Tooltip.
CheckBox box = new CheckBox("Choose me!");
box.setTooltip(new Tooltip("I do something!"));

Is there a way to not display empty TreeCells in JavaFX?

My TreeView is displayed even though there are no TreeItems in it. Is there any way to not display the TreeCells before they actually have a corresponding TreeItem to display?
This is how it looks without any items:
This is how it looks when one item is added to the root:
Thanks for any suggestions :)
I think you can not prevent empty TreeCells being added to the TreeView, but if all you care about is appearance, you can define different style for empty cells. Empty cells have the :empty pseudo class, which you can use in your CSS:
.tree-cell:empty {
-fx-background-color: transparent;
}

get the Indexnumber of HBox element for Controller Class

I have an Hbox which contains Vboxes and each Vbox itself contains other Elements which can be added or removed and some control Buttons. Now I want to add or remove those flexible Elements in my Model and I need to know in which Vbox this is happening.
My attempt was to get the Children of the Hbox and check where the Vbox, where something happend/changed, is located in this List. And work with this Index afterwards.
public int getId (Action event){
Button button= event.getSource();
Vbox vbox= button.getParent();
Hbox hbox= vbox.getParent();
hbox.getChildrenUnmodifiable();
....//TODO
}
The Problem is that if I Print hbox.getChildrenUnmodifiable(); it shows me that:
[VBox#1402dd44, Separator#4eaff333[styleClass=separator], Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT]
and after I add an other Vbox it just adds the
Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT]
part again and again and doesen't show me any more information of the Vboxes which have been added.
I load the Vboxes from an other .fxml file with hbox.getChildren().add(FXMLLoader.load(getClass().getResource("/VBoxElement.fxml")));
Now I wonder if that looks like an proper way to get the Indexnumber of my Vbox where I am working on? And if anyone has any advice how to get a working Index of my Hbox?
This is my first JavaFX project and if I miss understood something feel free to correct me.
As James_D sayd:
ObservableList is just a subinterface of java.util.List, so you can
just call all the usual methods: indexOf(...), add(..), remove(...),
etc – James_D May 14 at 15:41
Thx, worked fine. As I expected it as first but I messed up my fxml structure and my added fxml did not contain a VBox as root element it contained a Grid. The toString() from the Grid produces the Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT in the output

How to highlight a column instead of a row in javafx TableView when hover?

How to highlight a column instead of a row in javafx TableView when hover or click on it?
I highly recommend that you use ScenicView for this. You can inspect the scene graph and see all the css attributes for each element in it. This will allow you to easily identify the proper css hierarchy to use to style just the column rather than the row.
Then you can apply the appropriate styles with css for the hover or clicked states using the following css event selectors:
:hover
:armed
:pressed
:disabled
etc...

Creating custom text editing component with JavaFX

I want to create a custom text editing component. Basically I need a text with an ability to set a caret and handle key events in a custom way. Is there a way to do so in JavaFX? There are implementation specific methods on Text (which are deprecated and comments say that they should be considered private), but I can't use them.
Is it possible to do so in JavaFX?
For single-lined text, use a TextField.
For multi-lined text, use a TextArea.
For a styled text, use a HTMLEditor.
If using a TextField or TextArea, they are both subclasses of TextInputControl. The TextInputControl provides a caretPositionProperty you can use to read and modify the caret position. All scene graph objects (including TextField and TextArea) are instances of Node. A node allows you to set custom EventHandlers and event filters. You can use an event handler to override the default key processing handler of the TextInputControls so that you can implement your own handling.
If you need further customization capability which you cannot get from customizing a TextField or TextArea instance, then you could create your own TextInputControl subclass and implement the custom text editing component as you need, but I wouldn't really advise that approach due to the complexity and possible required dependency on internal JavaFX apis that you would likely end up needing.
TextField doesn't work for me because: 1. It has a border which indicates focus and which I don't know how to remove.
Here is a link to the default caspian.css stylesheet for JavaFX 2.2.
You can set the the style for TextField to remove the focus border, by setting it's :focused pseudoclass selector style to the same values as it's default selector. For example:
.text-field:focused {
-fx-background-color: -fx-shadow-highlight-color, -fx-text-box-border, -fx-control-inner-background;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 3, 2, 2;
}
Refer to the JavaFX css reference guide for information on how to use css in JavaFX.
TextField doesn't work for me because: 2. It has fixed number of columns and I need to change width dynamically.
TextField is a resizable control, if you place it in a resizing layout pane with appropriate constraints it will change it's width dynamically. The number of columns setting in TextField is a preferred column count, not a fixed column count.
Refer to the JavaFX layout documentation on further information regarding layout in JavaFX.

Resources