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

ADempiere DVD by Trifon Trifonov

Today's Enterprise software, particularly ERP-CRM is quite complex for use and administration. The same applies for sources and setup of workspace and build. Quite often in forums of ADempiere ERP-CRM we see complains that setup of development environment is complicated and labour extensive. Many times developers spend 2-3 days trying to setup workspace without result and post desperate messages in forums asking for assistance. In order to decrease time spent for setup i created ADempiere DVD which contains all ADempiere sources, Eclipse IDE and Sun JDK. All properly setuped and ready for use. Developer only need to copy one directory to his local hard drive and start customizing and extending ADempiere ERP-CRM suite. Currently there are two editions available: for Windows and Linux Operating systems. Trifon Trifonov

Compiere - Beginning of the end!

Compiere Inc. has very strange understandings for free: http://compiere.com/support/service-packs.php Is there a charge for downloading these service packs? The Service Pack is free of charge for customers on Standard and Extended level support subscriptions. For 2.6.3, the Service Pack is also available to customers on Self-service Support Subscriptions at no charge. Acording to the post all is free, but user must have subscription... We at ADempiere are totaly free as our migration and bug fixes which are more than 500 till now are free even for non subscribed users. One big THANK YOU to Compiere Inc. for leaving the battle field OPEN SOURCE to ADempiere and other forks. Trifon Trifonov

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