在product列中,我有一个数组列表,其中包含每个产品的名称、每个产品的价格和每个产品的编号。当用户从DatePicker中选择一个特定日期并单击"Show today's sales“按钮时,该表将显示该特定日期的销售额。
问题是,整个Arraylist打印在单元格的一行中,但假设arraylist中的每个索引必须打印在一行上(其中包含产品名称、产品价格和产品编号)。
它应该看起来像这样:
我知道有一些问题看起来很相似,但不同的是,在这种情况下,我想在一个单元格中插入一个完整的数组列表,并且它应该打印在多行上。这个具体的问题之前没有得到回答。
到目前为止,我的代码如下:
package gui;
import container.Container;
import controller.Controller;
import model.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.GridPane;
// this class extends GridPane because it is a Javafx tab
public class Todays_sales extends GridPane {
Controller controller = Controller.getInstance();
Container container = Container.getInstance();
private DatePicker dpDate;
private TableView<Sale> table = new TableView<>();
private ObservableList<Sale> data;
public Todays_sales () {
setGridLinesVisible(false);
setPadding(new Insets(20));
setHgap(10);
setVgap(10);
// date
dpDate = new DatePicker();
this.add(dpDate, 0, 0);
dpDate.setEditable(false);
// button for today's sales
Button btnShowTodaysSales = new Button("Show today's sales");
this.add(btnShowTodaysSales , 0, 1);
btnShowTodaysSales.setOnAction(event -> showTodaysSales_Action());
// product
TableColumn<Sale, String> productCol = new TableColumn<>("Product");
productCol .setMinWidth(350);
productCol .setCellValueFactory(new PropertyValueFactory<Sale, String>("name_price_number"));
table.getColumns().add(productCol);
// total price
TableColumn<Sale, Integer> totalPriceCol = new TableColumn<>("Total price");
totalPriceCol.setMinWidth(100);
totalPriceCol.setCellValueFactory(new PropertyValueFactory<Sale, Integer>("totalprice"));
table.getColumns().add(totalPriceCol);
// type of payment
TableColumn<Sale, String> paymentTypeCol = new TableColumn<>("Payment type");
paymentTypeCol.setMinWidth(150);
paymentTypeCol.setCellValueFactory(new PropertyValueFactory<Sale, String>("paymentType"));
table.getColumns().add(paymentTypeCol);
this.add(table, 0, 4);
}
private void showTodaysSales_Action() {
if (dpDate.getValue() != null) {
data = FXCollections.observableArrayList(controller.getTodaysSales(dpDate.getValue()));
table.setItems(data);
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Today's sale");
alert.setHeaderText("");
alert.setContentText("A date must be selected");
alert.show();
}
}
}
发布于 2018-10-15 07:30:33
根据name_price_number已经是一个字符串数组,您可以替换下面这一行:
productCol .setCellValueFactory(new PropertyValueFactory<Sale, String>("name_price_number"));
自定义单元格值工厂,例如
productCol.setCellValueFactory(param -> {
String result = String.join("\n",param.getValue().getName_price_number());
return new SimpleStringProperty(result);
});
在此工厂中,您只需将数组与新的行分隔符连接在一起,并作为javaFX StringProperty返回
https://stackoverflow.com/questions/52807387
复制相似问题