This discussion is archived
6 Replies Latest reply: May 17, 2013 4:56 AM by jsmith RSS

How to get event source from value property changed handler?

979248 Newbie
Currently Being Moderated
I am relatively new to JavaFX, so perhaps this is an easy question to answer. I am creating in Java code several (e.g., 5) ChoiceBox instances. I add a handler for when the value of the ChoiceBox changes. My code:
ChoiceBox<String> rpmSelector = new ChoiceBox<>();
ObservableList<String> rpmSelectorItems = FXCollections.observableArrayList();
rpmSelectorItems.add(ALL_RPM);
for (Double engSpeed : engSpeeds) {
    rpmSelectorItems.add(String.format("%.0f", engSpeed.doubleValue()));
}
rpmSelector.setItems(rpmSelectorItems);
rpmSelector.getSelectionModel().selectFirst();
        
rpmSelector.valueProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> obsValue,
            String oldValue, String newValue) {
       // How can I get the ChoiceBox that was the source of this event?
    }
});
My question is: in the "changed" method, how do I determine which of the ChoiceBox instances had its value changed?

Thanks!

Chris

Edited by: 976245 on May 15, 2013 1:05 PM

Edited by: 976245 on May 15, 2013 1:05 PM
  • 1. Re: How to get event source from value property changed handler?
    jsmith Guru
    Currently Being Moderated
    Make rpmSelector final and you can refer to it in the ChangeListener.
    final ChoiceBox<String> rpmSelector = new ChoiceBox();
    ...
    rpmSelector.valueProperty().addListener(new ChangeListener<String>() {
      @Override public void changed(ObservableValue<? extends String> obsValue, String oldValue, String newValue) {
         System.out.println("Value changed to " + newValue + " for " + rpmSelector);
      }
    });
    You can also pass the choicebox the listener is for in to the ChangeListener constructor and store it:
    final ChoiceBox<String> rpmSelector = new ChoiceBox();
    ...
    rpmSelector.valueProperty().addListener(RpmSelectorChangeListener(rpmSelector));
    ...
    class RpmSelectorChangeListener extends ChangeListener<String>() {
      final ChoiceBox<String> rpmSelector;
      RpmSelectorChangeListener(ChoiceBox<String> rpmSelector) {
        this.rpmSelector = rpmSelector;
      }
      @Override public void changed(ObservableValue<? extends String> obsValue, String oldValue, String newValue) {
         System.out.println("Value changed to " + newValue + " for " + rpmSelector);
      }
    }
  • 2. Re: How to get event source from value property changed handler?
    shakir.gusaroff Expert
    Currently Being Moderated
         My question is: in the "changed" method, how do I determine which of the ChoiceBox instances had its value changed?
    You have only one ChoiceBox control. Probably you want to get the selected item. If it is the case you can get it through selectedItemProperty(),
    selectedIndexProperty(), and valueProperty()
            rpmSelector.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
                public void changed(ObservableValue<? extends String> ov, String old_val, String new_val) {
                    System.out.println("Selected item is: " + new_val);
                }
            });
    
    
    
            rpmSelector.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observableValue, Number oldv, Number newv) {
                    System.out.println("Selected index is: " + newv.intValue());
                }
            });
    
    
            rpmSelector.valueProperty().addListener(new ChangeListener<String>() {
                public void changed(ObservableValue<? extends String> ov, String old_val, String new_val) {
                    System.out.println("Selected   value: " + new_val);
                }
            });
  • 3. Re: How to get event source from value property changed handler?
    979248 Newbie
    Currently Being Moderated
    Thank you very much jsmith for your two solutions!! I think either one will work perfectly.
  • 4. Re: How to get event source from value property changed handler?
    979248 Newbie
    Currently Being Moderated
    Thanks for your reply Shakir. Although it was not shown in the code I posted, I do have multiple ChoiceBox controls... the code I posted is executed multiple times, i.e. it is inside a method that is called multiple times.
  • 5. Re: How to get event source from value property changed handler?
    jugen Newbie
    Currently Being Moderated
    You can also try the following:
    rpmSelector.valueProperty().addListener( new ChangeListener<String>() 
    {
        @Override
        public void changed(ObservableValue<? extends String> obsValue, String oldValue, String newValue) 
        {
            ReadOnlyProperty<? extends String>  property;
            property = (ReadOnlyProperty<? extends String>) obsValue;
    
            ChoiceBox  chbox = (ChoiceBox) property.getBean() ;
        }
    });
  • 6. Re: How to get event source from value property changed handler?
    jsmith Guru
    Currently Being Moderated
    Thanks jugen, I was looking for something like the getBean method, but got lost in the maze of JavaFX property interfaces and couldn't find it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points