gpt4 book ai didi

java - 在 JavaFX 中使用 ComboBox 进行多重选择和取消选择

转载 作者:行者123 更新时间:2023-12-02 10:43:39 25 4
gpt4 key购买 nike

我正在学习 JavaFX 中的 ComboBox,我正在尝试构建一个 UI 来让用户选择选择模式(单个或多个),并选择模型中列出的国家/地区。我能够为模型添加更改监听器,并且当我在多重选择模式下按住 Ctrl + 选择国家/地区并且数据也被选择时,它会起作用。但是,当我取消选择之前选择的国家/地区时,它不会触发更改,也不会从 UI 底部显示的字符串中删除 -“所选项目是”。我试图了解如何触发 ComboBox 更改监听器以删除所选值。这是到目前为止的代码。

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Exercise_16_16_Copy extends Application {

ListView<String> listCountries = new ListView<String>();

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

@Override
public void start(Stage primaryStage) throws Exception {
HBox topSection = new HBox();
topSection.setPadding(new Insets(3));
topSection.setSpacing(5);
topSection.setAlignment(Pos.CENTER);

ComboBox<SelectionMode> comboList = new ComboBox<SelectionMode>();
comboList.getItems().add(SelectionMode.SINGLE);
comboList.getItems().add(SelectionMode.MULTIPLE);
comboList.setValue(comboList.getItems().get(0));
comboList.setOnAction(e-> listCountries.getSelectionModel().setSelectionMode(comboList.getValue()));

Label comboBoxLabel = new Label("Selection Mode: ", comboList);

comboBoxLabel.setContentDisplay(ContentDisplay.RIGHT);

topSection.getChildren().add(comboBoxLabel);

HBox bottomSection = new HBox();
bottomSection.setPadding(new Insets(3));
bottomSection.setAlignment(Pos.CENTER_LEFT);

Label lblSelectedItem = new Label("No Selected Items");
lblSelectedItem.setPadding(new Insets(5));

bottomSection.getChildren().add(lblSelectedItem);

Pane centerSection = new Pane();
listCountries.getItems().addAll("China", "Japan", "Korea", "India", "Malaysia", "Vietnam");
listCountries.setPrefHeight(listCountries.getItems().size() * 26);
centerSection.getChildren().add(listCountries);

listCountries.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
lblSelectedItem.setText(getItems());
primaryStage.sizeToScene();
});

BorderPane brdrPane = new BorderPane();
brdrPane.setTop(topSection);
brdrPane.setCenter(centerSection);
brdrPane.setBottom(bottomSection);
Scene scene = new Scene(brdrPane);
primaryStage.setScene(scene);
primaryStage.setTitle("ListView");
primaryStage.show();
}

private String getItems() {
ObservableList<String> selectedCountries = listCountries.getSelectionModel().getSelectedItems();
if (selectedCountries.size() == 0) {
return "No Countries Selected";
}
if (selectedCountries.size() == 1) {
return "Selected Item " + selectedCountries.get(0);
}
String displayText = "Selected Items are ";
for (String country : selectedCountries) {
displayText += country + " ";
}
return displayText;
}
}

getItems() 方法用于获取我在创建的 GUI 页脚处显示的选定数量的项目。是否可以使用 ComboBox 来实现此功能,或者我应该使用任何其他组件?请帮忙!

最佳答案

您可以在一个绑定(bind)中完成所有这些操作。

final List<String> selectedCountries = listCountries.getSelectionModel().getSelectedItems();
lblSelectedItem.textProperty().bind(Bindings.createStringBinding(() -> {
if (selectedCountries.isEmpty()) {
return "No Countries Selected";
}
else if (selectedCountries.size() == 1) {
return "Selected Item is " + selectedCountries.get(0);
}
else {
return "Selected Items are " + String.join(", ", selectedCountries);
}
}, selectedCountries));

关于java - 在 JavaFX 中使用 ComboBox 进行多重选择和取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52750914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com