Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:08:47+00:00 2026-06-05T09:08:47+00:00

The following is my code for attempting to add create a JButton which will

  • 0

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].

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:08:49+00:00Added an answer on June 5, 2026 at 9:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create the following code through the use of CodeDom: public
Following code iterates through many data-rows, calcs some score per row and then sorts
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666
I am attempting to create a MySQL snippet that will analyse a table and
I have the following code inside a Form, and am attempting to find a
I'm attempting to create a recursive routine that will retrieve PropertyInfos for all members
I have the following code (attempting to log a user in programatically): List<GrantedAuthority> authorities
I'm attempting to update my database table with the following code: public void onUpgrade(SQLiteDatabase
The following code is a helper I've created for a system which allows administrators
I'm attempting to translate the following C# code into IronPython but I don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.