ChoiceBox:从另一个ChoiceBox中删除所选项目时冻结

时间:2014-08-30 15:01:43

标签: javafx javafx-8

我想使用两个ChoiceBox提供相同的选择项,但另一个选择的项除外。但是,在选择了几次选项后,整个java进程都没有响应。

更新:如果我使用ComboBox而不是ChoiceBox,则不会出现此问题。但是,对其发生原因的解释会很有趣。

我有两个ChoiceBox

@FXML private ChoiceBox<String> firstCB;
@FXML private ChoiceBox<String> secondCB;

最初具有相同的选择选项

firstCB.getItems().addAll(Charset.availableCharsets().keySet());
secondCB.getItems().addAll(Charset.availableCharsets().keySet());

和事件监听器从其他ChoiceBox的选项中删除新选项并再次使旧选项可用

firstCB.getSelectionModel().selectedItemProperty()
.addListener((observable, oldVal, newVal) -> {
  secondCB.getItems().remove(newVal);
  secondCB.getItems().add(oldVal);
});

使用JComboBoxes的等效Swing代码和以下事件处理程序工作

firstCB.addItemListener(itemEvent -> {
  if (itemEvent.getStateChange() == ItemEvent.DESELECTED) {
    secondCB.addItem((String) itemEvent.getItem());
  } else if (itemEvent.getStateChange() == ItemEvent.SELECTED) {
    secondCB.removeItem(itemEvent.getItem());
  }
});

完整代码:

class test.Main

public class Main extends Application {
  private Parent rootPane;
  @Override
  public void start(Stage arg0) throws Exception {
    Scene scene = new Scene(rootPane);
    arg0.setScene(scene);
    arg0.show();
  }
  @Override
  public void init() throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
    rootPane = loader.load();
    loader.<View>getController().init();
  }
  public static void main(String[] args) {
    launch(args);
  }
}

class test.View

public class View {
  @FXML
  private ChoiceBox<String> firstCB;
  @FXML
  private ChoiceBox<String> secondCB;
  public void init() {
    firstCB.getItems().addAll(Charset.availableCharsets().keySet());
    secondCB.getItems().addAll(Charset.availableCharsets().keySet());
    firstCB.getSelectionModel().selectedItemProperty()
        .addListener((observable, oldVal, newVal) -> {
          System.out.printf("[%s]firstCB selection changed%n", Thread.currentThread().getName());
          secondCB.getItems().remove(newVal);
          secondCB.getItems().add(oldVal);
        });
    // removing one of the event listeners doesn't help
    secondCB.getSelectionModel().selectedItemProperty()
        .addListener((observable, oldVal, newVal) -> {
          System.out.printf("[%s]secondCB selection changed%n", Thread.currentThread().getName());
          firstCB.getItems().remove(newVal);
          firstCB.getItems().add(oldVal);
        });
  }
}

main.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="90.0" prefWidth="180.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.View">
   <children>
      <ChoiceBox fx:id="firstCB" layoutX="14.0" layoutY="14.0" prefWidth="150.0" />
      <ChoiceBox fx:id="secondCB" layoutX="14.0" layoutY="52.0" prefWidth="150.0" />
   </children>
</Pane>

1 个答案:

答案 0 :(得分:1)

试试这个,它对我来说很好用

firstComboBox.valueProperty().addListener((obs, oldV, newV) -> {
        secondComboBox.getItems().remove(newV);
        if(oldV!= null) secondComboBox.getItems().add(oldV);
    });

secondComboBox.valueProperty().addListener((obs, oldV, newV) -> {
        firstComboBox.getItems().remove(newV);
        if(oldV!= null) firstComboBox.getItems().add(oldV);
    });

或其他不具有可观察和过滤列表的最佳解决方案

public class Controller implements Initializable{
public ComboBox<String> firstComboBox;
public ComboBox<String> secondComboBox;

@Override
public void initialize(URL location, ResourceBundle resources) {
    ObservableList<String> list = FXCollections.observableArrayList();
    list.addAll("one", "two", "three", "four", "five");
    firstComboBox.getItems().addAll(list);
    secondComboBox.getItems().addAll(list);

    firstComboBox.valueProperty().addListener(c -> refreshList(list, secondComboBox, firstComboBox));
    secondComboBox.valueProperty().addListener(c -> refreshList(list, firstComboBox, secondComboBox));
}

private void refreshList(ObservableList<String> list, ComboBox<String> choiceBox, ComboBox<String> choiceBox2) {
    String tmp = choiceBox.getValue();
    FilteredList<String> filteredList = new FilteredList<>(list, string -> !string.equals(choiceBox2.getValue()));
    choiceBox.setItems(filteredList);
    choiceBox.setValue(tmp);
}