Skip to main content

How to catch right mouse click in JTable

This is a quick post to show how developer can intercept Right mouse click in JTable. Name  of our JTable object is "jTable". We need to add MouseListener and catch "MousePressed" events. As the code shows correct mouse button is determined using "SwingUtilities" class, quite easy job. Tricky part is finding Row number and Column number and displaying Pop-up window at correct position.

Enjoy!
jTable.addMouseListener( new MouseAdapter()
{
 public void mousePressed( MouseEvent event )
 {
  if ( SwingUtilities.isLeftMouseButton( event ) ) {
   // Do something
  } else if ( SwingUtilities.isRightMouseButton( event ) ) {
   Point p = event.getPoint();
 
   // Get row and column index that contains our coordinate
   int rowIndex = jTable.rowAtPoint( p );
   int colIndex = jTable.columnAtPoint( p );
   
   // Get ListSelectionModel of the JTable
   ListSelectionModel model = jTable.getSelectionModel();
 
   // Set how many rows are selected using "rowIndex".
   // If beginning and end selection indexes are equal then 
                        // only one row is selected.
   model.setSelectionInterval( rowIndex, rowIndex );
  }
  
  // Show popup
  if (event.isPopupTrigger() && event.getComponent() instanceof JTable ) {
                  JPopupMenu popup = createMyPopUp();
                  popup.show(event.getComponent(), event.getX(), event.getY());
                }
        }
});

Comments

Popular posts from this blog

Compiere Inc. still do not know how to make developer friendly environment

I work with Compiere and it's fork Adempiere since 2.5 years. During all this years till Adempiere was born i had big headache each time new version was produced by Compiere Inc. because of the lack of proper Eclipse IDE setting. I had to manually set project setting inside Eclipse for all Compiere ERP-CRM sub-projects. This process took around 1-2 hours if all source files were provided by Compiere Inc. There were cases when Compiere Inc. forgot for given class and source were non-compilable. A recent post from Enrique Ruibal reminded me about this past headaches: https://sourceforge.net/forum/message.php?msg_id=4679608 Adempiere() is much more mature and developer friendly project. At least i'm trying to keep all IDE setting up to date and working. Good luck

Compiere vs. ADempiere: Count of Partners

ADempiere project is one year old now, more precisely 13 months old. Before one year father project Compiere had around 60 partners, which was quite good number. Each partner was obliged to pay 3 000 USD per year which made 180 000 USD income only from partners. Today Compiere has 32 partners and each pays 8 000 USD = 256 000 USD year. It looks that Compiere Inc. managed to increase it's income, but number of partner dropped by half. It is Compiere inc. decision how much to charge for partnership, i'm not going to discuss this now. But it is very interesting observation that although income from partners increased Compiere Inc. decided to close source code access to Service Packs and push users to pay for it. It looks that only money from partners are not enough for the Inc.. Well it is again Compiere Inc. right to decide how much to charge for access to source code and how much not to charge, but users can see that functionality which ADempiere provides get bigger and bigger e...