I'm trying to make a list of boxes which a user can select through with their mouse and when one box is selected, it highlights it with a color, and all the rest of the boxes turn white. Is there an equivalent to the css tag :target in javafx like there is an equivalent to :focus(:focused) or do I have to handle selecting items in a list my own way?
2 Answers
There is no built-in target pseudoclass, but there is an API for creating your own CSS PseudoClass objects.
Here is a simple example:
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.css.PseudoClass;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SelectableBoxes extends Application {
private static final PseudoClass SELECTED_PSEUDOCLASS = PseudoClass.getPseudoClass("selected");
private ObjectProperty<Pane> selectedBox = new SimpleObjectProperty<>();
@Override
public void start(Stage primaryStage) {
VBox container = new VBox(5);
container.setPadding(new Insets(20));
int numBoxes = 5 ;
for (int i = 0 ; i < numBoxes; i++) {
container.getChildren().add(createBox());
}
ScrollPane scroller = new ScrollPane(container);
Scene scene = new Scene(scroller, 400, 400);
scene.getStylesheets().add("selectable-boxes.css");
primaryStage.setScene(scene);
primaryStage.show();
}
private Pane createBox() {
Pane pane = new Pane();
pane.setMinSize(50, 50);
pane.getStyleClass().add("box");
pane.setOnMouseClicked(e -> selectedBox.set(pane));
selectedBox.addListener((obs, oldSelection, newSelection) ->
pane.pseudoClassStateChanged(SELECTED_PSEUDOCLASS, newSelection == pane));
return pane ;
}
public static void main(String[] args) {
launch(args);
}
}
with the corresponding CSS file selectable-boxes.css:
.box {
-fx-border-width: 1 ;
-fx-border-color: black ;
}
.box:selected {
-fx-background-color: blue ;
}
Comments
This "answer" is to provide an alternate solution for the task you wish to accomplish (rather than directly answering your question regarding CSS tags).
For your task, you may wish to use ToggleButtons in a ToggleGroup, or a ListView.
Oracle provide a ToggleButton demo. By default a ToggleButton behaves a bit different from a radio button (e.g. it is possible to have nothing selected). If you want radio button style behavior, to ensure something is always selected, you can use the bit of code at: JDK-8090668 Need TogglePolicy for ToggleButton in ToggleGroup
toggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) {
if (toggle != null && new_toggle == null) {
toggle.setSelected(true);
}
}
});
The in-built controls can be styled quite extensively using CSS to get custom looks. Refer to the modena.css file for default css styles for the controls which can be overriden.
targetpseudoclass, but you can just create your own CSSPseudoClasson the fly.