0

I've been trying to get my action listener to select the row where my mouse right clicks. I referred alot of posts and these are some of them:

Stack overflow

Code Guru

Code Ranch

Heres my code:

t1 is the name of the table

public void clickListener(){
        final RowPopUp pops = new RowPopUp(jTable1);
        jTable1.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent me){
                if(SwingUtilities.isRightMouseButton(me)){
                    int row = jTable1.rowAtPoint( me.getPoint() );
                    jTable1.changeSelection( row, 0, false, false );
                    
                    pops.getRow(jTable1.getSelectedRow());
                    pops.show(me.getComponent(),me.getX(),me.getY());   
                }
            }
        });
    }

now heres what happens.

when I right click on my table with no selected row, I get a Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 error however, when there is an existing selected row on my table, the code works and chooses on the row I right click into.

what I want to achieve is when I right click anywhere on the rows even if there is no selected row, the part I right click into will be selected. any ideas how will this work?

Community
  • 1
  • 1
Bumpy
  • 97
  • 11
  • When asking for debugging help, essentially, "why isn't this code working as expected?" help, you should consider creating and posting the shortest code necessary for us to be able to compile, run and help identify your problem, a [mcve] (please check the link). And yes, what I'm requesting would not be an easy or quick thing to do as it would require quite a bit of effort on your part, but if you remain stuck and need a solution to this, it would be effort well spent. – Hovercraft Full Of Eels Jan 25 '16 at 03:13
  • thanks for the response, I created a short code and surprisingly, it works.. but not on my main project.. I'll try to re-isolate them again. – Bumpy Jan 25 '16 at 03:33
  • That's the key to solving this issue -- **isolating** the bug first. Good luck. – Hovercraft Full Of Eels Jan 25 '16 at 03:37
  • 2
    thanks! I finally solved it. the error was because I am extracting data from the table before selecting the row. again, thank you for the guide on how to isolate the code. it helped. – Bumpy Jan 25 '16 at 03:44

0 Answers0