Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将其中一个命名为TableModel中的循环_Java_Codenameone - Fatal编程技术网

Java 将其中一个命名为TableModel中的循环

Java 将其中一个命名为TableModel中的循环,java,codenameone,Java,Codenameone,我试图在TableModel中放置一个循环,以循环arraylist中的所有项目,插入表中的所有行,以便将其添加到表单并向用户显示所有项目 公共类ListArticlesForm扩展表单{ public ListArticlesForm(Form previous) { setTitle("List all articles"); SpanLabel sp = new SpanLabel(); sp.setText(ServiceTask.getInstance().ge

我试图在TableModel中放置一个循环,以循环arraylist中的所有项目,插入表中的所有行,以便将其添加到表单并向用户显示所有项目 公共类ListArticlesForm扩展表单{

public ListArticlesForm(Form previous) {
    setTitle("List all articles");
    SpanLabel sp = new SpanLabel();
    sp.setText(ServiceTask.getInstance().getAllArticles().toString());

    ArrayList<Articles> articles = ServiceTask.getInstance().getAllArticles();

    TableModel model = new DefaultTableModel(new String[]{"name", "description", "label", "quantity", "rating", "rate"}, 

            new Object[][]{
        {
           // I WANT TO PLACE A FOR HERE (this is showing only the first row ! 
            articles.get(0).getName(), articles.get(0).getDescription(), articles.get(0).getLabel(), articles.get(0).getQuantity(), articles.get(0).getRating(), add(createStarRankSlider())

        },});

    Table table = new Table(model);
    add(table);
    getToolbar().addMaterialCommandToLeftBar("", FontImage.MATERIAL_ARROW_BACK, e -> previous.showBack());
}
public ListArticlesForm(以前的表单){
setTitle(“列出所有物品”);
SpanLabel sp=新的SpanLabel();
sp.setText(ServiceTask.getInstance().getAllArticles().toString());
ArrayList articles=ServiceTask.getInstance().getAllArticles();
TableModel model=新的DefaultTableModel(新字符串[]{“名称”、“说明”、“标签”、“数量”、“评级”、“费率”},
新对象[][]{
{
//我想在这里放置一个(这只显示第一行!
articles.get(0).getName(),articles.get(0).getDescription(),articles.get(0).getLabel(),articles.get(0).getQuantity(),articles.get(0).getRating(),add(createStarRankSlider())
},});
表=新表(型号);
增加(表);
getToolbar().AddMaterialCommand和ToLeftBar(“”,FontImage.MATERIAL_ARROW_BACK,e->previous.showBack());
}

}

您不能在数组中创建一个数组作为for循环。您需要更快地创建一行

Object[][] rows = new Object[articles.size()][];

for(int iter = 0 ; iter < rows.length ; iter++) {
    rows[iter] = new Object[] {
         articles.get(iter).getName(), articles.get(0).getDescription(), articles.get(0).getLabel(), articles.get(0).getQuantity(), articles.get(0).getRating(), add(createStarRankSlider())
    };
}

TableModel model = new DefaultTableModel(new String[]{"name", "description", "label", "quantity", "rating", "rate"}, rows);
Object[][]行=新对象[articles.size()][];
对于(int-iter=0;iter
现在,在我运行项目后,表中的行将正确显示,除了秩星 将在表外和列内显示文本: 创建星级的功能是:

           private Slider createStarRankSlider(int id) {
    Slider starRank = new Slider();
    starRank.setEditable(true);
    starRank.setMinValue(0);
    starRank.setMaxValue(10);
    int fontSize = Display.getInstance().convertToPixels(3);
    Font fnt = Font.createTrueTypeFont("Handlee", "Handlee-Regular.ttf").
            derive(fontSize, Font.STYLE_PLAIN);
    Style s = new Style(0xffff33, 0, fnt, (byte) 0);
    Image fullStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
    s.setOpacity(100);
    s.setFgColor(0);
    Image emptyStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
    initStarRankStyle(starRank.getSliderEmptySelectedStyle(), emptyStar);
    initStarRankStyle(starRank.getSliderEmptyUnselectedStyle(), emptyStar);
    initStarRankStyle(starRank.getSliderFullSelectedStyle(), fullStar);
    initStarRankStyle(starRank.getSliderFullUnselectedStyle(), fullStar);
    starRank.setPreferredSize(new Dimension(fullStar.getWidth() * 5, fullStar.getHeight()));
    starRank.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            ServiceTask.getInstance().UpdateRank(id,starRank.getIncrements());
             Dialog.show("Success","thank you for rating our product",new Command("OK"));

        }
    });

    return starRank;
}