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

  • SEARCH
  • Home
  • 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 6031771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:14:04+00:00 2026-05-23T05:14:04+00:00

I have a JTable and a button next to it that calls deleteSelectedRows() ,

  • 0

I have a JTable and a button next to it that calls deleteSelectedRows(), which does exactly what it sounds like:

public void deleteSelectedRows() {
    int[] selected = jTable.getSelectedRows();
    for(int i = selected.length - 1; i >= 0; i--) {
        model.removeRow(selected[i]);
    }
    if(model.getRowCount() < 1) {
        addEmptyRow();
    }
}

But if a cell was in the act of being edited when it (and/or cells above it) were deleted, the edited cell stayed while the rest left, like this:

And then trying to exit out of the editing threw an ArrayIndexOutOfBoundsException since row 5 was trying to be accessed and there was only one row left in the table.

I then tried all sorts of fun and games with jTable.getEditingRow(). At first, adding an if(selected[i] != editing) before the removal seemed to work, but then removing rows above the edited cell caused problems.

Then I tried this:

public void deleteSelectedRows() {
    int[] selected = jTable.getSelectedRows();
    int editing = jTable.getEditingRow();
    for(int s : selected) { //PS: Is there a better way of doing a linear search?
        if(s == editing) {
            return;
        }
    }
    for(int i = selected.length - 1; i >= 0; i--) {
        model.removeRow(selected[i]);
    }
    if(model.getRowCount() < 1) {
        addEmptyRow();
    }
}

But that doesn’t delete anything, ever. Judging from printlns I sprinkled around, the last cell to be highlighted (that has the special border seen here on spam) is considered part of the editing row, and thus triggers my early return.

So I don’t really care whether the solution involves fixing the original problem–that of the wacky results when a cell being edited is deleted–or this new problem–that of getEditingRow() not behaving as I expected, it’s just that I need at least one of those to happen. That said, I would be interested to hear both solutions just out of academic curiosity. Thanks in advance.

  • 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-05-23T05:14:05+00:00Added an answer on May 23, 2026 at 5:14 am

    Try to include the following lines before removing any rows from your model:

    if (table.isEditing()) {
        table.getCellEditor().stopCellEditing();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jTable and a jButton. When clicked, the button's actionPerformed method calls
I have a JTable with 3 column of which first column is a button
I have a JTable that is within a JScrollPane. Rows are added to the
I have a JTable that I want to use to display some data (a
I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
I have a JTable that stores the results of a database query, so far
I have a standard JTable with certain data inside. I would like to create
I have a table which contains delete buttons for each row. I use int
I am trying to create a JTable that when a user presses a button
I have a JTable that is using a TableColumnModelListener() to detect when the column

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.