我知道如何使用普通数据类型或自定义类包含普通数据类型来填充JavaFX中的tableview。但是,当我的类还包含列表时,我对如何制作一个tableview很感兴趣。我使用Scene Builder作为我的视图,因此我的视图文件是FXML。
以下是我的课程,其中列表为 duePersons 和 owePersons :
public class OverviewTable {
private SimpleStringProperty user = new SimpleStringProperty();
private SimpleDoubleProperty income = new SimpleDoubleProperty();
private SimpleDoubleProperty expenses = new SimpleDoubleProperty();
private SimpleDoubleProperty generalTotal = new SimpleDoubleProperty();
private ObservableList<Contributor> duePersons = FXCollections.observableArrayList();
private ObservableList<Contributor> owePersons = FXCollections.observableArrayList();
public OverviewTable(String user){
this.user.set(user);
}
public OverviewTable(String user, double income, double expenses) {
this.user.set(user);
this.income.set(income);
this.expenses.set(expenses);
this.generalTotal.set(income-expenses);;
}
public SimpleStringProperty userProperty() {
return user;
}
public void setUser(String user) {
this.user.set(user);
}
public SimpleDoubleProperty incomeProperty() {
return income;
}
public void setIncome(double income) {
this.income.set(income);
}
public SimpleDoubleProperty expensesProperty() {
return expenses;
}
public void setExpenses(double expenses) {
this.expenses.set(expenses);
}
public void setGeneralTotal(){
this.generalTotal.set(this.income.doubleValue() - this.expenses.doubleValue());
}
public SimpleDoubleProperty generalTotalProperty() {
return generalTotal;
}
public List<Contributor> duePersonsProperty() {
return duePersons;
}
public void setDuePersons(ObservableList<Contributor> duePersons) {
this.duePersons = duePersons;
}
public List<Contributor> getOwePersons() {
return owePersons;
}
public void setOwePersons(ObservableList<Contributor> owePersons) {
this.owePersons = owePersons;
}
public void addDuePersons(Contributor contributor){
this.duePersons.add(contributor);
}
public void addPersons(Contributor contributor){
this.duePersons.add(contributor);
}
}
这是Contributor类,它还扩展了只有“name”和“pid”属性的Person类。
public class Contributor extends Person{
private SimpleDoubleProperty amount = new SimpleDoubleProperty();
public Contributor(int pid, String name, Double sum) {
super(pid, name);
this.amount.set(sum);
}
public Contributor(String name, double amount) {
super(name);
this.amount.set(amount);
}
public SimpleDoubleProperty getSum() {
return amount;
}
public void setSum(double sum) {
this.amount.set(sum);
}
public SimpleStringProperty nameProperty(){
return super.nameProperty();
}
这是我的Java FXML视图控制器:
public class OverviewController{
@FXML
private TableView<OverviewTable> tableView;
@FXML
private TableColumn<OverviewTable, String> userColumn;
@FXML
private TableColumn<OverviewTable, Double> incomeColumn;
@FXML
private TableColumn<OverviewTable, Double> expensesColumn;
@FXML
private TableColumn<OverviewTable, Double> totalGeneralColumn;
@FXML
private TableColumn<OverviewTable, List<Contributor>> personDueColumn;
@FXML
private TableColumn<?, ?> amountDueColumn;
@FXML
private TableColumn<?, ?> totalDueColumn;
@FXML
private TableColumn<?, ?> personOweColumn;
@FXML
private TableColumn<?, ?> amountOwnColumn;
@FXML
private TableColumn<?, ?> totalOweColumn;
public void initialize() {
userColumn.setCellValueFactory(new PropertyValueFactory<>("user"));
incomeColumn.setCellValueFactory(new PropertyValueFactory<>("income"));
expensesColumn.setCellValueFactory(new PropertyValueFactory<>("expenses"));
totalGeneralColumn.setCellValueFactory(new PropertyValueFactory<>("generalTotal"));
personDueColumn.setCellValueFactory(new PropertyValueFactory<>("duePersons"));
tableView.setItems(OverviewTabQueries.getUsers());
}
正如您所看到的,最后一些列甚至尚未使用,因为我想首先使其适用于OverviewTable类中的一个列表,然后我将为另一个列执行此操作。 在上面的控制器中,一切正常,前4列,使用普通的字符串/双属性,但是当尝试输入hte List时,它不起作用。请注意,我从localhost上的MySQL数据库获取数据,但它正常工作,这不是问题。
那么有人可以帮我解决这个问题吗?我在网上搜索过但找不到tableview的例子,其中tableview中使用的Object中有一个或多个列表属性。
如果有人想知道,这里是加载数据时结果的样子(请原谅我的Paint技能): "Due Amount" and "Owe Amount" are the columns that should integrate the lists with the Person/User's name and amount
任何建议都会有所帮助:)