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 8484419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:22:34+00:00 2026-06-10T20:22:34+00:00

I needed to delete rows from a JTable on the delete key-press. So the

  • 0

I needed to delete rows from a JTable on the delete key-press. So the use case is quite simple, the user select some rows, press the delete key, the rows get deleted. The code is also very simple :

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    int[] selectedRows = table.getSelectedRows();
    for (int i = selectedRows.length - 1; i > -1; i--) {
        tableModel.removeRow(selectedRows[i]);
    }

The problem is that after the deletion gets finished we’ll hear a sound beep (I’m on windows, typical windows beep), as when pressing the delete key in an empty text box (or when the caret is at the end of the text).
What I thing is happening is that the key press is dispatched further to the text component that displays the text content of the cell(the first cell after the deleted ones). The beep is fired by the DefaultEditorKit$DeleteNextCharAction#actionPerformed method because there is no character ahead the dot.
As a hack I modify the event in the listener :

e.setKeyCode(KeyEvent.VK_SHIFT) // see JTable#processKeyBinding

the event does not get forwarded further, so the beep dissapear but I think it’s a bad solution and there’s a better one. But which is that better solution ?

  • 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-10T20:22:35+00:00Added an answer on June 10, 2026 at 8:22 pm

    Use key bindings instead…

    InputMap im = table.getInputMap(JTable.WHEN_FOCUSED);
    ActionMap am = table.getActionMap();
    
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "DeleteRow");
    am.put("DeleteRow", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
    
            System.out.println("Delete row");
            int row = table.getSelectedRow();
    
            if (row > -1) {
    
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                model.removeRow(row);
    
            }
    
        }
    });
    

    (I borrowed mKorbel data for my test, so my test was using a DefaultTableModel, you will need to cast to the model you are using).

    Also, if you editing, this may still fire, so you will need to check for that

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

Sidebar

Related Questions

i needed help. How do i get domain from a string? For example: Hi
I'm trying to use this query to delete the rows that are already on
I am trying to write some SQL to automatically delete some records from the
I get xml data and I get value from it. I need to delete
DELETE FROM LIM_STAGE.RESULTS A WHERE ROWID > ( SELECT min(1) FROM LIM_STAGE.RESULTS B WHERE
I needed to retain the words enclosed in brackets and delete the others in
I needed to change a work item field from PlainText -> String. As I
In my web application, I was dynamically adding/deleting rows from my HTML table using
I use Git and needed to send many many... git init . ... git
Is possible to delete bundles not needed in order to keep the project clean?

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.