0

我希望为我的 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;
}
4

1 回答 1

0

I would change the JLabel into a JTable, and add a JButton as the 3rd column of the table. Something like this...

// 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);
于 2012-12-10T12:59:31.633 回答