2016-04-25 101 views
-1

嗨,我寫一個小應用程序,以同時ping多個主機。JButton爲每一行只有最後一個按鈕的actionlistener被激活

正如你可以在下面的圖片看到我有jtable與列滿按鈕。我的問題如下:我用相同的actionListener向整列添加相同的Button。如果我點擊其中一個按鈕,則只激活最後一個按鈕的actionlistener,並將名稱更改爲單擊按鈕的名稱。爲什麼?

該按鈕的目標是用戶在第一列中插入IP地址並通過單擊按鈕啓動ping。現在幾個ping被髮送到IP地址來確定它的連接。如果用戶插入IP地址,表格模型中的PingRow對象將正確地存儲地址。我在tablemodel中使用了對象而不是二維數組。

應用

http://i.stack.imgur.com/ONrcO.jpg

按鈕 「抵達Neue Zeilehinzufügen」 的ActionListener的圖片。這意味着「添加新行」。 actionlistener實例是表模型中使用的PingRow對象,第二列是JButton。該行動在主持人的行爲監督者中作出迴應。該行爲應該以某種方式獲得該行的正確數量或該行的正確對象。在最後兩行中,我添加了CellRenderer和Cell Editor列。它們被用來渲染JButton。

public class NewRow implements ActionListener{ 
JTable table; 

Model_Main mMain; 
public NewRow(JTable table_Ping, Model_Main MM) { 
    table = table_Ping; 
    mMain = MM; 
} 
@Override 
public void actionPerformed(ActionEvent e) { 
    //Get the tablemodel 
    PingTableModel modelA = (PingTableModel) table.getModel(); 
    //JButton definition 
    OJButton tmp = new OJButton(table.getRowCount()); 
    tmp.setText("Starte Ping: " + tmp.getId()); 
    //Instance a object for the new row 
    PingRow pingRowObject = new PingRow("",tmp,0,0,table.getRowCount()); 
    //Instance the action 
    PingAddressAction pingAddressAction = new PingAddressAction(pingRowObject); 
    //Add the object to an arraylist 
    mMain.getListederPingRows().add(pingRowObject); 
    //Add the object to the tablemodel 
    modelA.addRow(pingRowObject); 
    //Rob Camick Class 
    ButtonColumn buttonColumn = new ButtonColumn(table,pingAddressAction,1); 
} 

} 

CellEditor - OJButton是一個普通的JButton,帶有用於測試目的的附加ID;我從Camick先生的教程中複製了mouselistener並將其添加到JTable。

public class JButtonEditor extends AbstractCellEditor implements TableCellEditor { 
OJButton button; 
String txt; 

public JButtonEditor(OJButton Button, PingRow pingRowObject, JTable table, Action action) { 
    super(); 
    button = Button; 
    button.setOpaque(true); 
    button.addActionListener(e1 -> { 
     //TODO: Bug fixen. Wenn man zu schnell eine weitere Zeile hinzufügt wird die gleiche rowcount übergeben. 
     int row = table.convertRowIndexToModel(table.getEditingRow()); 
     fireEditingStopped(); 
     // Invoke the Action 
     ActionEvent event = new ActionEvent(
       table, 
       ActionEvent.ACTION_PERFORMED, 
       "" + row); 
     action.actionPerformed(event); 
    }); 
} 
public Component getTableCellEditorComponent(JTable table, Object value, 
              boolean isSelected, int row, int column) { 
    txt = (value == null) ? "" : value.toString(); 
    button.setText(txt); 
    return button; 
} 


} 

行動

public class PingAddressAction implements Action { 
PingRow pingRowObject; 

public PingAddressAction(PingRow pingRowObject) { 
    this.pingRowObject = pingRowObject; 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    Ping_Thread ping_thread = new Ping_Thread(pingRowObject); 

    if (ping_thread.isStatus()) { 
     ping_thread.start(); 
    } 
}.... 

解決方案

  1. 實現從教程類:https://tips4java.wordpress.com/2009/07/12/table-button-column/
  2. 寫一個這樣的動作:

    public class PingAddressAction implements Action { 
    
    @Override 
    public void actionPerformed(ActionEvent e) { 
    
    JTable table = (JTable) e.getSource(); 
    PingTableModel modelA = (PingTableModel) table.getModel(); 
    //Get the row number from the event actioncommand and the object from the tablemodel 
    PingRow pingRowObject= modelA.getData(Integer.valueOf(e.getActionCommand())); 
    //the object contains all the information from the row 
    Ping_Thread ping_thread = new Ping_Thread(pingRowObject); 
    
    if (ping_thread.isStatus()) { 
        ping_thread.start(); 
    } 
    }..... 
    
+1

如果仍然卡住,一個有效的[mcve]會很好地幫助我們。 –

+0

好的,你是對的我會刪除表模型和celleditor有趣的代碼部分是第一個。 – AlaskaStack

+0

????這不是一個MCVE。在刪除其他內容之前,請閱讀或重新閱讀[mcve]鏈接。另請看[sscce](http:// sscce。org)鏈接更多關於我們需要看到的內容。 –

回答

1
aJTable.rowAtPoint(evt.getPoint()); 
+0

嗯,是的,這是mouseclickListener的想法。 :) – AlaskaStack

+0

@AlaskaStack,什麼MouseListener? – camickr

1

退房Table Button Column爲實現一個JButton作爲某列的渲染器/編輯器更好的辦法。

您只需要提供Action以在調用單元的編輯器時調用。

此外,變量名稱不應以大寫字符開頭。有些變量是正確的,其他的不是。由於論壇會突出顯示基於Java約定的類/變量,因此您的代碼無法閱讀,因爲您沒有遵循這些約定。

+0

我試圖實現教程中的提示,但我仍然有問題,我不明白這一點。 – AlaskaStack

+0

@AlaskaStack,本教程的要點是用本教程的代碼替換(不定製)您的渲染器/編輯器。然後你只需編寫Action。 – camickr

相關問題