Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to prevent mouse right-clicking on a JPopupMenu

  1. #1
    Junior Member
    Join Date
    Sep 2023
    Location
    London
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to prevent mouse right-clicking on a JPopupMenu

    I am trying to implement a GUI with Java. I bound a JPopupMenu to a JTable. The JPopupMenu becomes visible on mouse right-click on the JTable.

    I managed to prevent mouse left-clicks on the JTable. I haven't been able to prevent mouse left-clicks on any item of the JPopupMenu. In fact each JMenuItem is bound to an ActionListener whose actionPerformed has no access to the mouse buttons. May I associate a MouseListener with a JMenuItem? If that is not possible then how can I make the JPopupMenu listen to mouse right-clicking only?

    My goal is to reserve mouse left-clicks on the JTable for other tasks that are not related to the JPopupMenu. I suppose I will need to bind the JTable to another Listener. Can the same component (JTable) be associated with two different listeners?

    In the following I am pasting my code.
    Thank you in advance.


    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JPopupMenu;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    import javax.swing.border.EtchedBorder;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Dimension;
    import java.awt.Point;
    import javax.swing.UIManager;
    import java.awt.Rectangle;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingUtilities;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    import javax.swing.border.SoftBevelBorder;
    import javax.swing.border.BevelBorder;
    import javax.swing.JScrollPane;
    import javax.swing.JList;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.AbstractListModel;
    import javax.swing.DefaultListModel;
     
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Component;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Vector;
     
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
     
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
    //--------- Define ActionListener
    class PopupActionListener implements ActionListener 
    {
    	public void actionPerformed(ActionEvent actionEvent) {
    	 System.out.println("Selected: " + actionEvent.getActionCommand());
    }
    }
     
     
    public class MyNewGUI extends JFrame 
    {
     
    // INSTANCE VARIABLE/COMPONENTS
    		private JPanel ctpMain;
    		private JTextField txtNewPoint;
    		private JLabel lblNewPoint;
    		private JButton btnNewPoint;
    		private JButton btnDeleteAll;
    		private JButton btnDeleteUnused;
    		private JScrollPane scrPoints;
    		private JButton btnExit;
    		DefaultTableModel pointsTableModel = new DefaultTableModel ();
    		JTable tblPoints = new JTable(pointsTableModel);
    		JPopupMenu popupMenu = new JPopupMenu();
    		JMenuItem menuItemAddBefore;
    		JMenuItem menuItemAddAfter;
    		JMenuItem menuItemRemove;
    		JMenuItem menuItemRename;
     
    /**************************** Launch the application *********************************************/
    	public static void main(String[] args) 
    	{
    		try 
    		{
    			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    		} catch (Throwable e) 
    		{
    			e.printStackTrace();
    		}
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run() 
    			{
    				try
    				{
    					MyNewGUI frame = new MyNewGUI();
    					frame.setVisible(true);
    				} catch (Exception e) 
    				{
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
     
    //**************************  JFrame Class Constructor --> Create the frame   ***************************                                                                                                                                                                                                                                                                                                                                                    	 */
    	public MyNewGUI() 
    	{
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(new Rectangle(10, 10, 100, 100));
    		setMinimumSize(new Dimension(100, 100));
    		setFont(new Font("Arial", Font.BOLD, 16));
    		setBounds(1050,200,800,800);
    		setTitle("MyNewGUI");
    		initComponents();
     
    		btnExit = new JButton("Exit GUI");
    		btnExit.setFont(new Font("Arial", Font.BOLD, 16));
    		btnExit.setBounds(14, 254, 101, 26);
    		ctpMain.add(btnExit);
    		setVisible(true);
    		createEvents();
    	}
     
    	/////////////////////////////////////////////////////////////////////////////////////////////////////////
    	//  This method contains all of the code for creating and initializing components
    	/////////////////////////////////////////////////////////////////////////////////////////////////////////
    	private void initComponents()
    	{
    		setTitle("MyGUI");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 1041, 779);
    		ctpMain = new JPanel();
    		ctpMain.setBounds(new Rectangle(5, 5, 0, 0));
    		ctpMain.setAlignmentY(5.0f);
    		ctpMain.setAlignmentX(5.0f);
    		ctpMain.setSize(new Dimension(10, 10));
    		ctpMain.setPreferredSize(new Dimension(100, 100));
    		ctpMain.setLocation(new Point(10, 10));
    		ctpMain.setFont(new Font("Arial", Font.BOLD, 16));
    		ctpMain.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    		setContentPane(ctpMain);
     
    		btnNewPoint = new JButton("Add New Point");
     
    		btnNewPoint.setBounds(14, 55, 167, 29);
    		btnNewPoint.setFont(new Font("Arial", Font.BOLD, 16));
     
    		txtNewPoint = new JTextField();
    		txtNewPoint.setBounds(162, 14, 144, 29);
    		txtNewPoint.setFont(new Font("Arial", Font.BOLD, 16));
    		txtNewPoint.setColumns(10);
     
    		lblNewPoint = new JLabel("  Point Identifier");
    		lblNewPoint.setLocation(new Point(14, 19));
    		lblNewPoint.setSize(new Dimension(136, 19));
    		lblNewPoint.setDisplayedMnemonicIndex(1);
    		lblNewPoint.setAutoscrolls(true);
    		lblNewPoint.setFont(new Font("Arial", Font.BOLD, 16));
     
    		JTextArea textArea = new JTextArea();
    		textArea.setBounds(179, 197, 1, 16);
    		textArea.setText("");
     
    		JPanel pnPoints = new JPanel();
    		pnPoints.setBounds(321, 21, 211, 668);
    		pnPoints.setFont(new Font("Arial", Font.BOLD, 24));
    		pnPoints.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(64, 0, 64)));
    		ctpMain.setLayout(null);
    		ctpMain.add(btnNewPoint);
    		ctpMain.add(txtNewPoint);
    		ctpMain.add(lblNewPoint);
    		ctpMain.add(textArea);
    		ctpMain.add(pnPoints);
    		pnPoints.setLayout(null);
     
    		scrPoints = new JScrollPane();
    		scrPoints.setBounds(0, 0, 206, 668);
    		pnPoints.add(scrPoints);
     
    // --------- Initialize JTable Model with points fromCliff's code
    		pointsTableModel.addColumn("Point Names");
    		pointsTableModel.insertRow(0, new Object[] { "ONE" });
    		pointsTableModel.insertRow(0, new Object[] { "TWO" });	 
    		pointsTableModel.insertRow(0, new Object[] { "THREE" });	 
    		scrPoints.setViewportView(tblPoints);
    		tblPoints.setFocusable(true);
    		tblPoints.setRowSelectionAllowed(true);
    		tblPoints.setVisible(true);
     
     
    // --------- Initialize popupMenu with items ---
    		PopupListener myListener = new PopupListener();
    		menuItemAddBefore = new JMenuItem("Add Point Before");
    	//	menuItemAddBefore.addMouseListener(myListener);
    		menuItemAddAfter = new JMenuItem("Add Point After");
    	//	menuItemAddAfter.addMouseListener(myListener);
    		menuItemRemove = new JMenuItem("Remove Current Point");
    	//	menuItemRemove.addMouseListener(myListener);
    		menuItemRename = new JMenuItem("Rename Current Point");
    	//	menuItemRename.addMouseListener(myListener);
    		popupMenu.add(menuItemAddBefore);
    		popupMenu.add(menuItemAddAfter);
    		popupMenu.add(menuItemRemove);
    		popupMenu.add(menuItemRename);
     
    // --------- Set popupMenu to tblPoints
    		tblPoints.setComponentPopupMenu(popupMenu);	
    		tblPoints.addMouseListener(myListener);
    	//	tblPoints.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
     
    		btnDeleteAll = new JButton("Delete All Points");
    		btnDeleteAll.setFont(new Font("Arial", Font.BOLD, 16));
    		btnDeleteAll.setBounds(14, 192, 167, 26);
    		ctpMain.add(btnDeleteAll);
     
    		btnDeleteUnused = new JButton("Delete Unused Points");
    		btnDeleteUnused.setFont(new Font("Arial", Font.BOLD, 16));
    		btnDeleteUnused.setBounds(14, 126, 205, 26);
    		ctpMain.add(btnDeleteUnused);
    	}
     
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    //  This method contains all of the code for creating events
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    	private void createEvents() 
    	{
     
    //-------- Append Point To Point Names List ------------------------------------------------------------
    		btnNewPoint.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				String newPoint = txtNewPoint.getText();	
    				pointsTableModel.addRow(new Object[] {newPoint});
    				txtNewPoint.setText("");
    			}
    		});
     
    //-------- Remove Unused Points From Point Names List ---------------------------------------------------
    		btnDeleteUnused.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				// TO BE DEVELOPED
    			}
    		});
     
    //-------- Empty Point Names List --------------------------------------------------------------------------		
    		btnDeleteAll.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				while (pointsTableModel.getRowCount() > 0) 
    					pointsTableModel.removeRow(0);
    			}
    		});
     
    //--------------- Button EXIT Handler --------------------------------------------------------------------------------------------------------------------
    		btnExit.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				Object[] options = {"Exit Without Saving", "Exit With Saving","Cancel"};    	//Custom button text
    				int sel = JOptionPane.showOptionDialog(null ,
    				    "Would you like to save your work before exiting ? ",
    				    "EXIT WORK SESSION",
    				    JOptionPane.YES_NO_CANCEL_OPTION,
    				    JOptionPane.QUESTION_MESSAGE,
    				    null,
    				    options,
    				    options[2]);
    // Process Customer's Selection
    				if (sel == 0) 
    				{
    					 JOptionPane.showMessageDialog(null, "You chose to exit your work session without saving!");
    					 System.exit(0);	
    				}
    				if(sel == 1) 
    				{
    					 JOptionPane.showMessageDialog(null, "You chose to save your work session before exiting !");
    //					 if(lstPoints != null && !pointsTableModel.isEmpty())  // If JList contains Points
    //					 {
    //						 pointsList.clear();     // Empty Points storage
    //						 System.out.print("\n pointsList after clear():  " + pointsList);       // DEBUG-PURPOSE
    //						 System.out.print("\n pointsTableModel :  " + pointsTableModel);         // DEBUG-PURPOSE
    //						 System.out.print("\n pointsTableModel size:  " + pointsTableModel.size() );        // DEBUG-PURPOSE
    //						 System.out.print("\n");
    //						 for(int i=0; i < pointsTableModel.size(); i++)
    //							 pointsList.add(pointsTableModel.get(i));    // Move Points from JList to Points storage
    //						 System.out.print("Saved Points:  " + pointsList + "\n");
    //					 }else 
    //						 JOptionPane.showMessageDialog(null, "Your Ponts List is empty!"); 
    //					 System.exit(0);	
    				}
    				if(sel == 2) 
    					 JOptionPane.showMessageDialog(null, "You changed your mind !");
    			}
    		});
     
    //--------------- JMenuItem Handler To Add Item Before Selected JTable Row-------------------------------------------------------------------------------------------------------
    		menuItemAddBefore.addActionListener(new ActionListener() 
    		{
    		    public void actionPerformed(ActionEvent ev) 
    		    {
    		    	int row = tblPoints.getSelectedRow();
    				  if (row == -1) 
    					  return;
    		    	System.out.print("Add Point to tblPoints Before Row:  " + row + "\n");
    		    	String precedingPointName = JOptionPane.showInputDialog("Enter Following Point Name");
    				System.out.println(precedingPointName);   
    		    	pointsTableModel.insertRow(row, new Object[] {precedingPointName});
    		    	tblPoints.getSelectionModel().clearSelection();
    		    }
    		});	
     
    //--------------- JMenuItem Handler To Add Item After Selected JTable Row-------------------------------------------------------------------------------------------------------
    		menuItemAddAfter.addActionListener(new ActionListener() 
    		{
    		    public void actionPerformed(ActionEvent ev) 
    		    {
    		    	int row = tblPoints.getSelectedRow();
    				  if (row == -1 || !tblPoints.isCellEditable(row,0)) 
    					  return;
    				String value = tblPoints.getValueAt(row, 0).toString(); 
    		    	System.out.print("Add Point to tblPoints After PointName: " + value + " On  Row:  " + row + "\n");
    		    	String followingPointName = JOptionPane.showInputDialog("Enter Following Point Name");
    				System.out.println(followingPointName);   
    		    	pointsTableModel.insertRow(row +1, new Object[] {followingPointName});
    		    	tblPoints.getSelectionModel().clearSelection();
    		    }
    		});			
     
    //--------------- JMenuItem Handler To Remove Selected JTable Row----------------------------------------------------------------------------------------------------------------
    		menuItemRemove.addActionListener(new ActionListener() 
    		{
    		    public void actionPerformed(ActionEvent ev) 
    		    {
    		    	int row = tblPoints.getSelectedRow();
    				  if (row == -1) 
    					  return;
    		    	System.out.print("Remove Point at tblPoints rown:  " + row + "\n");
    		    	pointsTableModel.removeRow(tblPoints.getSelectedRow());
    		    	tblPoints.getSelectionModel().clearSelection();
    		    }
    		});	
     
    //--------------- JMenuItem Handler To Rename Item At Selected JTable Row----------------------------------------------------------------------------------------------------------------
    		menuItemRename.addActionListener(new ActionListener() 
    		{
    		    public void actionPerformed(ActionEvent ev) 
    		    {
    		    	int row = tblPoints.getSelectedRow();
    				 if (row == -1) 
    					  return;
    		    	 System.out.print("Rename Point at tblPoints rown:  " +  row + "\n");
    		    	 String replacingPointName = JOptionPane.showInputDialog("Enter Current Point Replacing Name");
    		    	 tblPoints.getModel().setValueAt(replacingPointName, row, 0);
    		    	 tblPoints.getSelectionModel().clearSelection();
    		    }
    		});			
     
    	}  //-----------------------------------  End Of  createEvents() Method ----------------------------------------------------------------------	
     
    // ********* Inner classes *********
     
    	public class PopupListener extends MouseAdapter
    	{
    		 public PopupListener()                 //  Default Constructor
    		 {
    	 	    System.out.print("\n Default PopupListener Constructor engaged \n ");
    		 }
     
    		 public void mousePressed(MouseEvent e) 
     
    		 {
    			if(e.getButton() == MouseEvent.BUTTON3)
    	 		{
    				  Point p = e.getPoint();
    				  Component c = e.getComponent();
    				  int row = tblPoints.rowAtPoint(p);
    				  System.out.print("\n `Selected row:  " + row + "\n");
    				  if (row == -1)
    				  {
    					  tblPoints.getSelectionModel().clearSelection();
    					  return;
    				  }
    				  else
    					  tblPoints.setRowSelectionInterval(row, row);
    		    }
    			else
    				tblPoints.getSelectionModel().clearSelection();
    	     }
         }
    }

  2. #2
    Member
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to prevent mouse right-clicking on a JPopupMenu

    Left click and right click mouse buttons. // the java.awt.mouseMotionListener will control the left and right click buttons automatically. This is a hardware issue and the Java API programs already have it set for anyone to use. That would be the correct method to use to get the mouse pad working on the GUI container.

Similar Threads

  1. [SOLVED] close JPopupMenu after focus lost
    By cnmeysam in forum Java SE APIs
    Replies: 42
    Last Post: October 19th, 2022, 11:54 AM
  2. How do I prevent these exceptions from occurring?
    By CodeBro in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2014, 04:47 PM
  3. Clicking a coordinate on my GUI (Not Local Mouse Or Normal Robot.movemouse!)
    By testingjava in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 30th, 2012, 06:28 AM
  4. Prevent object going outside of screen?
    By JakkyD in forum Java Theory & Questions
    Replies: 2
    Last Post: May 7th, 2012, 10:56 AM
  5. Replies: 1
    Last Post: May 5th, 2012, 05:00 PM

Tags for this Thread