-2

Is there any way in the code below to replace (Color) to standard Color. (with dot) in getting a color from UserData?

This line

rect.setFill((Color) group.getSelectedToggle().getUserData()

Wider code fragment based on Oracle doc about ToggleButton

tb1.setUserData(Color.LIGHTGREEN);
tb2.setUserData(Color.LIGHTBLUE);
tb3.setUserData(Color.SALMON);

final Rectangle rect = new Rectangle(145, 50);

final ToggleGroup group = new ToggleGroup();
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){

    public void changed(ObservableValue<? extends Toggle> ov,
        Toggle toggle, Toggle new_toggle) {
            if (new_toggle == null)
                rect.setFill(Color.WHITE);
            else
                rect.setFill(
                    (Color) group.getSelectedToggle().getUserData()
                );
         }
    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    `import static java.awt.Color.*;` – Elliott Frisch Sep 02 '17 at 05:23
  • As Elliott suggests: if you want to turn setUserData(Color.BLACK) to setUserData(BLACK) - just statically import these constants. Beyond that: look into your naming: how can you set *user data* and then pass a color? That method should rather be called `setDisplayColorForUser()` or something alike. – GhostCat Sep 02 '17 at 05:28
  • 3
    @GhostCat, are you sure that's the question? Isn't this question about how to avoid casting? – Modus Tollens Sep 02 '17 at 05:30
  • `(Color)` is a cast. It doesn't use a dot. The fact that you are casting `getUserData()` suggests you maybe returned an `Object` type – OneCricketeer Sep 02 '17 at 05:30
  • @ModusTollens It is the only Color line that has dots ... if you disagree, I can as well re-open and close-vote for "unclear" – GhostCat Sep 02 '17 at 05:33
  • 1
    @GhostCat The question is about casting https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Toggle.html#getUserData-- – OneCricketeer Sep 02 '17 at 05:34
  • @GhostCat I guess that unclear is better until OP explains a bit more. – Modus Tollens Sep 02 '17 at 05:34
  • @cricket_007 That is also just a guess ... – GhostCat Sep 02 '17 at 05:35
  • @GhostCat It's in the question *"based on Oracle doc about ToggleButton"* – OneCricketeer Sep 02 '17 at 05:37
  • Could you advise me source where I can read more about this statement (Color) - I mean Color with parentheses without dot. Till now i've meet only Color. (with dot) using articles on the internet. So I asked if there is possibility to use standard Color. as a alternative solution to (Color) in this particulatr case believing that this will help me to undertand how statement (Color) without dot works ;-). – Dariusz Dubiel Sep 03 '17 at 08:31

1 Answers1

2

set and get-UserData take/return Object types.

You need (Color) to cast the value given by group.getSelectedToggle().getUserData()

Unless I misunderstood the question, there doesn't need to be a way to replace that value because those are the properties defined on the Toggle... In other words, the value returned is equivalent to the Color.<VALUE> defined previously for that Toggle.

Your code can be re-written as this

        Color newColor;
        if (new_toggle == null)
            newColor = Color.WHITE;
        else
            newColor = (Color) group.getSelectedToggle().getUserData();
        rect.setFill(newColor);

Or this using a ternary expression

    rect.setFill(new_toggle == null ? Color.WHITE : (Color) group.getSelectedToggle().getUserData());
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Could you advise me source where I can read more about this statement (Color) - I mean Color with parentheses without dot. Till now i've meet only Color. (with dot) using articles on the internet. So I asked if there is possibility to use standard Color. as a alternative solution to (Color) in this particulatr case believing that this will help me to undertand how statement (Color) without dot works ;-). – Dariusz Dubiel Sep 03 '17 at 08:30
  • With or without is not the problem. The thing you need to research is called **casting** – OneCricketeer Sep 03 '17 at 08:38
  • Color with dot is a **static variable** access. The usage `Color c = (Color) variable;` is **casting** the `variable` instance into a `Color` object type – OneCricketeer Sep 03 '17 at 08:40