如何自定义表组件的标题行(在codenameone中)

时间:2015-03-19 16:43:38

标签: codenameone

如何实现图像中所示的标题结构,以便对表格组件中提供的参数进行一定程度的区分。 enter image description here

如果你在图表中看到表格分为热侧和冷侧两部分。热侧有一些参数,冷侧有一些参数。至少需要这么多。

我根据您的问题修改了我的问题:

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
                   ...
            }

我只有两列" Hot 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;
            }

我的TableModel是:

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

这是屏幕的预览: enter image description here

1 个答案:

答案 0 :(得分:0)

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

在这种情况下,标题区域需要“跨越”,您可以通过覆盖约束行为来执行此操作:

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;
}