我每个人中有4个CONFIG SET appendonly no
有5个元素:" "," 1"," 2"," 3"," 4"。
如果项目" 1"我必须做在ComboBox
中被选中,它会被自动排除在其他内容之外。如果项目是" "在ComboBox
insame中选中
它会自动返回到另一个。因此除了"之外的其他项目也是如此。 &#34 ;.
我试着这样做以下代码:
ComboBox
q1vub1,q1vub2,q1vub3,q1vub4是从public void vubadditems(ComboBox<String> vub1,ComboBox<String> vub2,ComboBox<String> vub3,ComboBox<String> vub4){
vub1.getItems().addAll(" ","1","2","3","4");
vub2.getItems().addAll(" ","1","2","3","4");
vub3.getItems().addAll(" ","1","2","3","4");
vub4.getItems().addAll(" ","1","2","3","4");
}
public void vubact(ComboBox<String> vub1,ComboBox<String> vub2,ComboBox<String> vub3,ComboBox<String> vub4){
if(vub1.getSelectionModel().getSelectedItem()!=" "){
vub2.getItems().remove(vub1.getSelectionModel().getSelectedItem());
vub3.getItems().remove(vub1.getSelectionModel().getSelectedItem());
vub4.getItems().remove(vub1.getSelectionModel().getSelectedItem());
s=vub1.getSelectionModel().getSelectedItem();
}else{
}
if(vub2.getSelectionModel().getSelectedItem()!=" "){
vub1.getItems().remove(vub2.getSelectionModel().getSelectedItem());
vub3.getItems().remove(vub2.getSelectionModel().getSelectedItem());
vub4.getItems().remove(vub2.getSelectionModel().getSelectedItem());
}
if(vub3.getSelectionModel().getSelectedItem()!=" "){
vub1.getItems().remove(vub3.getSelectionModel().getSelectedItem());
vub2.getItems().remove(vub3.getSelectionModel().getSelectedItem());
vub4.getItems().remove(vub3.getSelectionModel().getSelectedItem());
}
if(vub4.getSelectionModel().getSelectedItem()!=" "){
vub1.getItems().remove(vub4.getSelectionModel().getSelectedItem());
vub2.getItems().remove(vub4.getSelectionModel().getSelectedItem());
vub3.getItems().remove(vub4.getSelectionModel().getSelectedItem());
}
}
public void vubq1ac(){
vubact(q1vub1,q1vub2,q1vub3,q1vub4);
}
public void initialize(){
vubadditems(q1vub1,q1vub2,q1vub3,q1vub4);
vubadditems(q2vub1,q2vub2,q2vub3,q2vub4);
vubadditems(q3vub1,q3vub2,q3vub3,q3vub4);
vubadditems(q4vub1,q4vub2,q4vub3,q4vub4);
vubadditems(q5vub1,q5vub2,q5vub3,q5vub4);
}
FXML_file
加载的
我在ComboBox
上添加了vubq1ac作为动作列表器
但我有一些例外和程序工作不正确
例外:
ComboBox
答案 0 :(得分:0)
如果您遵循当前的方法,您的代码将会很长并且很难调试。目前似乎:
ComboBox
&#34; 1&#34;被选中(并随后被排除在其他组件之外),您决定将其更改回&#34; &#34 ;.您无法直接从ComboBox
获取之前的值,因为.getSelectedItem()
只会返回当前值,即&#34; &#34;已经。我将向您介绍我的方法,并简要介绍一下它的工作原理。
public class JavaFXApplication extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
FlowPane root = new FlowPane();
ArrayList<String> items = new ArrayList<>();
Collections.addAll(items, " ", "1", "2", "3", "4");
List<ComboBox> comboBoxList = new ArrayList<>();
for (int i = 0; i < 4; i++) {
ComboBox c = new ComboBox(FXCollections.observableArrayList(items));
c.setValue(c.getItems().get(0));
c.valueProperty().addListener(removeFromOtherCombo(comboBoxList, c));
comboBoxList.add(c);
}
root.getChildren().addAll(comboBoxList);
Scene scene = new Scene(root, 200, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static ChangeListener removeFromOtherCombo(List<ComboBox> boxes, ComboBox current) {
return (o, oldVal, newVal) -> {
if (newVal.equals(oldVal)) {
return;
}
if (!oldVal.equals(" ")) {
boxes.stream()
.filter(c -> !c.equals(current))
.forEach((ComboBox) -> {
ComboBox.getItems().add(oldVal);
});
}
if (!newVal.equals(" ")) {
boxes.stream()
.filter(c -> !c.equals(current))
.forEach((ComboBox) -> {
ComboBox.getItems().remove(newVal);
});
}
};
}
}
对于每个ComboBox
,我创建一个ChangeListener
,了解其分配给的所有其他ComboBox
和ComboBox
。
c.valueProperty().addListener(removeFromOtherCombo(comboBoxList, c));
每次选择新值时都会通知ChangeListener
,幸运的是,它会收到旧值和新值。
如果旧值等于新值,则我们无需做任何事情。
if (newVal.equals(oldVal)) {
return;
}
如果旧值是一个数字(例如我们从&#34; 1&#34;更改为&#34; 2&#34;)那么我们必须将之前选择的数字返回到{{1除了一个,它被过滤了。
ComboBox
当我们更改为某个数字时,会采用相同的方法,但这次我们必须从除{1}之外的每个if (!oldVal.equals(" ")) {
boxes.stream()
.filter(c -> !c.equals(current))
.forEach((ComboBox) -> {
ComboBox.getItems().add(oldVal);
});
}
中删除它。