JavaFX-CSS selector

  Preface

In CSS, selectors are a mode for selecting elements that need to be styled. There are four types of selectors in JavaFX CSS. The editor will explain them one by one below.

1. Type Selector (TypeSelector)

Most JavaFX control classes correspond to a CSS Type. We can use the type selector to control the appearance of the type control. The corresponding naming is: change the JavaFX class name to lowercase first letter. If there are multiple words splicing the class name, lowercase the first letter of the original uppercase letter of each word and connect multiple words with hyphens. Such as:

JavaFX Class CSS Type class
Button button
Label     label
CheckBox     check-box
TextField    text-field
...     ...

The usage of type selector is as follows:

.label{
     -fx-text-fill:red
}

In this way, we set all the label text styles in the FXML file to which this CSS file is applied to red.


Second, the class selector (Class selector)

Some controls in Javafx do not have their own type selectors. At this time, you need to define if you can use a class selector and set the styles for the corresponding controls. The class selector can be applied to multiple controls, as long as you add StyleClass to the corresponding controls.

The usage of class selector is as follows:

.background-color{
     -fx-background-color:black;
}

This defines a background-color type selector. For any subclass of Node, there is a getStyleClass method, which can be passed through getStyleClass().add("background-color");, or through the FXML file The StyleClass tag adds the style of the background-color selector. The class selector will override the style selected by the type.


Three, ID selector

The ID selector is defined by #. Generally, an ID selector is unique to each control.

The usage of ID selector is as follows:

#ok-button{
   -fx-font-color:rgb(255,0,0,0);
   -fx-font-size:25px;
}

At this time, the Button whose ID is okbutton can change the style through this ID selector.


Four, pseudo-class selector (CSS Pseudo-class)

The syntax of pseudo-class is: selector: pseudo-class {property: value}

For example, we set the background color to change to gray when the mouse moves on the Button:

.button:hover{
   -fx-background-color:gray;
}

There are roughly the following types of pseudo-class selectors in JavaFx:

  • disabled
  • focused
  • selected
  • hover
  • empty
  • pressed
  • show-mnemonic

to sum up

The JavaFX CSS documentation is not very complete, and there are a lot of things to explore. You still have to use more experience in the project, so that practice makes perfect.

Intelligent Recommendation

Give JavaFX program with CSS

2019 Unicorn Enterprise Heavy Recruitment Python Engineer Standard >>> Skinning JavaFX Applications with CSS You can create a custom look, also called a skin, for your JavaFX application with...

JavaFX \ FXML \ CSS

Blogger pure amateur, not developers. The four files mentioned below: Main.java fxmlcontroller.java fxml file (.fxml) TEST.CSS 0.mvc framework Model View Controller In the classic MVC mode, m means th...

JavaFX CSS style

JavaFX CSS style JavaFX is entry from entry to the home series JavaFX CSS style, I said it in front of it similar to HTML, he has a CSS control style, but the latest CSS standard does not support, and...

JavaFX uses CSS style

By using the CSS style, we can make our JavaFX UI show more beautiful, set up in the CSS style: 1, direct settings 2, CSS introduction CSS style file: Project structure: final effect  ...

[css selector] selector specificity

Selector specificity Original link: CSS Authoritative Guide 3rd Edition, 69 pages - 73 pages Particularity There are many different ways to select elements. In fact, it is possible that the same eleme...

More Recommendation

[css selector] property selector

Attribute selector CSS2 introduces an attribute selector The attribute selector can select elements based on their attributes and attribute values. Simple attribute selection If you want to select an ...

[css selector] descendant selector

Descendant selector Original link: Descendant selector Descendant selector The descendant selector can choose the element that is the descendant of an element. Select elements based on context We can ...

[css selector] class selector

Class selector Original link:Class selector Class selectors allow styles to be specified in a way that is independent of document elements The class selector allows you to specify styles in a way that...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top