0

I am making a simple todolist app. Here is a watered-down version of it:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        ListView<Label> todoListView = new ListView();

        Label doneTodo = new Label ("This todo is meant to be finished and struck through");
        doneTodo.getStyleClass().add("done"); // Add the "done" class to this to-do
        Label undoneTodo = new Label("This todo is meant to be undone and therefore isn't struck through");

        // Add both to-dos to the listview
        addTodo(doneTodo, todoListView);
        addTodo(undoneTodo, todoListView);

        // Set the listview as the scene, and add the stylesheet
        Scene scene = new Scene(todoListView, 600, 550);
        scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());

        primaryStage.setTitle("Label not taking on Strikethrough");
        primaryStage.setScene(scene);
        primaryStage.show();


    }

    // Adds the to-do (in this case just a simple Label) to the listview
    private static void addTodo(Label todo, ListView<Label> todoList) {
        todoList.getItems().add(todo);
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Here is my CSS class:

.done {
    -fx-strikethrough: true;
}

When I run the code, the -fx-strikethrough property does not show up on my Label. Note that although this is a simplified version of my app, the issue remains the same: The text inside the JavaFX Label is not being struck through.

Again, sorry for any inadequacies in my question. I am fairly new to Stack Overflow!

Thanks in advance.

  • Is the `todo` node also added under `root`? Maybe show us that code. – john16384 Jul 11 '20 at 22:22
  • 1
    It's best to _create_ and provide a [mre] that demonstrates the problem (and only the problem). It allows us to see the entire flow of execution and the process of creating a minimal example may even lead you to the solution. – Slaw Jul 11 '20 at 22:54
  • 1
    Thanks for the tip! I'll see if I can cut down on the code a bit. – Ayaan Pathan Jul 11 '20 at 23:09
  • Only `Text` seems to provide the `-fx-strikethrough` property; `Label` does not (at least according to https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/doc-files/cssref.html) `.strikethrough Text { -fx-strikethrough: true; }` may work; not 100% sure if the default label skin internally uses a `Text` node... – fabian Jul 12 '20 at 08:32
  • _I'll see if I can cut down on the code a bit_ that's not enough to create an example as suggested by the referenced help page, please read it carefully and than act accordingly .. – kleopatra Jul 12 '20 at 10:28
  • You're right. I'll update it immediately. Sorry! – Ayaan Pathan Jul 12 '20 at 19:04
  • @fabian Is there some way for me to retrieve the text from my Label and add the strikethrough class to just that? – Ayaan Pathan Jul 12 '20 at 19:20

1 Answers1

0

The CSS rule should apply to the text node under the label node:

.done .text {
    -fx-strikethrough: true;
}
Dennis
  • 32,200
  • 11
  • 64
  • 79