如何在for-loop中的每一行添加按钮

时间:2012-12-10 12:49:47

标签: java for-loop resultset stringbuilder

我希望为我的for循环中的每一行实现一个按钮。但我不知道该怎么做。

我希望能够通过按下按钮来编辑每行中的数据。

for(HentbestillingsordreRegistrer hentboregistrer: hbor){
        if(status.equals("Leveret")){

        temp.append("<html>");
        temp.append("BestillingsID: ");   
        temp.append(hentboregistrer.BestillingsID);   
        temp.append("<br />");
        temp.append("Ordre Status:");
        temp.append(hentboregistrer.BestillingsStatus);
        temp.append("<br />");
        temp.append("LeverandoerID: ");
        temp.append(hentboregistrer.Leverandoernavn);
        temp.append("<br />");
        temp.append("Modtaget af: ");
        temp.append(hentboregistrer.ModtagetAf);
        temp.append("<br />");
        temp.append("<br />");


        }else{
            temp.append("<html>");
            temp.append("BestillingsID: ");
            temp.append(hentboregistrer.BestillingsID);   
            temp.append("<br />");   
            temp.append(hentboregistrer.BestillingsStatus);   
            temp.append("<br />");
            temp.append("LeverandoerID: ");
            temp.append(hentboregistrer.Leverandoernavn);
            temp.append("<br />");
            temp.append("Modtaget af: ");
            temp.append(hentboregistrer.ModtagetAf);
            temp.append("<br />");
            temp.append("<br />");



        }

    }
        return temp.toString();
}

public JPanel HentOrdreGUI(){

    JPanel info = new JPanel();
    info.setLayout(new BorderLayout());
    JLabel labelprintdata = new JLabel(temp.toString());
    JScrollPane scroll = new JScrollPane(labelprintdata);
    info.add(scroll, BorderLayout.CENTER);
    return info;
}

1 个答案:

答案 0 :(得分:0)

我会将JLabel更改为JTable,并添加JButton作为表格的第3列。像这样......

// Create a container for all your table data.
// There are 3 columns (type, value, button) in the table.
// And each order has 4 items in it (BestillingsId, Status, LeverandoerID, Modtaget)
Object[][] tableData = new Object[numOrders*4][3];

for(int i=0;i<numOrders;i++){
    HentbestillingsordreRegistrer hentboregistrer = hbor[i];

    // For each order, we need to add 4 rows, each with 3 cells in it (to hold the type, value, and the button)
    if(status.equals("Leveret")){
        tableData[(i*4)+0] = new Object[]{"BestillingsID",hentboregistrer.BestillingsID,new JButton("Edit")};
        tableData[(i*4)+1] = new Object[]{"Ordre Status",hentboregistrer.BestillingsStatus,new JButton("Edit")};
        tableData[(i*4)+2] = new Object[]{"LeverandoerID",hentboregistrer.Leverandoernavn,new JButton("Edit")};
        tableData[(i*4)+3] = new Object[]{"Modtaget af:",hentboregistrer.ModtagetAf,new JButton("Edit")};
    }
    else{
        tableData[(i*4)+0] = new Object[]{"BestillingsID",hentboregistrer.BestillingsID,new JButton("Edit")};
        tableData[(i*4)+1] = new Object[]{"Ordre Status",hentboregistrer.BestillingsStatus,new JButton("Edit")};
        tableData[(i*4)+2] = new Object[]{"LeverandoerID",hentboregistrer.Leverandoernavn,new JButton("Edit")};
        tableData[(i*4)+3] = new Object[]{"Modtaget af:",hentboregistrer.ModtagetAf,new JButton("Edit")};
    }
}

// Headings for the table columns
String[] tableHeadings = new String[]{"Type","Value","Button"};

JPanel info = new JPanel();
info.setLayout(new BorderLayout());

// Create the table from the data above
JTable table = new JTable(tableData,tableHeadings);
JScrollPane scroll = new JScrollPane(table);

info.add(scroll, BorderLayout.CENTER);