1

How do I set the colur of specific segments of a segmented bar in JavafX - which is from ControlsFX?

https://javadoc.io/static/org.controlsfx/controlsfx/8.40.16/org/controlsfx/control/SegmentedBar.html

So if I very basically constructed and set two segments as below, how would I set them as different colours?

@FXML SegmentedBar segments;

public void setSegments() {
segments.getSegments().addAll(
     new SegmentedBar.Segment(10, "10"),
     new SegmentedBar.Segment(90, "90"));
}

The .setStyle() method, which I've seen used elsewhere as a solution, seems to only work for the Segmented Bar object itself, not the individual segments. So I'm at a loss.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
sillyPin
  • 35
  • 5
  • what is SegmentedBar? not part of core fx, or is it? If not, please add the tag of the framework it is contained in – kleopatra Dec 01 '21 at 13:55
  • It's from ControlsFX which I guessed was more widely known than it must be. I've linked it in the post above. I've read and read the info in the link but I'm a newbie and can't figure out how to do it if .getStyle isn't an option for the bar segments – sillyPin Dec 01 '21 at 14:07
  • Never used it, so not sure of the details, but you can set the style on the `SegmentedBar.SegmentView` if you're using the default view factory and/or define a custom view factory. – James_D Dec 01 '21 at 14:35
  • cheers. But I have no issue with setting the style for the bar itself by using something like: "segments.setStyle("-fx-background-color: #2D3339")". But not the Segments themselves. I can't see how SegmentView helps with that, but I could be wrong. – sillyPin Dec 01 '21 at 14:43
  • As I said, you can set the style of the individual `SegmentView` objects. They are subclasses of `StackPane`. – James_D Dec 01 '21 at 14:51
  • Sorry I'm really not following- can you give an example line? – sillyPin Dec 01 '21 at 14:57
  • `segments.setSegmentViewFactory(segment -> { SegmentView view = new SegmentView(segment); view.setStyle(...); /* do other stuff with view */ ; return view;});` – James_D Dec 01 '21 at 15:31
  • Ok I'm not trying to be difficult, but that code just doesn't seem to work. The IDE does not like/accept: "SegmentView view = new SegmentView(segment)" I get red squiggles. I am new to all this, but I really don't see how that allows me to then refer to each segment, unless instead of above I name a segment and then set its style somehow (to name it something like: SegmentedBar.Segment segment1 = new SegmentedBar.Segment(10, "10") - but that doesn't allow for setStyle). Is there any chance you could use what you replied with in the context of my example above please? – sillyPin Dec 01 '21 at 16:13
  • Oh give over, it's just colloquial. It showed no error message...of course I checked. As it turns out, I did investigate. Your answer below, although very helpful, didn't fully solve the issue. The error was because when binding my segmented bar from scenebuilder/FXML I hadn't included the type (as in original post above). So I still got the error after using the below and yes then went on to figure it out. I've been coding now for about three weeks, just FYI, so no I probably didn't get there as quick as others. But don't assume I wasn't trying. Anyway, thanks for the help. – sillyPin Dec 01 '21 at 17:23
  • @strudelPie you might want to add a [self-answer](https://stackoverflow.com/help/self-answer) that fully solves the issue, to help future users who may have the same issue. – jewelsea Dec 01 '21 at 23:33

1 Answers1

2

Use the segmentViewFactory to style the SegmentView used to display each Segment:

import org.controlsfx.control.SegmentedBar;
import org.controlsfx.control.SegmentedBar.Segment;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;



public class App extends Application {

    @Override
    public void start(Stage stage) {
        SegmentedBar<Segment> segments = new SegmentedBar<>();
        segments.setOrientation(Orientation.HORIZONTAL);
        segments.getSegments().addAll(
                 new SegmentedBar.Segment(10, "10"),
                 new SegmentedBar.Segment(90, "90")     
        );
        segments.setSegmentViewFactory(segment -> {
            SegmentedBar<Segment>.SegmentView view = segments.new SegmentView(segment);
            String color = segment.getValue() < 50 ? "#66C2A5" : "#FC8D62" ;
            view.setStyle("-fx-background-color: "+color);
            return view ;
        });
        
        BorderPane root = new BorderPane(segments);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 800, 500);
        stage.setScene(scene);
        stage.show();
    }

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

}

enter image description here

James_D
  • 201,275
  • 16
  • 291
  • 322