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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:28:25+00:00 2026-06-06T22:28:25+00:00

I face some problems when I am trying to remove rows from a table

  • 0

I face some problems when I am trying to remove rows from a table in java. In particular, I use the DefaultTableModel, and when I am trying to remove a row, using the removeRow(int row) method, the last row is removed, regardless what the row is. For example, let’s say that we have six rows. When the removeRow(0) or removeRow(2) or removeRow(5) is called, the last row is always removed. Any idea, why this is happening?

Thanks

—update

When a particular cell of the jtable is pressed, the corresponding row should be removed.

    class TagsTableMA extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e){
        Point p = e.getPoint();
        int row = tagsJT.rowAtPoint(p);
        int column = tagsJT.columnAtPoint(p);

        if (column == COLUMN_DELETE_TAG){
            DocDialog docDialog = new DocDialog(parentMainJF, 
                                                true, 
                                                null, 
                                                "Please confirm...", 
                                                "Are you sure you want to delete the \"" + 
                                                tagsJT.getValueAt(row, COLUMN_TAG_NAME) +
                                                "\" tag?",
                                                DocDialog.TYPE_YES_NO);
            docDialog.show();
            int answer = docDialog.getAnswer();

            if (answer == DocDialog.YES)                                                
                    model.removeRow(row);
        }
    }   
}

— update no2
Here is some code with my issue. I’ve removed some stuff that are irrelevant.

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MainJF extends JFrame {

public MainJF(){
    this.add(createTagsTable());
    setMinimumSize(new java.awt.Dimension(200,400));
}

 private JTable createTagsTable(){

    String[] columnNames = {"",          
                            "Tag",
                            "",
                            "",
                            ""};


    Object[][] data = new Object[10][columnNames.length];
    for (int i=0; i<data.length; i++){
        data[i][COLUMN_CHECK] = false;
        data[i][COLUMN_TAG_NAME] = "Tag " + i;
        data[i][COLUMN_TAG_ID] = i;
        data[i][COLUMN_EDIT_TAG] = "edit";
        data[i][COLUMN_DELETE_TAG] = "delete";
    }

    model = new TagsSelectionTableModel(columnNames, data);
    tagsJT = new JTable(model);

    tagsJT.setRowSelectionAllowed(true);
    tagsJT.addMouseListener(new TagsTableMA());

    return tagsJT;
}


class TagsSelectionTableModel extends DefaultTableModel{

    public TagsSelectionTableModel(String[] columnNames, Object[][] data){
        super(data, columnNames);
        this.columnNames = columnNames;
        this.data = data;
    }

    private String[] columnNames;
    private Object[][] data;


    @Override
    public Object getValueAt(int row, int col) { return data[row][col]; }
}

  class TagsTableMA extends MouseAdapter{

    @Override
    public void mousePressed(MouseEvent e){
        Point p = e.getPoint();
        int row = tagsJT.rowAtPoint(p);
        int column = tagsJT.columnAtPoint(p);

        if (column == COLUMN_DELETE_TAG){
            int irow = tagsJT.convertRowIndexToView(row);     
            model.removeRow(irow);
        }
    }
}


private JTable tagsJT;
private TagsSelectionTableModel model;   

private static int COLUMN_CHECK = 0;
private static int COLUMN_TAG_NAME = 1;
private static int COLUMN_TAG_ID = 2;
private static int COLUMN_EDIT_TAG = 3;
private static int COLUMN_DELETE_TAG = 4;


public static void main(String args[]) {
    new MainJF().setVisible(true);
}
}
  • 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-06T22:28:25+00:00Added an answer on June 6, 2026 at 10:28 pm

    The row obtained from columnAtPoint() is in view coordinates, while removeRow() assumes model coordinates. Quoting from the relevant tutorial section:

    This distinction does not matter unless your viewed data has been rearranged by sorting, filtering, or user manipulation of columns.

    If so, you will need to use convertRowIndexToModel(), described near the end of Sorting and Filtering, which advises:

    When using a sorter, always remember to translate cell coordinates.

    Also, consider using a ListSelectionListener instead of a MouseAdapter.

    Addendum: Your implementation of getValueAt() continued to access the array supplied to the constructor, while the model’s data was actually stored in the parent implementation. If you really need more control over the model’s data structure, extend AbstractTableModel, as shown here.

    import java.awt.EventQueue;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    /** @see https://stackoverflow.com/a/11237205/230513 */
    public class MainJF extends JFrame {
    
        private JTable tagsJT;
        private TagsSelectionTableModel model;
        private static int COLUMN_CHECK = 0;
        private static int COLUMN_TAG_NAME = 1;
        private static int COLUMN_TAG_ID = 2;
        private static int COLUMN_EDIT_TAG = 3;
        private static int COLUMN_DELETE_TAG = 4;
    
        public MainJF() {
            this.add(new JScrollPane(createTagsTable()));
            pack();
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        private JTable createTagsTable() {
    
            String[] columnNames = {"0", "Tag", "2", "3", "4"};
    
            Object[][] data = new Object[10][columnNames.length];
            for (int i = 0; i < data.length; i++) {
                data[i][COLUMN_CHECK] = false;
                data[i][COLUMN_TAG_NAME] = "Tag " + i;
                data[i][COLUMN_TAG_ID] = i;
                data[i][COLUMN_EDIT_TAG] = "edit";
                data[i][COLUMN_DELETE_TAG] = "delete";
            }
            model = new TagsSelectionTableModel(columnNames, data);
            tagsJT = new JTable(model);
            tagsJT.setRowSelectionAllowed(true);
            tagsJT.addMouseListener(new TagsTableMA());
            return tagsJT;
        }
    
        class TagsSelectionTableModel extends DefaultTableModel {
    
            public TagsSelectionTableModel(String[] columnNames, Object[][] data) {
                super(data, columnNames);
            }
        }
    
        class TagsTableMA extends MouseAdapter {
    
            @Override
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                int row = tagsJT.rowAtPoint(p);
                int col = tagsJT.columnAtPoint(p);
                if (col == COLUMN_DELETE_TAG) {
                    model.removeRow(row);
                }
            }
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new MainJF().setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use iTextSharp to print a grid view but I face some problems: No
I am currently developing for android and using some face recognition services. The service
I'm trying to line up some social icons (in @font-face format which why they
I am trying some programs in c face a problem with this program can
I am newbie about octopress, so i can face some problems. At the beginning,
I was trying to use Zend_Currency to format my currency outputs and i face
I face some problems while I host my web application(which is a PowerBuilder V12.0
I am trying to use jquery DataTables plugin to display details from my db
According to my previous post I am trying to get back some information from
I'm trying to run some unit tests using QUnit written in CoffeeScript but there

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.