Results 1 to 7 of 7
  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default [SOLVED] Intercepting mouse clicks

    I have a JFrame, which has a JPanel and a JMenuBar in it. That JPanel has several more JPanels in it, each with several other components in it.

    I've created a JPopupMenu that responds to right clicks, but to do so I had to code a mouse event for each component that has right-click actions... That is quite tiresome.

    Much worse though is that if the user now wants to remove the popup, he would generally want to just click somewhere else on the screen. That can't work though, as only certain components are coded to intercept mouse events.

    I tried to create the mouse event handler at the level of the main JFrame, and at the main JPanel, but that still didn't work. I know there is some sort of GlassPane that can intercept actions right "on top" of everything else, but I don't know how to simply implement it...

    Any advice? just need a simple method to intercept mouse clicks anywhere in my JFrame...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Caveat: I'm no expert in this.

    But AFAIK, the glasspane is already implemented, so perhaps all you need to do is extract it via getGlassPane or somesuch similar, and add a mouselistener to it?

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    That is what I thought. This is what I tried:
    Java Code:
    JPanel glass = (JPanel) frame.getGlassPane();
    glass.addMouseListener(new MouseAdapter() {
    	public void mousePressed(MouseEvent evt) {
    		System.out.println("Hello World!");
    	}
    });
    frame above is the main JFrame...

    I can click all I want, but no 'Hello World!'
    If I add the same action code to a JList say, it works perfectly...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps you need to set the glasspane as visible? Again, I'm no expert in this.

  5. #5
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    hehe. Yup, that's it. Thanks for the pointer.
    SOLVED.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome. For any who are interested, this is the small "proof of concept" app that I created:
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    
    public class TestGlassPane
    {
      private static final String GLASS_INACTIVE = "Glass Inactive";
      private static final String GLASS_ACTIVE = "Glass Active for 10 sec";
      private static final int DELAY = 10000;
      private JPanel mainPanel = new JPanel();
      private JLabel statusLabel = new JLabel(GLASS_INACTIVE);
      private JPanel glasspane;
      private JButton activateGlassBtn = new JButton("Activate GlassPane");
      
      public TestGlassPane(JFrame frame)
      {
        activateGlassBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            statusLabel.setText(GLASS_ACTIVE);
            glasspane.setVisible(true);
            Timer timer = new Timer(DELAY, new ActionListener()
            {
              public void actionPerformed(ActionEvent e)
              {
                glasspane.setVisible(false);
                statusLabel.setText(GLASS_INACTIVE);
              }
            });
            timer.setRepeats(false);
            timer.start();
          }
        });
        
        JButton otherBtn = new JButton("Other Button");
        otherBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.out.println("Other Button Pressed");
          }
        });
        
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        mainPanel.add(activateGlassBtn);
        mainPanel.add(Box.createVerticalStrut(15));
        mainPanel.add(otherBtn);
        mainPanel.add(Box.createVerticalStrut(15));
        mainPanel.add(new JTextField(12));
        mainPanel.add(Box.createVerticalStrut(15));
        mainPanel.add(statusLabel);
        
        glasspane = (JPanel)frame.getGlassPane();
        glasspane.addMouseListener(new MyMouseListener());
        frame.setGlassPane(glasspane);
      }
      
      public JComponent getComponent()
      {
        return mainPanel;
      }
      
      private class MyMouseListener extends MouseAdapter
      {
        @Override
        public void mousePressed(MouseEvent e)
        {
          System.out.println("GlassPane Pressed");
        }
      }
      
      private static void createAndShowGUI()
      {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new TestGlassPane(frame).getComponent());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
      
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
      }
    }

  7. #7
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Well making the GlassPane visible solved it, but introduced a big problem: no other components worked anymore, as the GlassPane 'blocked' everything else. I've solved it, and removed the need for a GlassPane altogether though:
    Java Code:
    myJList.addMouseListener(new MouseAdapter() {
    	public void mousePressed(MouseEvent evt) {
    		if (evt.getButton() == MouseEvent.BUTTON3) {
    			JPopupMenu popup = new JPopupMenu();
    			popup.add("Hello");
    			popup.setLocation(evt.getXOnScreen(), evt.getYOnScreen());
    			popup.setVisible(true);
    
    			//this intercepts the next mouse click
    			myJFrame.addMouseListener(new MouseAdapter() {
    				public void mousePressed(MouseEvent evt) {
    					myJFrame.removeMouseListener(this);
    				}
    			});
    		}
    	}
    });
    Now it works beautifully. I have the above code for each of my two JLists that need right click functionality. When I right click anywhere else, nothing, but if I right click in one of the JLists, I get my menu. Then the code after the comment intercepts any other mouse input, and removes the popup. If, however, the new mouse input goes to another component, say a JButton, then the popup is removed but it doesn't stop the button from being clicked.

    Perfect. I'm infinitely impressed with myself. :p
    Now all I need to sort out is this: JMenu to JPopupMenu

Similar Threads

  1. code for number of clicks clicked on a web page???
    By Sveta_cool in forum Advanced Java
    Replies: 2
    Last Post: 02-01-2008, 03:55 PM
  2. JavaAgents class intercepting
    By zstars in forum Advanced Java
    Replies: 1
    Last Post: 07-16-2007, 11:27 PM
  3. Mouse over JButton
    By sandor in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2007, 10:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •