对于TableColumn
值的设置,我不会解释它,因为它已经被回答过了,再加上你没有问这个问题。您可以在How to set value to cell in TableView in javafx以及Google搜索中找到更多信息。我搜索了“javafx set tablecolumn value”
您应该有一个合适的数据模型Class
,它具有适合您的数据集的方法,您可以从数据库中获取该数据集。我不知道你的数据库里有什么,所以我只是使用你在图片中发布的内容,即Item Name
,Priority
,Status
。
您当然可以将Circle
添加到您的TableView
,但应该在您的数据模型Class
中适当声明。
有些事情你还需要我没有提供:
- 你需要分析你的数据库的回报,并进行相应的数据构造函数调用。我会让你知道的。
好吧,现在我正在提供什么和我做了什么来实现它。
您将需要使用您的数据模型Class
来存储您的数据库返回信息,这就是为什么它的重要性在于使其设置正确。因此,对于您在返还时关心的每个数据点,应该有适当的set
和get
方法。
在您的返回数据循环中,您需要创建一个new DataModel(x, y, z, ...);
并将其添加到ObservableList<DataModel>
。
这里是示范基地穿上你的图片提供:
主。java的
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Data> dataTable = new TableView<>();
TableColumn<Data, String> itemName = new TableColumn<>("Item Name");
itemName.setCellValueFactory(cell -> new SimpleStringProperty(cell.getValue().getName()));
TableColumn<Data, String> priority = new TableColumn<>("Priority");
priority.setCellValueFactory(cell -> new SimpleStringProperty(cell.getValue().getPriority()));
TableColumn<Data, Circle> status = new TableColumn<>("Status");
status.setCellValueFactory(cell -> new SimpleObjectProperty<>(cell.getValue().getStatus()));
dataTable.getColumns().addAll(itemName, priority, status);
ObservableList<Data> data = FXCollections.observableArrayList();
Data high = new Data("toy", "high");
Data medium = new Data("box", "medium");
Data low = new Data("candy", "low");
data.addAll(high, medium, low);
dataTable.setItems(data);
Scene scene = new Scene(dataTable, 500, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Data.java(这是我的数据模型)
public class Data {
private String name;
private String priority;
private Circle status;
public Data (String name, String priority){
setName(name);
setPriority(priority);
setStatus(priority);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPriority(){
return priority;
}
public void setPriority(String priority){
this.priority = priority;
}
public Circle getStatus(){
return status;
}
public void setStatus(String priority){
Circle c = new Circle(5);
switch(priority){
case "high":
c.setFill(Color.GREEN);
break;
case "medium":
c.setFill(Color.YELLOW);
break;
case "low":
c.setFill(Color.RED);
break;
default:
}
status = c;
}
}
正如你所看到的,我没有提供在Constructor
的status
,而是我用Priority
来确定它。
希望这会有所帮助。
我增加了更多的内容 - 如果你不介意看看 @HypnicJerk – jerry