Skip to content

Instantly share code, notes, and snippets.

@james-d
Created March 22, 2016 16:43
Show Gist options
  • Save james-d/846eb9ff72bd66fdd955 to your computer and use it in GitHub Desktop.
Save james-d/846eb9ff72bd66fdd955 to your computer and use it in GitHub Desktop.
Demo of using CSS pseudoclasses to provide strikethrough in labels
.root {
-fx-padding: 16 ;
-fx-vgap: 4 ;
}
.label:withdrawn .text {
-fx-strikethrough: true ;
}
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class LabelsWithStrikethrough extends Application {
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
List<Candidate> candidates = loadCandidates();
PseudoClass withdrawn = PseudoClass.getPseudoClass("withdrawn");
for (int row = 0; row < candidates.size(); row++) {
Candidate c = candidates.get(row);
Label label = new Label(c.getName());
label.pseudoClassStateChanged(withdrawn, c.isWithdrawn());
c.withdrawnProperty().addListener(
(obs, wasWithdrawn, isNowWithdrawn) -> label.pseudoClassStateChanged(withdrawn, isNowWithdrawn));
Button button = new Button("Withdraw");
button.setOnAction(e -> c.setWithdrawn(true));
button.disableProperty().bind(c.withdrawnProperty());
root.addRow(row, label, button);
}
Scene scene = new Scene(root);
scene.getStylesheets().add("labels-with-strikethrough.css");
primaryStage.setScene(scene);
primaryStage.show();
}
private List<Candidate> loadCandidates() {
return Arrays.asList(new Candidate("Ted Cruz", false), new Candidate("John Kasich", false),
new Candidate("Donald Trump", false), new Candidate("Marco Rubio", true),
new Candidate("Ben Carson", true), new Candidate("Jeb Bush", true),
new Candidate("Jim Gilmore", true), new Candidate("Chris Christie", true),
new Candidate("Carly Fiorina", true), new Candidate("Rick Santorum", true),
new Candidate("Rand Paul", true), new Candidate("Mike Huckerbee", true),
new Candidate("George Pataki", true), new Candidate("Lindsey Graham", true),
new Candidate("Bobby Jindal", true), new Candidate("Scott Walker", true),
new Candidate("Rick Perry", true), new Candidate("Hillary Clinton", false),
new Candidate("Bernie Sanders", false), new Candidate("Martin O'Malley", true),
new Candidate("Vermin Supreme", true), new Candidate("Lawrence Lessig", true),
new Candidate("Lincoln Chaffee", true), new Candidate("Jim Webb", true));
}
public static class Candidate {
private final String name;
private final BooleanProperty withdrawn = new SimpleBooleanProperty();
public Candidate(String name, boolean withdrawn) {
this.name = name;
setWithdrawn(withdrawn);
}
public String getName() {
return name;
}
public final BooleanProperty withdrawnProperty() {
return this.withdrawn;
}
public final boolean isWithdrawn() {
return this.withdrawnProperty().get();
}
public final void setWithdrawn(final boolean withdrawn) {
this.withdrawnProperty().set(withdrawn);
}
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment