Question

The following is my code for attempting to add create a JButton which will add a row to the connected 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. 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.

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. The issue here is that when I go to press the button, the wrong actionListener is triggered. For instance, pressing "Add Row" in Frame 0 should trigger addRow[0], but instead triggers addRow[4].

Was it helpful?

Solution

You're adding a row to the table at tables[currentUser] whenever any button is clicked. 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:

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 personally would accomplish this by subclassing ActionListener and passing the table index as a parameter to the constructor, but that's just me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top