【发布时间】:2018-06-13 04:45:50
【问题描述】:
这是我第一次使用 JavaFX 和 TableView。我有一个应用程序,用户可以输入高尔夫分数来为他们生成让分值。
我有一个 ArrayList 用作我的“数据库”:
public ArrayList<Score> scoreDB = new ArrayList<>();
用这个作为加值的方法:
public static Score scoreSubmit(DatePicker roundDate, TextField courseName, TextField courseRating, TextField courseSlope, TextField score)
{
Score temp = new Score();
//Wanted to use own Date class, must use this roundabout way to set the date correctly
LocalDate tempDate = roundDate.getValue();
temp.setRoundDate(tempDate.getMonthValue(), tempDate.getDayOfMonth(), tempDate.getYear());
temp.setCourseName(courseName.getText());
temp.setCourseRating(Double.valueOf(courseRating.getText()));
temp.setCourseSlope(Double.valueOf(courseSlope.getText()));
temp.setScore(Double.valueOf(score.getText()));
return temp;
}
注意:我使用了我自己创建的日期类。
我检查了该方法是否确实有效,并且 ArrayList 确实收到了一个值。
这是我构建的 TableView:
//Table columns
//Creating columns and setting the display to call the values from Score class
TableColumn<Score, String> courseNameColumn = new TableColumn<>("Course Name");
courseNameColumn.setMinWidth(100);
courseNameColumn.setCellValueFactory(new PropertyValueFactory<>("courseName"));
TableColumn<Score, Date> dateColumn = new TableColumn<>("Date");
dateColumn.setMinWidth(100);
dateColumn.setCellValueFactory(new PropertyValueFactory<>("roundDate"));
TableColumn<Score, Double> scoreColumn = new TableColumn<>("Score");
scoreColumn.setMinWidth(100);
scoreColumn.setCellValueFactory(new PropertyValueFactory<>("score"));
//This adds rating and slope under one column
TableColumn courseDataColumn = new TableColumn("Course Data");
TableColumn<Score, Double> courseRatingColumn = new TableColumn<>("Course Rating");
TableColumn<Score, Double> courseSlopeColumn = new TableColumn<>("Course Slope");
courseDataColumn.getColumns().addAll(courseRatingColumn, courseSlopeColumn);
courseDataColumn.setMinWidth(100);
courseRatingColumn.setCellValueFactory(new PropertyValueFactory<>("courseRating"));
courseSlopeColumn.setCellValueFactory(new PropertyValueFactory<>("courseSlope"));
scoreTable.getColumns().addAll(courseNameColumn, dateColumn, scoreColumn, courseDataColumn);
displayLayout.setCenter(displaySP);
displaySP.setBackground(new Background(new BackgroundFill(Paint.valueOf("#006400"), CornerRadii.EMPTY, Insets.EMPTY)));
displaySP.getChildren().add(scoreTable);
displayLayout.setBottom(displayHbox);
scoreTable.setItems(addScore());
以下是我的分数课程中的变量:
private double score = 0.0;
private double courseRating = 0.0;
private double courseSlope = 0.0;
private String courseName = "";
private Date roundDate;
这是我的分数类吸气剂(我已经读过这些必须以某种方式命名才能工作):
public double getScore()
{
return score;
}
public String getCourseName()
{
return courseName;
}
public Double getCourseSlope()
{
return courseSlope;
}
public double getCourseRating()
{
return courseRating;
}
public Date getRoundDate()
{
return roundDate;
}
这里是返回可观察列表的方法:
public ObservableList<Score> addScore()
{
ObservableList<Score> scores = FXCollections.observableArrayList();
// scores.add(new Score());
for (int i = 0; i < scoreDB.size(); i++)
{
scores.add(scoreDB.get(i));
}
return scores;
}
当仅使用注释掉的 new Score() 时,表格会填充此默认分数。使用 for 循环时,不会填充任何内容。
非常感谢任何帮助。
【问题讨论】:
-
代码中存在结构问题,但我猜你也有逻辑错误。在 scoreDB 中提交对象时不清楚,因为在上面的代码中,数组将为空且符合逻辑,循环不会执行任何操作。
-
scoreSubmit似乎没有将Score对象添加到任何数据结构中。它不是从您发布的任何代码 sn-p 调用的,您也没有描述它是如何调用的。是否调用了此方法?是使用结果还是按照方法名称建议的方式使用方法(即您假设该方法修改了某些数据结构)? -
请阅读stackoverflow.com/help/mcve并采取相应措施。
标签: java javafx arraylist tableview observablelist