如何自定义 header 行的 Table 组件(在 codenameone 中)

how to customize header row of Table component (in codenameone )

如何实现如图所示的 header 结构,以便我可以对 Table 组件中提供的参数进行一定程度的区分。

如果你在图中看到Table分为热端和冷端两部分side.There是热端下的一些参数和冷端下的一些参数。至少需要这么多。

我正在根据您的回答修改我的问题:

String[][] tableData = {
                 { "Density", "Volume Flow", "T (in)","T (out)","Flow", "Specific Heat", "Density", "Volume Flow", 
                    "T (in)","T (out)","Flow", "Specific Heat", "Duty", "UA"}
         };

    final CustomTableModel tableModel = new CustomTableModel (
                new String[]  {"Hot Side", "","","","","Cold Side","","","","","",""}, tableData,true);

dataTable =  new Table(tableModel) {


         @Override
            protected Constraint createCellConstraint(Object value, int row, int column) {

                Constraint con = super.createCellConstraint(value, row, column);
                if(row == -1 && (column == 0 || column == 5) ) {
                    con.setHorizontalSpan(5);
                }
                return con;
            }
        @Override
        protected Component createCell(Object value, final int row, final int column, boolean editable) {

            if(row == -1) {
                    final Button headerButton = new Button((String)value);
                    headerButton.setUIID(getUIID() + "Header");
                    headerButton.getUnselectedStyle().setAlignment(Component.CENTER);
                    headerButton.getSelectedStyle().setAlignment(Component.CENTER);
                    headerButton.setFlatten(true);


                   return headerButton;
               }
                // Conditions for Other rows
                   ...
            }

我在 header 行中只有两列 "Hot Side" 和 "Cold Side",其余行中有 14 列。我尝试了不同的条件,但没有得到这种结构。我应该输入什么条件来获得结构。我想我在这里做错了。

这是我试过的:

            @Override
            protected Constraint createCellConstraint(Object value, int row, int column) {

                Constraint con = super.createCellConstraint(value, row, column);
                if(row == -1 && (column == 0 || column == 5) ) {
                    con.setHorizontalSpan(5);
                }
                return con;
            }

我的Table型号是:

final CustomTableModel tableModel = new CustomTableModel (
                new String[]  {"Hot Side", "","","","","Cold Side","","","","","",""}, tableData,true);

这是屏幕预览:

您可以通过覆盖 createCell 方法自定义 table 中的任何单元格,这包括 header 单元格,如果我没记错的话,它们被标记为 -1 行。

在这种情况下,header 区域需要 "span",您可以通过覆盖约束行为来做到这一点:

protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
    TableLayout.Constraint tl = super.createCellConstraint(value, row, column);
    if(this is the cell I want to span) {
       tl.setHorizontalSpan(4);
    }
    return tl;
}