如何禁用组合框中的项?

lkaoscv7  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(256)

我有两个组合框的一个可观察列表。当用户在其中一个框中选择一个项目时,我希望在另一个框中禁用选中的项目,反之亦然。在下面的代码中,我坚持在一边做:

  1. public class Controller implements Initializable {
  2. ObservableList<String> items = FXCollections.observableArrayList("first", "second", "third", "forth", "fifth");
  3. @FXML
  4. private ComboBox<String> first;
  5. @FXML
  6. private ComboBox<String> second;
  7. @Override
  8. public void initialize(URL url, ResourceBundle resourceBundle) {
  9. first.getItems().addAll(items);
  10. second.getItems().addAll(items);
  11. first.setCellFactory(new Callback<>() {
  12. @Override
  13. public ListCell<String> call(ListView<String> stringListView) {
  14. return new ListCell<>() {
  15. @Override
  16. protected void updateItem(String s, boolean b) {
  17. super.updateItem(s, b);
  18. if (b)
  19. setText(null);
  20. else {
  21. if (second.getValue() != null) {
  22. if (second.getValue().equals(s)) {
  23. setText(s);
  24. setDisable(true);
  25. } else setText(s);
  26. }
  27. }
  28. }
  29. };
  30. }
  31. });
  32. }
  33. }

如何禁用在“第二个”组合框中选择的“第一个”组合框中的项?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题