Skip to main content

problems using a glasspane

4 replies [Last post]
Joined: 2006-01-27

This isn't really the most relevant forum for my question, but I have had little help with other forums, so here is goes.

I am having trouble redirecting mouseEvents to a combo box which is underneath a glasspane. I have provided some example code (I grabbed it from one of Joshua Marinacci's blogs, but changed it up a bit), which shows the problems.

The problems are:

1. When the combo box is expanded/opened, the mouse movement is tracked as the correct items are highlighted as the mouse moves over each item, however, when you click the item, it does not become selected, pretty much rendering the combo box useless (although key actions do work)

2. When the combo box is expanded/opened, the scrollbar on the right hand side immediately disappears when click on.

3. When the combo box is expanded/opened, it should be closed when you click on the combo box again, but it does not close until the mouse is clicked outside of the combo box area.

Another problem I found was that the components do not have the rollover effects with a glass pane, but I have since found a solution to that.

Here is the code. any help is greatly appreciated as I have begun pulling my hair out over this:

import javax.swing.*;<br />
import java.awt.event.MouseEvent;<br />
import java.awt.event.MouseMotionListener;<br />
import java.awt.event.MouseListener;<br />
import java.awt.*;</p>
<p>public class RightClick extends JComponent implements MouseListener, MouseMotionListener {</p>
<p>    JPopupMenu popup;<br />
    Container contentPane;<br />
	Point p = new Point(20,20);</p>
<p>    public RightClick(Container contentPane) {<br />
        addMouseListener(this);<br />
        addMouseMotionListener(this);<br />
        this.contentPane = contentPane;</p>
<p>        popup = new JPopupMenu();<br />
        popup.add(new JMenuItem("Dogs"));<br />
        popup.add(new JMenuItem("Cats"));<br />
        popup.add(new JMenuItem("Mass Hysteria"));<br />
<p>    public void paint(Graphics g) {<br />
        super.paintComponent(g);<br />
		g.setColor(;<br />
		g.fillOval(p.x, p.y, 20, 20);<br />
<p>    public void mouseMoved(MouseEvent e) {<br />
        redispatchMouseEvent(e);<br />
    }<br />
    public void mouseDragged(MouseEvent e) {<br />
        redispatchMouseEvent(e);<br />
    }<br />
    public void mouseClicked(MouseEvent e) {<br />
        print("mouse clicked");<br />
	    p = e.getPoint();<br />
	    this.repaint();<br />
        redispatchMouseEvent(e);<br />
    }<br />
    public void mouseEntered(MouseEvent e) {<br />
        redispatchMouseEvent(e);<br />
    }<br />
    public void mouseExited(MouseEvent e) {<br />
        redispatchMouseEvent(e);<br />
    }<br />
    public void mousePressed(MouseEvent e) {<br />
        redispatchMouseEvent(e);<br />
    }<br />
    public void mouseReleased(MouseEvent e) {<br />
        redispatchMouseEvent(e);<br />
<p>    private void redispatchMouseEvent(MouseEvent e) {</p>
<p>        if(e.isPopupTrigger()) {<br />
            print("it's a popup");<br />
  , e.getX(), e.getY());</p>
<p>        } else {<br />
            Point containerPoint = SwingUtilities.convertPoint(this,<br />
<p>            Component component = SwingUtilities.getDeepestComponentAt(<br />
                        contentPane,<br />
                        containerPoint.x,<br />
<p>            if (component == null) {<br />
                return;<br />
<p>            Point componentPoint = SwingUtilities.convertPoint(<br />
                this,<br />
                e.getPoint(),<br />
<p>            component.dispatchEvent(new MouseEvent(component,<br />
                e.getID(),<br />
                e.getWhen(),<br />
                e.getModifiers(),<br />
                componentPoint.x,<br />
                componentPoint.y,<br />
                e.getClickCount(),<br />
                e.isPopupTrigger()));<br />
        }<br />
<p>    public static void main(String[] args) {<br />
        JFrame frame = new JFrame("Right Click Test");<br />
        JButton button = new JButton("this is a button");<br />
        JPanel panel = new JPanel();<br />
	    JComboBox combo = new JComboBox( );<br />
	    for(int x=0; x < 100; x++) {<br />
		    combo.addItem("item " + x);<br />
<p>        panel.add(button);<br />
        panel.add(combo);<br />
<p>        RightClick rc = new RightClick(frame.getLayeredPane());<br />
        frame.setGlassPane(rc);<br />
<p>        frame.pack();<br />
        frame.setSize(600,500);<br />
        frame.setVisible(true);<br />
<p>    public static void print(String str) {<br />
        System.out.println(str);<br />
<p>}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-01-27

I tested it on version 04 & 05 and still encountered the same problem.

I also setLightWeightPopup(false) on the combobox, and that solved some of the problems, but it is not an acceptable solution. In this case, you can correctly select an item, but the mouseover highlight does not follow your mouse. Also, the up down buttons work, but the scrollbar does not move.

Joined: 2004-01-02

Hmm.. I don't have any of those problems when I run it. What version of the JVM are you using? What platform?

I'm on a Mac using 1.5.0_06. It worked with both the Mac L&F and Metal.

Message was edited by: reden

Joined: 2006-01-27

hmm...thats interesting. I am on windows XP SP2, using Java 1.5_0_08...

I will try it with some other java versions, but my guess is that it works for you since your on a Mac...I tested this on the default L&F (Metal I believe), as well as Windows L&F, but the problem is still there.

Joined: 2007-05-01

Did you ever solve this?
I have exactly the same problem.