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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:41:08+00:00 2026-05-29T15:41:08+00:00

I am making a java project and i am using JTable . The thing

  • 0

I am making a java project and i am using JTable.
The thing i want to do is link a popupmenu to the jtable. The popupmenu has four different items. Some of the items need to know selected row in jtable. The problem is in selectRow -function. i cannot get a function to return a proper row. i click on a jtable with right mouse button and i get a popupmenu popped in the same column i clicked as it should be. but when i use addSelectedRow or removeSelectedRow -functions (in menuItem with label “addSelected” or “deleteSelected”) and any row(s) is no selected i use selectRow -function to find a row i am clicking at with right mousebutton. Seems that rowAtPoint(Point p) cant not just find a right row. It returns eather 0 or -1. I have been struggling with this problem almost a week so pleace pimp my code 😀 i hope code is clean enough 😀

<–Edited–>
http://painkiller.comlu.com/images/1.jpg

Here is a screenshot of a problem.
JTable has allways at least one row. I click arbitrary any row (at this situation second row). Row is not selected by purpose. Now if i press “Lisää valittu” – which means “Add selected“, function selectRow() will be called.

/**Locations

jTbl.getLocation(): java.awt.Point[x=0,y=0]
e.getPoints() java.awt.Point[x=4,y=8]
SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), jTbl) java.awt.Point[x=350,y=31]

first i tried e.getPoint()
* e.getPoints() -> java.awt.Point[x=4,y=8]
returned row index number from rowAtPoint(): 0

then i tried SwingUtilities.convertPoint()
* SwingUtilities.convertPoint(tablePopMenu, e.getPoint(), jTbl); -> java.awt.Point[x=28,y=10]
returned row index number from rowAtPoint(): 2

so it seems to work but sometimes function throws -1 or 0. (need to ckeck out that bug) but mostly works pritty good.

<– –>

private void TableMousePressed(java.awt.event.MouseEvent evt) {
        try
        {
            /*
             * The popupmenu content (items) are standart for all tables
             * The label of popupmenu items are used to select proper 
             * command in select case
             * 1. Add selected-> Add only selected row(s)
             * 2. Add all -> Add all rows
             * 3. Delete selected -> Remove only selected row(s)
             * 4. Delete all-> Remove all row(s)
            */

            javax.swing.JTable temp;    //Temporar jTable alien
            String label = ((javax.swing.JMenuItem)evt.getSource()).getText();   //Switch Case


            temp = (javax.swing.JTable)((JPopupMenu)(evt.getComponent().getParent())).getInvoker();
            switch(label)   //Select rigt method by jMenuItem.getText() method (the label of menu item)            {
                case "Add selected":
                    addSelectedRow(evt, temp);
                    break;
                case "Add all":
                    addAllRows(temp);
                    break;
                case "Delete selected":
                    removeSelectedRow(evt, temp);
                    break;
                case "Delete all":
                    removeAllRows(temp);
                    break;
            }            
        }
        catch(Exception ex){ex.printStackTrace();}
    }

    /**
     * @param e - MouseEvent
     * @param jTbl - JTable which will be used
     */
    private void addSelectedRow(java.awt.event.MouseEvent e, javax.swing.JTable jTbl){
        if(jTbl.getSelectedRow() == -1){ //If no row(s) is selected 
            selectRow(e, jTbl); //select row
        }  
        int cellCount = jTbl.getModel().getColumnCount(); //amount of columns to apply
        int rows[] = jTbl.getSelectedRows(); //amount of rows to apply
        cellCollection = new Object[jTbl.getModel().getColumnCount()]; //Object which contains row and cells. will be added to a jTable as a new row

        for(int row = 0; row < rows.length; row++){ //Creates new row by excisting data
            for(int cell = 0; cell < cellCount; cell++){
                cellCollection[cell] = jTbl.getModel().getValueAt(rows[row], cell);
            }
            ((DefaultTableModel)jTbl.getModel()).addRow(cellCollection); //Adds a new row with data
        }         
    }

    /**
     * @param jTbl - JTable which will be used
     */
    private void addAllRows(javax.swing.JTable jTbl){
        int rowCount = jTbl.getModel().getRowCount(); //Amount of adding rows
        int cellCount = jTbl.getModel().getColumnCount(); //Amount of adding colums
        cellCollection = new Object[jTbl.getModel().getColumnCount()]; //New object which contains a row with columns filled with excisting data in a for-loop

        for(int row = 0; row < rowCount; row++){ 
            for(int cell = 0; cell < cellCount; cell++){
                cellCollection[cell] = jTbl.getModel().getValueAt(row, cell);
            }
            ((DefaultTableModel)jTbl.getModel()).addRow(cellCollection);
        }     
    }

    /**
     * @param e - MouseEvent
     * @param jTbl - JTable which will be used
     */
    private void removeSelectedRow(java.awt.event.MouseEvent e, javax.swing.JTable jTbl){
        if(jTbl.getSelectedRow() == -1){ //If no row(s) is selected
            selectRow(e, jTbl); //selects a row
        }
        int[] rows = jTbl.getSelectedRows(); //Get selected row(s)
        for(int i = rows.length - 1; i >= 0; i--)
            ((DefaultTableModel)jTbl.getModel()).removeRow(rows[i]); //Removes the rows
        getTableToLife(jTbl); //Adds a new empty row if jtable has no rows after removing
    }

    /**
     * @param jTbl - JTable which will be used
     */
    private void removeAllRows(javax.swing.JTable jTbl){
        int rowCount = jTbl.getModel().getRowCount(); //Amount of removing rows
        for(int row = rowCount-1; row >=0; row--){ 
            ((DefaultTableModel)jTbl.getModel()).removeRow(row); //Remove rows
        }
        getTableToLife(jTbl);//Adds a new empty row if jtable has no rows after removing        
    }

    /**
     * @param jTbl - JTable which will be saved
     */
    private void getTableToLife(javax.swing.JTable jTbl){
        if(jTbl.getModel().getRowCount() == 0){ //If jtable has no rows
            int cellCount = jTbl.getModel().getColumnCount(); //How many cells will be added
            cellCollection = new Object[cellCount];
            for(int cell = 0; cell < cellCount; cell++){
                cellCollection[cell] = null; //No new data is required                }   
            ((DefaultTableModel)jTbl.getModel()).addRow(cellCollection); //Adding a new empty row
        }
    }

    /**
     * @param e - Mouse event
     */
    private void selectRow(java.awt.event.MouseEvent e, javax.swing.JTable jTbl)
    {

    //      This is the main problem at the moment

    //    System.out.print("\r " + jTbl.rowAtPoint(e.getPoint()));
    //    ListSelectionModel selector = jTbl.getSelectionModel();
    //    selector.removeSelectionInterval(0, jTbl.getHeight()); //I though it he could help, but no effect (row [0] seems to be selected
    //    int p = jTbl.rowAtPoint(e.getPoint()); //or this function seems to be uncapable to find right row i clicked on jtable
    //    selector.setSelectionInterval(p, p); //returns 0 or -1. reason is unknown

    //    New solution
    ListSelectionModel selector = jTbl.getSelectionModel();
    Point newP = SwingUtilities.convertPoint(tablePopMenu, e.getPoint(), jTbl);


    int p = jTbl.rowAtPoint(newP);
    System.out.print("\rRow number: " + (p));
    if(p <=0)
        selector.setSelectionInterval(p, p);
    else
        selector.setSelectionInterval(p-1, p-1);
    }

Thank you people for saving my day!

  • 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-29T15:41:08+00:00Added an answer on May 29, 2026 at 3:41 pm

    I am not entirely sure but it is might be that the getPoint() method of the MouseEvent returns a Point in the coordinate space of the popup menu.

    What JTable’s rowAtPoint(p) expects is a Point within the coordinate space of the table.

    You can use SwingUtilities.convertPoint() to translate the coordinates.

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

Sidebar

Related Questions

Using command line git with Java. Our project has tons of nested packages which
I'm making an application where I need to scroll some text on a java.awt.Canvas
I am making a distributed java app for which I need both parts of
I am making a game in JAVA where I want to come up with
I am making an sqlight using eclipse outside the android project what should I
I am making a simple 2D game in java... I am using java's Graphics2D
I want to use java classes created in pure java project in my blackberry
I am using hudson CI to manage a straight java web project, using ant
I'm making a basic game in Java using the LWJGL Library via Netbeans. I've
i'm making a JSF2.0 project using mojarra primefaces tomcat6.x. I made a select list

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.