Kishore
Kishore

Reputation: 300

How to set color, width and draw dashed line simultaneously for javafx line?

I have to draw multiple lines using javafx line chart. Since one line can overlap other lines. I want to use dashed line or line with different thickness to represent each line.

Below is my code snippet:-

    for (XYChart.Series<Number, Number> s : chart.getData()) {

        // Used for Line color
        if (("Current Threshold").equals(s.getName())) {
            s.getNode().setStyle("   -fx-stroke-width: 10; ");
            s.getNode().setStyle("-fx-stroke: #00FF00; ");
            s.getNode().setStyle("-fx-stroke-dash-array: 2 12 12 2; ");
        } 
        else if(some condition)
        {
        // Some other condition to draw other lines
        }
}

where chart is instance of LineChart.

Node.setStyle() methods override each other. I am unable to set multiple style together. Last style persists and others are overridden. ie for above sequence, dashed line is drawn. I am using css styles in java code.

Is there any way to apply multiple style, without overriding others.

Thanks

Upvotes: 2

Views: 6875

Answers (1)

eckig
eckig

Reputation: 11134

Node.setStyle() is, as the name should suggest, a setter method for the style attribute of a JavaFX Node.

By calling this method three times in a row, only the last invocation has an effect as the previous ones get overwritten.

So if you want to apply all three styles you should write:

node.setStyle("-fx-stroke-width: 10; -fx-stroke: #00FF00; -fx-stroke-dash-array: 2 12 12 2;");

Or even better, use a CSS File, see this answer for further reference: JavaFX Text styling for dynamic objects

Upvotes: 4

Related Questions