JavaFx:组合框动态文本更改
JavaFx: ComboBox dynamic text change
我正在尝试在更改元素时更新组合框中的文本,而不重新初始化项目或项目。
有没有办法将项目的文本绑定到显示的文本?
即使我使用 Property
似乎 StringConverter 会忽略它,这是可以理解的,但我想知道是否有一种方法可以在不重新初始化项目的情况下进行绑定。
这是重新初始化解决方案: ,我想避免。
这是您可以验证的简单代码:
public class MainStageController implements Initializable {
@FXML
private ComboBox<Hero> comboBox;
@FXML
private Button lvlUp;
@Override
public void initialize(URL location, ResourceBundle resources) {
comboBox.setConverter(new StringConverter<Hero>() {
@Override
public String toString(Hero hero) {
return hero.getName() + " - Level: " + hero.getLevel();
}
@Override
public Hero fromString(String string) {
return null;
}
});
lvlUp.setOnAction(event -> {
Hero value = comboBox.getValue();
value.levelProperty().setValue(value.getLevel() + 1);
});
Hero weakHero = new Hero("Ted", 1);
Hero averageHero = new Hero("Zed", 10);
Hero strongHero = new Hero("Med", 25);
lvlUp.disableProperty().bind(comboBox.valueProperty().isNull());
comboBox.setItems(FXCollections.observableArrayList(weakHero, averageHero, strongHero));
}
private static class Hero {
private String name;
private IntegerProperty level;
public Hero(String name, Integer level) {
this.name = name;
this.level = new SimpleIntegerProperty(level);
}
private String getName() {
return name;
}
private int getLevel() {
return level.get();
}
public IntegerProperty levelProperty() {
return level;
}
}
}
我想要的:当我按 升级 时,更新显示文本和下拉列表中的文本。
这可能吗?
我解释了如何使用提取器。使用 StringConverter,没有监听器....我正在使用 JavaFX 8...
public class UpdateableComboBox extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ObservableList<Hero> heros
= FXCollections.observableArrayList((Hero param) -> new Observable[] { param.nameProperty(), param.levelProperty() });
heros.add(new Hero("Ted", 1));
heros.add(new Hero("Zed", 10));
heros.add(new Hero("Med", 25));
ComboBox<Hero> comboBox = new ComboBox<>();
comboBox.setPrefWidth(350);
comboBox.setItems(heros);
Button button = new Button("Level Up");
button.setOnAction(e -> {
Hero value = comboBox.getValue();
value.levelProperty().setValue(value.getLevel() + 1);
});
comboBox.setConverter(new StringConverter<Hero>() {
@Override
public String toString(Hero hero) {
return hero.getName() + " - Level: " + hero.getLevel();
}
@Override
public Hero fromString(String string) {
return null;
}
});
HBox hbox = new HBox(comboBox, button);
Scene scene = new Scene(hbox, 500, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private class Hero {
private StringProperty name = new SimpleStringProperty();
private IntegerProperty level = new SimpleIntegerProperty();
Hero(String name, int level){
this.name.set(name);
this.level.set(level);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
public final IntegerProperty levelProperty() {
return this.level;
}
public final int getLevel() {
return this.levelProperty().get();
}
public final void setLevel(final int level) {
this.levelProperty().set(level);
}
}
}
我正在尝试在更改元素时更新组合框中的文本,而不重新初始化项目或项目。
有没有办法将项目的文本绑定到显示的文本?
即使我使用 Property
似乎 StringConverter 会忽略它,这是可以理解的,但我想知道是否有一种方法可以在不重新初始化项目的情况下进行绑定。
这是重新初始化解决方案:
这是您可以验证的简单代码:
public class MainStageController implements Initializable {
@FXML
private ComboBox<Hero> comboBox;
@FXML
private Button lvlUp;
@Override
public void initialize(URL location, ResourceBundle resources) {
comboBox.setConverter(new StringConverter<Hero>() {
@Override
public String toString(Hero hero) {
return hero.getName() + " - Level: " + hero.getLevel();
}
@Override
public Hero fromString(String string) {
return null;
}
});
lvlUp.setOnAction(event -> {
Hero value = comboBox.getValue();
value.levelProperty().setValue(value.getLevel() + 1);
});
Hero weakHero = new Hero("Ted", 1);
Hero averageHero = new Hero("Zed", 10);
Hero strongHero = new Hero("Med", 25);
lvlUp.disableProperty().bind(comboBox.valueProperty().isNull());
comboBox.setItems(FXCollections.observableArrayList(weakHero, averageHero, strongHero));
}
private static class Hero {
private String name;
private IntegerProperty level;
public Hero(String name, Integer level) {
this.name = name;
this.level = new SimpleIntegerProperty(level);
}
private String getName() {
return name;
}
private int getLevel() {
return level.get();
}
public IntegerProperty levelProperty() {
return level;
}
}
}
我想要的:当我按 升级 时,更新显示文本和下拉列表中的文本。
这可能吗?
我解释了如何使用提取器。使用 StringConverter,没有监听器....我正在使用 JavaFX 8...
public class UpdateableComboBox extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ObservableList<Hero> heros
= FXCollections.observableArrayList((Hero param) -> new Observable[] { param.nameProperty(), param.levelProperty() });
heros.add(new Hero("Ted", 1));
heros.add(new Hero("Zed", 10));
heros.add(new Hero("Med", 25));
ComboBox<Hero> comboBox = new ComboBox<>();
comboBox.setPrefWidth(350);
comboBox.setItems(heros);
Button button = new Button("Level Up");
button.setOnAction(e -> {
Hero value = comboBox.getValue();
value.levelProperty().setValue(value.getLevel() + 1);
});
comboBox.setConverter(new StringConverter<Hero>() {
@Override
public String toString(Hero hero) {
return hero.getName() + " - Level: " + hero.getLevel();
}
@Override
public Hero fromString(String string) {
return null;
}
});
HBox hbox = new HBox(comboBox, button);
Scene scene = new Scene(hbox, 500, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private class Hero {
private StringProperty name = new SimpleStringProperty();
private IntegerProperty level = new SimpleIntegerProperty();
Hero(String name, int level){
this.name.set(name);
this.level.set(level);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
public final IntegerProperty levelProperty() {
return this.level;
}
public final int getLevel() {
return this.levelProperty().get();
}
public final void setLevel(final int level) {
this.levelProperty().set(level);
}
}
}