ตั้งชื่อรหัสหนึ่งวงภายใน TableModel

ฉันกำลังพยายามวางการวนซ้ำภายใน TableModel เพื่อวนซ้ำบทความทั้งหมดในรายการอาร์เรย์เพื่อแทรกแถวทั้งหมดภายในตารางเพื่อให้ฉันสามารถเพิ่มลงในแบบฟอร์มและแสดงบทความทั้งหมดให้กับผู้ใช้คลาสสาธารณะ 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());
}

}


person mohamed lahsoumi    schedule 07.05.2020    source แหล่งที่มา


คำตอบ (2)


คุณไม่สามารถสร้างอาร์เรย์เป็น for loop ภายในอาร์เรย์ได้ คุณต้องทำเป็นบรรทัดเร็วกว่านี้

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);
person Shai Almog    schedule 07.05.2020

ตอนนี้หลังจากที่ฉันรันโปรเจ็กต์ แถวของตารางจะแสดงอย่างถูกต้อง ยกเว้นอันดับดาว การแสดงนอกตารางและภายในโคลอนข้อความจะปรากฏขึ้น: ฟังก์ชั่นสำหรับการสร้างอันดับดาวคือ:

           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;
}
person mohamed lahsoumi    schedule 08.05.2020