JavaFX我想用非基本数据类型填充我的表
我有一门课是这样的:JavaFX我想用非基本数据类型填充我的表,javafx,tableview,Javafx,Tableview,我有一门课是这样的: public class HourMinute { private int hour; private int minute; ... } 像这样的课程: public class WorkTime { private HourMinute start; private HourMinute end; private HourMinute total; private LocalDate ld; private in
public class HourMinute {
private int hour;
private int minute;
...
}
像这样的课程:
public class WorkTime {
private HourMinute start;
private HourMinute end;
private HourMinute total;
private LocalDate ld;
private int ID;
...
}
我将工作时间类型信息存储在数据库中。我应该如何将数据放在表视图上
public void tableColumns() {
TableColumn year = new TableColumn("Year");
TableColumn month = new TableColumn("Month");
TableColumn day = new TableColumn("Day");
TableColumn startHour = new TableColumn("Start hour");
TableColumn startMinute = new TableColumn("Start minute");
TableColumn endHour = new TableColumn("End hour");
TableColumn totalHour = new TableColumn("Total hour");
TableColumn totalMinute = new TableColumn("Total minute");
//table.getColumns().addAll(...);
data.addAll(db.getAllWork());
table.setItems(data);
}
正如您在上面所看到的,WorkTime类包含HourMite和LocalDate变量。我应该如何从这些类型中提取数据,使它们在我的tableview中“可显示”
(附带问题:是否可以用字符串填充单元格?就像我只能创建一个数据列,看起来像YYYY-MM-DD(年+“-”+月+“-”+日))您需要更新数据对象模型以表示希望在TableView中显示的数据。因此,在下面的代码中,我修改了您的
WorkTime
类,使其更适合显示
因此,对于您的工作时间
类,我已将每个字段更新为属性
,以便表视图
可以找到它
我也重构了WorkTime
类。构造函数现在接受HourMinute
对象,并在创建WorkTime
对象时解析这些对象中的数据
public class WorkTime {
// All fields should be converted to properties
private IntegerProperty startHour = new SimpleIntegerProperty();
private IntegerProperty startMinute = new SimpleIntegerProperty();
private IntegerProperty endHour = new SimpleIntegerProperty();
private IntegerProperty endMinute = new SimpleIntegerProperty();
private IntegerProperty totalHours = new SimpleIntegerProperty();
private IntegerProperty totalMinutes = new SimpleIntegerProperty();
private ObjectProperty<LocalDate> localDate = new SimpleObjectProperty<>();
public WorkTime(HourMinute start, HourMinute end, HourMinute total, LocalDate localDate) {
// Accept the start and end times as HourMinute objects and extract each value from them.
this.startHour.set(start.getHour());
this.startMinute.set(start.getMinute());
this.endHour.set(end.getHour());
this.endMinute.set(end.getMinute());
this.totalHours.set(total.getHour());
this.totalMinutes.set(total.getMinute());
this.localDate.set(localDate);
}
public int getStartHour() {
return startHour.get();
}
public IntegerProperty startHourProperty() {
return startHour;
}
public int getStartMinute() {
return startMinute.get();
}
public IntegerProperty startMinuteProperty() {
return startMinute;
}
public int getEndHour() {
return endHour.get();
}
public IntegerProperty endHourProperty() {
return endHour;
}
public int getEndMinute() {
return endMinute.get();
}
public IntegerProperty endMinuteProperty() {
return endMinute;
}
public int getTotalHours() {
return totalHours.get();
}
public IntegerProperty totalHoursProperty() {
return totalHours;
}
public int getTotalMinutes() {
return totalMinutes.get();
}
public IntegerProperty totalMinutesProperty() {
return totalMinutes;
}
public LocalDate getLocalDate() {
return localDate.get();
}
public ObjectProperty<LocalDate> localDateProperty() {
return localDate;
}
}
TableColumn
对象也需要更新,以定义要显示的数据类型。这将采用两个参数:总体对象类型和单元格将保存的数据类型:
TableColumn<WorkTime, Integer> colStartHour = new TableColumn<>("Start Hour");
TableColumn<WorkTime, String> colStartMinute = new TableColumn<>("Start Minute");
TableColumn<WorkTime, Integer> colEndHour = new TableColumn<>("End Hour");
TableColumn<WorkTime, String> colEndMinute = new TableColumn<>("End Minute");
TableColumn<WorkTime, Integer> colTotalHours = new TableColumn<>("Total Hours");
TableColumn<WorkTime, String> colTotalMinutes = new TableColumn<>("Total Minutes");
您将看到newpropertyValueFactory(“propertyName”)
部分定义了WorkTime
中定义的属性的名称。JavaFX将获取该属性的值,并将其显示在单元格中
因此,只要您定义了该列以期望该数据类型,这将适用于您需要的任何数据类型
这就是全部!这将允许您正确显示数据
下面是一个简单的MCVE,它使用上面的WorkTime
类
然后将该列添加到您的
表视图中
,并设置相应的CelLValueProperty
使一个getter返回包含您的HourMinute显示格式的SimpleStringProperty。听起来不错,但不起作用。我应该如何在以下代码中调用getter方法:startHour.setCellValueFactory(newPropertyValueFactory(“start”);请看我的回答:它将引导您完成在TableView中显示对象数据所需的步骤。如果我有字符串,您的注释可能会重复。但是我有HourTime数据类型,包含2个int,LocalDate数据类型包含3个int。我想要一个使用我的数据类型的示例代码。谢谢。现在就更容易理解了。顺便说一下:永远不要手动格式化日期,而是使用合适的DateTimeFormatter-否则,如果区域设置发生变化,你会下地狱
TableColumn<WorkTime, Integer> colStartHour = new TableColumn<>("Start Hour");
TableColumn<WorkTime, String> colStartMinute = new TableColumn<>("Start Minute");
TableColumn<WorkTime, Integer> colEndHour = new TableColumn<>("End Hour");
TableColumn<WorkTime, String> colEndMinute = new TableColumn<>("End Minute");
TableColumn<WorkTime, Integer> colTotalHours = new TableColumn<>("Total Hours");
TableColumn<WorkTime, String> colTotalMinutes = new TableColumn<>("Total Minutes");
colStartHour.setCellValueFactory(new PropertyValueFactory<>("startHour"));
colStartMinute.setCellValueFactory(new PropertyValueFactory<>("startMinute"));
colEndHour.setCellValueFactory(new PropertyValueFactory<>("endHour"));
colEndMinute.setCellValueFactory(new PropertyValueFactory<>("endMinute"));
colTotalHours.setCellValueFactory(new PropertyValueFactory<>("totalHours"));
colTotalMinutes.setCellValueFactory(new PropertyValueFactory<>("totalMinutes"));
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.time.LocalDate;
import java.util.List;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Create sample WorkTime objects. Store them in an ObservableArrayList so the TableView can display them
ObservableList<WorkTime> workTimes = FXCollections.observableArrayList();
workTimes.add(new WorkTime(
new HourMinute(1, 2),
new HourMinute(1, 2),
new HourMinute(1, 2),
LocalDate.now()));
workTimes.add(new WorkTime(
new HourMinute(1, 2),
new HourMinute(1, 2),
new HourMinute(1, 2),
LocalDate.now()));
workTimes.add(new WorkTime(
new HourMinute(1, 2),
new HourMinute(1, 2),
new HourMinute(1, 2),
LocalDate.now()));
// Simple UI
VBox root = new VBox(10);
root.setPadding(new Insets(10));
root.setAlignment(Pos.TOP_CENTER);
// Create a TableView
TableView<WorkTime> tableView = new TableView<>();
// Create each column
TableColumn<WorkTime, Integer> colStartHour = new TableColumn<>("Start Hour");
TableColumn<WorkTime, String> colStartMinute = new TableColumn<>("Start Minute");
TableColumn<WorkTime, Integer> colEndHour = new TableColumn<>("End Hour");
TableColumn<WorkTime, String> colEndMinute = new TableColumn<>("End Minute");
TableColumn<WorkTime, Integer> colTotalHours = new TableColumn<>("Total Hours");
TableColumn<WorkTime, String> colTotalMinutes = new TableColumn<>("Total Minutes");
// Set the CellValueFactory for each column to define what data to use for each column. This should match the
// property set in the WorkTime class
colStartHour.setCellValueFactory(new PropertyValueFactory<>("startHour"));
colStartMinute.setCellValueFactory(new PropertyValueFactory<>("startMinute"));
colEndHour.setCellValueFactory(new PropertyValueFactory<>("endHour"));
colEndMinute.setCellValueFactory(new PropertyValueFactory<>("endMinute"));
colTotalHours.setCellValueFactory(new PropertyValueFactory<>("totalHours"));
colTotalMinutes.setCellValueFactory(new PropertyValueFactory<>("totalMinutes"));
// Add the columns to the TableView
tableView.getColumns().addAll(colStartHour, colStartMinute, colEndHour, colEndMinute, colTotalHours, colTotalMinutes);
// Set the people list as the data for the TableView
tableView.setItems(workTimes);
// Add the TableView to the scene
root.getChildren().add(tableView);
// Finish building the stage and show it
primaryStage.setTitle("TableView Sample");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
private StringProperty dateString = new SimpleStringProperty();
public String getDateString() {
return year + "-" + month + "-" + date;
}