Placing component on Glass Pane

Posted by Chris Lieb on Stack Overflow See other posts from Stack Overflow or by Chris Lieb
Published on 2010-04-01T15:48:24Z Indexed on 2010/04/01 18:33 UTC
Read the original article Hit count: 439

Filed under:
|
|

I have a subclass of JLabel that forms a component of my GUI. I have implemented the ability to drag and drop the component from one container to another, but without any visual effects. I want to have this JLabel follow the cursor during the drag of the item from one container to another. I figured that I could just create a glass pane and draw it on there. However, even after I add the component to the glass pane, set the component visible, and set the glass pane visible, and set the glass pane as opaque, I still so not see the component. I know the component works because I can add it to the content pane and have it show up.

How do I add a component to the glass pane?

package wpics509s10t7.view;

import javax.swing.*; 

import wpics509s10t7.model.Tile;

import java.awt.*; 
import java.awt.dnd.DragSource;
import java.awt.event.AWTEventListener; 
import java.awt.event.MouseEvent; 

/** 
 * GlassPane tutorial 
 * "A well-behaved GlassPane" 
 * http://weblogs.java.net/blog/alexfromsun/ 
 * <p/> 
 * This is the final version of the GlassPane 
 * it is transparent for MouseEvents, 
 * and respects underneath component's cursors by default, 
 * it is also friedly for other users, 
 * if someone adds a mouseListener to this GlassPane 
 * or set a new cursor it will respect them 
 * 
 * @author Alexander Potochkin 
 */ 
public class GlassPane extends JPanel implements AWTEventListener { 
    private static final long serialVersionUID = 1L;

    private final JFrame frame; 
    private TileView tv; // subclass of JLabel
    private Point point;
    private WordStealApp wsa;

    public GlassPane(JFrame frame, WordStealApp wsa) { 
        super(null); 
        this.wsa = wsa;
        this.frame = frame; 
        setOpaque(true);
        setLayout(null);
        setVisible(true);
        composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
    }

    public void beginDrag(Tile t, Point p) {
        this.tv = new TileView(t, null, this.wsa, true);
        this.add(this.tv);
        System.out.println("Starting point: x=" + p.getX() + ",y=" + p.getY());
        this.tv.setLocation((int)p.getX(), (int)p.getY());
        this.tv.setVisible(true);
    }

    public void endDrag(Point p) {
        System.out.println("Ending point: x=" + p.getX() + ",y=" + p.getY());
        this.remove(this.tv);
        this.tv.setVisible(false);
        this.tv = null;
    }

    public void eventDispatched(AWTEvent event) { 
        if (event instanceof MouseEvent) { 
            MouseEvent me = (MouseEvent) event; 
            if (!SwingUtilities.isDescendingFrom(me.getComponent(), frame)) { 
                return; 
            } 
            if (me.getID() == MouseEvent.MOUSE_EXITED && me.getComponent() == frame) { 
                if (tv != null) {
                    tv.setVisible(false);
                }
                point = null; 
            } else { 
                MouseEvent converted = SwingUtilities.convertMouseEvent(me.getComponent(), me, frame.getGlassPane()); 
                point = converted.getPoint(); 
            } 
            repaint(); 
        } 
    } 

    /** 
     * If someone adds a mouseListener to the GlassPane or set a new cursor 
     * we expect that he knows what he is doing 
     * and return the super.contains(x, y) 
     * otherwise we return false to respect the cursors 
     * for the underneath components 
     */ 
    @Override
    public boolean contains(int x, int y) { 
        if (getMouseListeners().length == 0 && getMouseMotionListeners().length == 0 
                && getMouseWheelListeners().length == 0 
                && getCursor() == Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)) { 
            return false; 
        } 
        return super.contains(x, y); 
    }
} 

© Stack Overflow or respective owner

Related posts about java

Related posts about swing