当前位置:   article > 正文

JavaFX利用ListView做一个带搜索功能的多选框_javafx多选下拉框可搜索

javafx多选下拉框可搜索

        之前考虑过使用ComboBox做一个带搜索功能的多选下拉列表,但是发现实现过程中有些小坑,使用过其他大佬的案例,但是最终都存在一些小问题,没法实现自己想要的效果。后来在B站Aimls大神的视频中得到启发,使用TextField来完成搜索功能,这样做起来可以更轻松的实现搜索和多选。

        设计思路是TextField作为一个接收搜索字符的入口,在输入关键词后ListView检索出相关内容,并刷新数据源。ListView设置为单选模式,点击每一项都会让数据变成选中。

        直接上一段写好的代码吧,本人菜鸟,也没有过多的测试,有问题欢迎各位大佬指出。

  1. package sample;
  2. import javafx.application.Application;
  3. import javafx.beans.property.SimpleBooleanProperty;
  4. import javafx.beans.value.ChangeListener;
  5. import javafx.beans.value.ObservableValue;
  6. import javafx.collections.FXCollections;
  7. import javafx.collections.ObservableList;
  8. import javafx.event.ActionEvent;
  9. import javafx.event.EventHandler;
  10. import javafx.scene.Scene;
  11. import javafx.scene.control.*;
  12. import javafx.scene.control.cell.CheckBoxListCell;
  13. import javafx.scene.input.MouseEvent;
  14. import javafx.scene.layout.HBox;
  15. import javafx.scene.layout.VBox;
  16. import javafx.stage.Stage;
  17. import javafx.util.Callback;
  18. import javafx.util.StringConverter;
  19. import java.util.function.Consumer;
  20. import java.util.function.Predicate;
  21. public class Main extends Application {
  22. ObservableList<Data> items = FXCollections.observableArrayList();
  23. ObservableList<Data> tmpList = FXCollections.observableArrayList();
  24. @Override
  25. public void start(Stage primaryStage) throws Exception{
  26. HBox hb = new HBox(20);
  27. VBox root = new VBox(1);
  28. ListView<Data> listView = new ListView<Data>();
  29. ObservableList<Data> list = FXCollections.observableArrayList();
  30. Button selectFull = new Button("全选");
  31. Button clearBtn = new Button("清除");
  32. hb.getChildren().addAll(selectFull,clearBtn);
  33. TextField tf = new TextField();
  34. Data data1 = new Data("item1");
  35. Data data2 = new Data("item2",true);
  36. Data data3 = new Data("item3");
  37. Data data4 = new Data("item4");
  38. list.addAll(data1,data2,data3,data4);
  39. tf.textProperty().addListener(new ChangeListener<String>() {
  40. @Override
  41. public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
  42. System.out.println("搜索:"+newValue);
  43. tmpList = items.filtered(new Predicate<Data>() {
  44. @Override
  45. public boolean test(Data data) {
  46. return data.getName().contains(newValue);
  47. }
  48. });
  49. listView.setItems(tmpList);
  50. }
  51. });
  52. listView.setItems(list);
  53. tmpList = listView.getItems();
  54. items = listView.getItems();
  55. Callback<ListView<Data>,ListCell<Data>> call = CheckBoxListCell.forListView(new Callback<Data, ObservableValue<Boolean>>() {
  56. @Override
  57. public ObservableValue<Boolean> call(Data param) {
  58. if (param.isIsSelected()){
  59. return param.isSelectedProperty();
  60. }
  61. param.setIsSelected(false);
  62. return param.isSelectedProperty();
  63. }
  64. }, new StringConverter<Data>() {
  65. @Override
  66. public String toString(Data object) {
  67. return object.getName();
  68. }
  69. @Override
  70. public Data fromString(String string) {
  71. return new Data(string);
  72. }
  73. });
  74. listView.setCellFactory(call);
  75. listView.setOnMouseClicked(new EventHandler<MouseEvent>() {
  76. @Override
  77. public void handle(MouseEvent event) {
  78. if (listView.getSelectionModel().getSelectedItem() != null){
  79. listView.getSelectionModel().getSelectedItem().setIsSelected(!listView.getSelectionModel().getSelectedItem().isIsSelected());
  80. }
  81. }
  82. });
  83. listView.getItems().forEach(new Consumer<Data>() {
  84. @Override
  85. public void accept(Data data) {
  86. data.isSelectedProperty().addListener(new ChangeListener<Boolean>() {
  87. @Override
  88. public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
  89. System.out.println("data:"+data.getName()+":"+newValue);
  90. }
  91. });
  92. }
  93. });
  94. selectFull.setOnAction(new EventHandler<ActionEvent>() {
  95. @Override
  96. public void handle(ActionEvent event) {
  97. tmpList.forEach(new Consumer<Data>() {
  98. @Override
  99. public void accept(Data data) {
  100. data.setIsSelected(true);
  101. }
  102. });
  103. }
  104. });
  105. clearBtn.setOnAction(new EventHandler<ActionEvent>() {
  106. @Override
  107. public void handle(ActionEvent event) {
  108. tmpList.forEach(new Consumer<Data>() {
  109. @Override
  110. public void accept(Data data) {
  111. data.setIsSelected(false);
  112. }
  113. });
  114. }
  115. });
  116. root.getChildren().addAll(tf,hb,listView);
  117. primaryStage.setTitle("带搜索的多选");
  118. primaryStage.setScene(new Scene(root, 250, 250));
  119. primaryStage.show();
  120. }
  121. public static void main(String[] args) {
  122. launch(args);
  123. }
  124. }
  125. class Data{
  126. private String name;
  127. private SimpleBooleanProperty isSelected = new SimpleBooleanProperty();
  128. public Data(String name, Boolean isSelected) {
  129. this.name = name;
  130. this.isSelected = new SimpleBooleanProperty(isSelected);
  131. }
  132. public Data(String name){
  133. this.name = name;
  134. this.isSelected = new SimpleBooleanProperty(false);
  135. }
  136. public String getName() {
  137. return name;
  138. }
  139. public void setName(String name) {
  140. this.name = name;
  141. }
  142. public boolean isIsSelected() {
  143. return isSelected.get();
  144. }
  145. public SimpleBooleanProperty isSelectedProperty() {
  146. return isSelected;
  147. }
  148. public void setIsSelected(boolean isSelected) {
  149. this.isSelected.set(isSelected);
  150. }
  151. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号