JavaFx组合框valueProperty().addListener(新的ChangeListener<;String>;()逐步重复
我正在JavaFX项目上使用ComboBox。当我使用valueProperty().addListener(new ChangeListener())时, 它是随着每次单击而逐步重复的操作 这是我的控制器:JavaFx组合框valueProperty().addListener(新的ChangeListener<;String>;()逐步重复,java,javafx,combobox,controller,fxml,Java,Javafx,Combobox,Controller,Fxml,我正在JavaFX项目上使用ComboBox。当我使用valueProperty().addListener(new ChangeListener())时, 它是随着每次单击而逐步重复的操作 这是我的控制器: public class Controller { // some code @FXML private ComboBox<String> FruitList; int count = 1; public void comboAction() {
public class Controller {
// some code
@FXML
private ComboBox<String> FruitList;
int count = 1;
public void comboAction() {
FruitList.valueProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println("Selected value : " + newValue);
}
});
//count number of select actions
System.out.println("Selection number: " + count++);
}
}
如果您查看以下内容:
ComboBox操作,每当ComboBox值
属性已更改
所以当你这样做的时候:
<ComboBox fx:id="FruitList" onAction="#comboAction" ...>
Selection number: 1
Selected value : Apple
Selection number: 2
Selected value : Pear
Selected value : Pear
Selection number: 3
Selected value : Orange
Selected value : Orange
Selected value : Orange
Selection number: 4
Selected value : Apple
Selected value : Apple
Selected value : Apple
Selected value : Apple
Selection number: 5 Selected value : Pear
Selected value : Pear
Selected value : Pear
Selected value : Pear
Selected value : Pear
Selected value : Pear
<ComboBox fx:id="FruitList" onAction="#comboAction" ...>
public void comboAction() {
System.out.println("Selected value : " + FruitList.getValue());
}