禁用JavaFx ComboBox

时间:2018-11-18 13:55:26

标签: javafx-2

更正:这很好,我很傻。

我正在尝试禁用JavaFx ChoiceBox。认为这会起作用:

    shapeList = FXCollections.observableArrayList(CShape.DOT, CShape.RIGHT_GLIDER, CShape.LEFT_GLIDER);
    choiceBox = new ChoiceBox<>(shapeList);
    choiceBox.setDisable(true);

但似乎没有。

1 个答案:

答案 0 :(得分:1)

所有禁用/启用的功能都可以正常工作。您应该提供更多的代码来发现缺陷。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ChoiceBoxDisableApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        ObservableList<String> items = FXCollections.observableArrayList("one", "two", "three");
        ChoiceBox<String> choiceBox = new ChoiceBox<>(items);

        Button button = new Button("Disable");

        HBox hBox = new HBox(choiceBox, button);

        button.setOnMouseClicked(mouseEvent -> {
            boolean disable = !choiceBox.isDisabled();
            choiceBox.setDisable(disable);
            button.setText(disable ? "Enable" : "Disable");
        });

        stage.setScene(new Scene(hBox));
        stage.show();
    }
}