JavaFx:组合框动态文本更改

标签 java javafx javafx-8

当元素发生更改时,我尝试更新组合框中的文本,而不重新初始化项目或项目。

有没有办法将项目的文本绑定(bind)到显示的文本? 即使我使用 Property , StringConverter 似乎也会忽略它,这是可以理解的,但我想知道是否有一种方法可以在不重新初始化项目的情况下进行绑定(bind)。 这是重新初始化解决方案:JavaFx: ComboBox editor's text ,我想避免。

这是一个简单的代码,您可以验证:

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;
        }
    }
}

我想要的:当我按升级时,更新显示文本和下拉列表中的文本。

enter image description here enter image description here

这可能吗?

最佳答案

我解释了如何使用提取器。使用 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);
        }

    }

}

关于JavaFx:组合框动态文本更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60096575/

相关文章:

java - 如何在 NEO4J API 中设置关系属性?

java - 尝试使用准备好的语句删除元组

java - 如何在 TreeView 中显示我的图标而不是箭头?

java - 如何让hbox适合列标题?

java - 在 java 中将像素值从灰度设置为 0 和 1 时丢失图像部分

java - 如何调整包含 svg 图像的按钮

canvas - JavaFX GraphicsContext 更改文本大小

javafx-8 - Javafx8 弹出窗口透明度问题

java - javafx 中是否有任意形状的 "fill"函数?

java - 使用 google-http-client-xml 解析 xml : parsing xml element content as well as attributes using XmlObjectParser