2012-12-10 25 views
0

我希望爲我的for循環中的每一行實現一個按鈕。但我不知道如何去做。如何將按鈕添加到我的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; 
} 
+0

而不是使用JLabel,你應該實現類似'JTable'來顯示你的值。然後它很容易添加按鈕 - 只需在你的JTable中創建一個額外的行並添加一個JButton。 – wattostudios

+0

我該怎麼做? – user1880497

+0

參考下面我的回答 – wattostudios

回答

0

我會改變JLabelJTable,並添加JButton作爲表的第三列。像這樣的東西...

// 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); 
+0

HentbestillingsordreRegistrer hentboregistrer = hbor [i];將不起作用 – user1880497

+0

上面的代碼只是基於您在問題中發佈的代碼的一個示例。您將無法通過複製粘貼來使用它 - 您需要對其進行修改,以使其適合您的其他代碼。在這種情況下,我想要做的就是在'for'循環中獲取當前項目。 – wattostudios

+0

我不能讓它與你的代碼一起工作 – user1880497

相關問題