基本应用

  1. package com.hxw;
  2. import javafx.application.Application;
  3. import javafx.beans.value.ChangeListener;
  4. import javafx.beans.value.ObservableValue;
  5. import javafx.collections.FXCollections;
  6. import javafx.scene.Group;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.ListView;
  9. import javafx.stage.Stage;
  10. public class TestListView extends Application {
  11. @Override
  12. public void start(Stage primaryStage) throws Exception {
  13. ListView<String> listView = new ListView<>();
  14. listView.setItems(FXCollections.observableArrayList("小明","小红","小黄"));
  15. listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
  16. @Override
  17. public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
  18. System.out.println("选中的item为"+newValue);
  19. }
  20. });
  21. primaryStage.setHeight(400);
  22. primaryStage.setWidth(400);
  23. primaryStage.setScene(new Scene(new Group(listView)));
  24. primaryStage.show();
  25. }
  26. public static void main(String[] args) {
  27. launch();
  28. }
  29. }

image.png

Item的类型不为String的解决方案

  • 设置ListCell ```java package com.hxw;

import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.stage.Stage; import javafx.util.Callback;

public class TestListView extends Application {

  1. @Override
  2. public void start(Stage primaryStage) throws Exception {
  3. ListView<People> listView = new ListView<>();
  4. listView.setItems(FXCollections.observableArrayList(new People("小明",12),new People("小红",10),new People("小率",18)));
  5. listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<People>() {
  6. @Override
  7. public void changed(ObservableValue<? extends People> observable, People oldValue, People newValue) {
  8. System.out.println("选中的item为"+newValue.name + newValue.year);
  9. }
  10. });
  11. listView.setCellFactory(new Callback<ListView<People>, ListCell<People>>() {
  12. @Override
  13. public ListCell<People> call(ListView<People> param) {
  14. return new PeopleListCell();
  15. }
  16. });
  17. primaryStage.setHeight(400);
  18. primaryStage.setWidth(400);
  19. primaryStage.setScene(new Scene(new Group(listView)));
  20. primaryStage.show();
  21. }
  22. public static void main(String[] args) {
  23. launch();
  24. }
  25. class PeopleListCell extends ListCell<People> {
  26. @Override
  27. protected void updateItem(People item, boolean empty) {
  28. super.updateItem(item, empty);
  29. if (item == null){
  30. this.setText("");
  31. }else{
  32. this.setText(item.name+" "+item.year);
  33. }
  34. }
  35. }
  36. class People{
  37. public String name;
  38. public int year;
  39. public People(String name, int year) {
  40. this.name = name;
  41. this.year = year;
  42. }
  43. }

}

  1. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/645770/1605621130699-c478365b-a7bc-4652-a010-7cc5cc96c032.png#align=left&display=inline&height=400&margin=%5Bobject%20Object%5D&name=image.png&originHeight=400&originWidth=400&size=9170&status=done&style=none&width=400)<br />[TestListView.java](https://www.yuque.com/attachments/yuque/0/2020/java/645770/1605621221823-b36f156e-0438-4d52-8f1b-65c3b0bd1cc2.java?_lake_card=%7B%22uid%22%3A%221605621219593-0%22%2C%22src%22%3A%22https%3A%2F%2Fwww.yuque.com%2Fattachments%2Fyuque%2F0%2F2020%2Fjava%2F645770%2F1605621221823-b36f156e-0438-4d52-8f1b-65c3b0bd1cc2.java%22%2C%22name%22%3A%22TestListView.java%22%2C%22size%22%3A2140%2C%22type%22%3A%22%22%2C%22ext%22%3A%22java%22%2C%22progress%22%3A%7B%22percent%22%3A99%7D%2C%22status%22%3A%22done%22%2C%22percent%22%3A0%2C%22id%22%3A%22UaOTO%22%2C%22card%22%3A%22file%22%7D)
  2. <a name="C3EEb"></a>
  3. ## Item为多个控件
  4. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/645770/1610125009130-dd178dbb-e05d-4fb8-8eca-38606f1b6d9c.png#align=left&display=inline&height=393&margin=%5Bobject%20Object%5D&name=image.png&originHeight=393&originWidth=386&size=12328&status=done&style=none&width=386)
  5. ```java
  6. import javafx.application.Application;
  7. import javafx.beans.value.ChangeListener;
  8. import javafx.beans.value.ObservableValue;
  9. import javafx.collections.FXCollections;
  10. import javafx.geometry.Pos;
  11. import javafx.scene.Group;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.ListCell;
  16. import javafx.scene.control.ListView;
  17. import javafx.scene.layout.HBox;
  18. import javafx.stage.Stage;
  19. import javafx.util.Callback;
  20. public class TestListView extends Application {
  21. @Override
  22. public void start(Stage primaryStage) throws Exception {
  23. ListView<People> listView = new ListView<>();
  24. listView.setItems(FXCollections.observableArrayList(new People("小明",12),new People("小红",10),new People("小率",18)));
  25. listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<People>() {
  26. @Override
  27. public void changed(ObservableValue<? extends People> observable, People oldValue, People newValue) {
  28. System.out.println("选中的item为"+newValue.name + newValue.year);
  29. }
  30. });
  31. listView.setCellFactory(new Callback<ListView<People>, ListCell<People>>() {
  32. @Override
  33. public ListCell<People> call(ListView<People> param) {
  34. return new PeopleListCell();
  35. }
  36. });
  37. primaryStage.setHeight(400);
  38. primaryStage.setWidth(400);
  39. primaryStage.setScene(new Scene(new Group(listView)));
  40. primaryStage.show();
  41. }
  42. public static void main(String[] args) {
  43. launch();
  44. }
  45. class PeopleListCell extends ListCell<People> {
  46. @Override
  47. protected void updateItem(People item, boolean empty) {
  48. super.updateItem(item, empty);
  49. if (item == null){
  50. this.setText("");
  51. }else{
  52. HBox hBox = new HBox();
  53. hBox.setAlignment(Pos.CENTER);
  54. Label info = new Label(item.name+" "+item.year);
  55. Button delete = new Button("删除"+item.name);
  56. hBox.getChildren().addAll(info,delete);
  57. this.setGraphic(hBox);//核心就是这一句了
  58. }
  59. }
  60. }
  61. class People{
  62. public String name;
  63. public int year;
  64. public People(String name, int year) {
  65. this.name = name;
  66. this.year = year;
  67. }
  68. }
  69. }