简体   繁体   English

通过使用JButton的actionListener向JTable添加一行

[英]Adding a row to a JTable by use of a JButton's actionListener

The following is my code for attempting to add create a JButton which will add a row to the connected JTable. 以下是我尝试添加创建JButton的代码,该JButton将向连接的JTable添加一行。

My variables are shown below, tables and tbm are created but were initialized in another part of the program that is not currently shown. 我的变量如下所示,表和tbm已创建,但已在当前未显示的程序的另一部分中初始化。 These are all instance variables. 这些都是实例变量。

int currentUser = 0;
JTable[] tables = new JTable[5];
DefaultTableModel[] tbm = new DefaultTableModel[5];
JButton[] addRow = new JButton[5]

This is the code to create JButtons with actionlisteners. 这是使用actionlisteners创建JButton的代码。

for(int i=0;i<tbm.length;i++) {
addRow[i] = new JButton("Add Row");
selectionModel = tables[i].getSelectionModel();
currentUser=i;
addRow[i].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
Object[] temp = {"",""};
tbm[currentUser].addRow(temp);
selectionModel.setSelectionInterval(tbm[currentUser].getRowCount()-1,tbm[currentUser].getRowCount()-1);
}});
}

I later assemble the JTable and JButton into a JPanel by use of a for loop running from 0-tables.length and put it in a respective JFrame. 稍后,我使用从0-tables.length运行的for循环,将JTable和JButton组装为一个JPanel,并将其放入相应的JFrame中。 The issue here is that when I go to press the button, the wrong actionListener is triggered. 这里的问题是,当我按下按钮时,将触发错误的actionListener。 For instance, pressing "Add Row" in Frame 0 should trigger addRow[0], but instead triggers addRow[4]. 例如,在第0帧中按“添加行”应触发addRow [0],但触发addRow [4]。

You're adding a row to the table at tables[currentUser] whenever any button is clicked. 每当单击任何按钮时,您都将在tables[currentUser]添加一行。 It sounds like you want to add a row to table[X] when you click button X. Here's the quick and dirty way to do it: 听起来您想在单击按钮X时向table[X]添加一行。这是一种快速而肮脏的方法:

for(int i=0;i<tbm.length;i++) {
    final int tblIdx = i;
    addRow[i] = new JButton("Add Row");
    selectionModel = tables[i].getSelectionModel();
    currentUser=i;
    addRow[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Object[] temp = {"",""};
            tbm[tblIdx].addRow(temp);
            selectionModel.setSelectionInterval(tbm[tblIdx].getRowCount()-1,tbm[tblIdx].getRowCount()-1);
        }
    });
}

You can't pass i directly into your anonymous ActionListener since it's not a final variable, so at the beginning of each iteration through the loop a temporary final variable tblIdx is created and set to whatever i currently is. 你可以不通过i直接进入您的匿名ActionListener因为它不是最终的变数,所以在每次迭代通过循环开始的临时final变量tblIdx创建并设置为任何i目前是。

I personally would accomplish this by subclassing ActionListener and passing the table index as a parameter to the constructor, but that's just me. 我个人将通过ActionListener并将表索引作为参数传递给构造函数来实现此目的,但这仅是我自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM