Display Combobox values from numbers

后端 未结 2 770

I have a int value which I want to use for configuration. It can have 2 values - 0 for active and 1 for Blocked. I want to display this into friendly combo box:



        
2条回答
  •  情话喂你
    2020-12-22 11:08

    Here is another solution:

    declare state as BooleanProperty:

       private BooleanProperty state = new SimpleBooleanProperty(false);
    

    bind state property to the valueProperty of comboBox:

     comboBox.valueProperty().bind(new When(state).then("Active").otherwise("Blocked"));
    

    complete example:

    public class ComboboxTest extends Application {
    
        private BooleanProperty state = new SimpleBooleanProperty(false);
        private Button button;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
    
            ObservableList options = FXCollections.observableArrayList(
                    "Active",
                    "Blocked"
            );
            ComboBox comboBox = new ComboBox(options);
            button = new Button("false");
            button.setOnAction(e -> setSate());
            button.textProperty().bind(state.asString());
            BorderPane bp = new BorderPane(comboBox);
            StackPane stackpane = new StackPane(button);
            stackpane.setAlignment(Pos.CENTER);
            bp.setTop(stackpane);
            bp.setPrefSize(800, 800);
            Scene scene = new Scene(bp);
            stage.setScene(scene);
            stage.show();
            comboBox.valueProperty().bind(new When(state).then("Active").otherwise("Blocked"));
        }
    
        public void setSate() {
            if (state.get()) {
                state.set(false);
    
            } else {
                state.set(true);
            }
        }
    
    }
    

提交回复
热议问题