在使用自定义类和比较器时设置ObservableList的类型,可以通过以下步骤完成:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
// ...
}
public class AgeComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p1.getAge(), p2.getAge());
}
}
ObservableList<Person> personList = FXCollections.observableArrayList();
personList.add(new Person("Alice", 25));
personList.add(new Person("Bob", 30));
personList.add(new Person("Charlie", 20));
Collections.sort(personList, new AgeComparator());
TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableView<Person> tableView = new TableView<>();
tableView.setItems(personList);
tableView.getColumns().add(nameColumn);
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云