Java JButton对于每行,只有最后一个按钮的actionlistener被激活
嗨,我正在编写一个小应用程序,同时ping多个主机 正如你在下图中看到的,我有一个jtable,其中有一列按钮。我的问题如下:我在整个列中添加了具有相同actionListener的相同按钮。如果单击其中一个按钮,则只有最后一个按钮的actionlistener被激活,并且名称更改为单击按钮的名称。为什么? 该按钮的目标是用户在第一列中插入IP地址,并通过单击按钮开始ping。现在会向IP地址发送几个ping以确定其连接。如果用户插入IP地址,tablemodel中的PingRow对象将正确存储地址。我在tablemodel中使用对象而不是二维数组 申请图片 按钮“neue Zeile hinzufügen”的actionlistener。这意味着“添加新行”。actionlistener实例是tablemodel中使用的PingRow对象和第二列的JButton。该操作在其actionlistener中ping主机。该操作应该以某种方式获得正确的行数或正确的行对象。在最后两行中,我向列中添加了CellRenderer和CellEditor。它们用于渲染JButtonJava JButton对于每行,只有最后一个按钮的actionlistener被激活,java,swing,jtable,Java,Swing,Jtable,嗨,我正在编写一个小应用程序,同时ping多个主机 正如你在下图中看到的,我有一个jtable,其中有一列按钮。我的问题如下:我在整个列中添加了具有相同actionListener的相同按钮。如果单击其中一个按钮,则只有最后一个按钮的actionlistener被激活,并且名称更改为单击按钮的名称。为什么? 该按钮的目标是用户在第一列中插入IP地址,并通过单击按钮开始ping。现在会向IP地址发送几个ping以确定其连接。如果用户插入IP地址,tablemodel中的PingRow对象将正确存储
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();
}
}....
解决方案
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();
}
}.....
操作
此外,变量名不应以大写字符开头。有些变量是正确的,有些则不正确。当论坛突出显示基于Java约定的类/变量时,您的代码读起来很混乱,您没有遵循这些约定。是的,这就是mouseclickListener的想法。:)@阿拉斯卡斯塔克,什么MouseListener?如果仍然卡住,一个有效的将很好地帮助我们。是的,你是对的,我将删除tablemodel,celleditor有趣的代码部分是第一个。????那不是MCVE。在删除任何其他内容之前,请阅读或重新阅读链接。另外,请查看链接以了解更多我们需要查看的内容。请从标题中删除
已解决的,并将您找到的解决方案作为正确答案发布。我试图实现教程中的提示,但仍有一些错误,我不理解。@AlaskaStack,教程的重点是替换(而不是自定义)使用教程中的代码创建渲染器/编辑器。然后你就写动作。
aJTable.rowAtPoint(evt.getPoint());