无法在代号一个表中更改行的背景颜色

时间:2017-01-14 15:08:02

标签: background-color codenameone

我有一个带有路由数据表的应用。数据是csv文件。我喜欢 通过更改行的背景颜色突出显示codenameone表中的行。我怎么能这样做?

我的代码是

        String File_Name ="/route.csv";
        //"/root/sdcard/Pictures/route.csv";
        File f= new File (File_Name);
        if (f.exists())

        InputStream is = Display.getInstance().getResourceAsStream(getClass(), File_Name );
        CSVParser parser = new CSVParser();
        String[][] data = parser.parse(is);
        String[] columnNames = new String[data[0].length];


        l =  data.length;
        for(int iter=  0 ; iter < columnNames.length ; iter++) {

        if (iter== 0) {
         columnNames[iter] = "Naam";
        } 
        else if (iter== 1) {
         columnNames[iter] = "Latitude";
        } 
         else if (iter== 2) {
          columnNames[iter] = "Longitude";
         } 
        } 
     tm = new DefaultTableModel(columnNames, data);    

        }
    }
    catch (IOException err){
         err.printStackTrace();
    }

进一步在代码中

 Table tm2 = new Table(tm) 

编辑解决了自己
我编辑了表定义tm2并添加了变量A. A是突出显示的行

  Table tm2 = new Table(tm)  {
@Override
public Component createCell(Object value, int row, int column, boolean editable) { // (1)

    Component cell;

    cell = super.createCell(value, row, column, editable);


    if(row > a-1 && row < a+1) { // (5)
        // pinstripe effect 
        cell.getAllStyles().setBgColor(0xe2f30d);
        cell.getAllStyles().setBgTransparency(255);
    }
    return cell;

}

1 个答案:

答案 0 :(得分:0)

the developer guide中讨论了更改单元格颜色,因为您可能从那里发现了细条纹样本。

开发者指南中的完整样本是:

Table table = new Table(model) {
    @Override
    protected Component createCell(Object value, int row, int column, boolean editable) { 
        Component cell;
        if(row == 1 && column == 1) {  
            Picker p = new Picker();
            p.setType(Display.PICKER_TYPE_STRINGS);
            p.setStrings("Row B can now stretch", "This is a good value", "So Is This", "Better than text field");
            p.setSelectedString((String)value);  
            p.setUIID("TableCell");
            p.addActionListener((e) -> getModel().setValueAt(row, column, p.getSelectedString()));  
            cell = p;
        } else {
            cell = super.createCell(value, row, column, editable);
        }
        if(row > -1 && row % 2 == 0) {  
            // pinstripe effect
            cell.getAllStyles().setBgColor(0xeeeeee);
            cell.getAllStyles().setBgTransparency(255);
        }
        return cell;
    }

    @Override
    protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
        TableLayout.Constraint con =  super.createCellConstraint(value, row, column);
        if(row == 1 && column == 1) {
            con.setHorizontalSpan(2);
        }
        con.setWidthPercentage(33);
        return con;
    }
};

enter image description here