2014-08-30 60 views
1

我想使用两个ChoiceBoxes提供相同的选择项目,但另一个选择项目除外。但是,在几次选择选项之后,整个java进程变得没有响应。ChoiceBox:从另一个选择框中删除选定项目时冻结

更新:如果我使用ComboBox而不是ChoiceBox,则不会发生该问题。然而,为什么会发生这样的解释会很有趣。

我有两个ChoiceBoxes

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

与JComboBoxe等效的Swing代码S和下面的事件处理工作

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

全码:

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

类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> 
+0

看起来像一个用法错误,给我等不是最好的解决办法:从choicebox API文档“ChoiceBox使用用于向用户呈现**相对较小的一组 预定义的选项“ - 它不针对处理较大的项目计数而优化(如同ComboBox,fi通过虚拟化节点) – kleopatra 2014-08-31 09:06:56

回答

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); 
} 
+0

您的代码确实有效,但这似乎是由于您使用** Combo ** Boxes而不是** Choice ** Boxes。更改我的原始代码以使用Combo-而不是ChoiceBoxes修复了问题;反过来改变你的代码使用ChoiceBoxes会导致同样的问题。 – 2014-08-31 07:49:50

相关问题