如何在javafx的tableview中显示多行单元格?
我有一个用作收据的表(TableView)。该表由三列组成:产品、总价和付款类型。 在product列中,我有一个arraylist,其中包含每个产品的名称、每个产品的价格和每个产品的编号。 当用户从日期选择器中选择特定日期并单击“显示今天的销售额”按钮时,表格将显示该特定日期的销售额 问题是整个Arraylist打印在单元格中的一行中,但假设Arraylist中的每个索引都必须打印在一行中(其中包含产品名称、产品价格和产品编号) 应该是这样的: 我知道有些问题看起来很相似,但区别在于,在这种情况下,我想在单元格中插入一个完整的arraylist,它应该打印在多行上。这一具体问题此前尚未得到回答 以下是我目前的代码:如何在javafx的tableview中显示多行单元格?,java,javafx,Java,Javafx,我有一个用作收据的表(TableView)。该表由三列组成:产品、总价和付款类型。 在product列中,我有一个arraylist,其中包含每个产品的名称、每个产品的价格和每个产品的编号。 当用户从日期选择器中选择特定日期并单击“显示今天的销售额”按钮时,表格将显示该特定日期的销售额 问题是整个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();
}
}
}
packagegui;
进口集装箱。集装箱;
导入控制器。控制器;
导入模型。*;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Insets;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.Alert.AlertType;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.control.Alert;
导入javafx.scene.control.Button;
导入javafx.scene.control.DatePicker;
导入javafx.scene.layout.GridPane;
//此类扩展了GridPane,因为它是一个Javafx选项卡
公共类Todays_sales扩展了GridPane{
Controller=Controller.getInstance();
Container=Container.getInstance();
私有日期选择器dpDate;
private TableView table=new TableView();
私有可观测列表数据;
今日公开发售(){
setGridLinesVisible(假);
设置填充(新插图(20));
setHgap(10);
setVgap(10);
//日期
dpDate=新的日期选择器();
添加(dpDate,0,0);
dpDate.setEditable(false);
//今天销售的按钮
Button btnShowTodaysSales=新按钮(“显示今天的销售额”);
添加(btnShowTodaysSales,0,1);
btnShowTodaysSales.setOnAction(事件->showTodaysSales_Action());
//产品
TableColumn productCol=新的TableColumn(“产品”);
productCol.setMinWidth(350);
productCol.setCellValueFactory(新属性ValueFactory(“名称、价格、编号”);
table.getColumns().add(productCol);
//总价
TableColumn totalPriceCol=新的TableColumn(“总价”);
总价格设置最小宽度(100);
totalPriceCol.setCellValueFactory(新属性ValueFactory(“totalprice”));
table.getColumns().add(totalPriceCol);
//付款方式
TableColumn paymentTypeCol=新的TableColumn(“付款类型”);
paymentTypeCol.setMinWidth(150);
paymentTypeCol.setCellValueFactory(新属性ValueFactory(“paymentType”));
table.getColumns().add(paymentTypeCol);
添加(表0,4);
}
私人无效展示TodaysSales_Action(){
if(dpDate.getValue()!=null){
data=FXCollections.observearraylist(controller.getTodaysSales(dpDate.getValue());
表2.设置项目(数据);
}否则{
警报警报=新警报(警报类型.信息);
alert.setTitle(“今日销售”);
alert.setHeaderText(“”);
alert.setContentText(“必须选择日期”);
alert.show();
}
}
}
任何形式的帮助都将不胜感激-提前感谢 根据name\u price\u number是一个字符串数组,您可以直接替换它
这一行:
productCol .setCellValueFactory(new PropertyValueFactory<Sale, String>("name_price_number"));
在这个工厂中,您只需将您的阵列与一个新的测线delimeter和
作为与您的问题无关的javaFX StringProperty返回:请学习java命名约定并遵守它们
productCol.setCellValueFactory(param -> {
String result = String.join("\n",param.getValue().getName_price_number());
return new SimpleStringProperty(result);
});