I have a drag-able JLabel that I'm trying to set that object to be above all other JLabel objects when its dragged. The only method I can find that sets the order is
setComponentZOrder(Component,int)'

But when I Try to use it, I get a large amount of errors printed to the console saying something about an unknown source. How do I fix these errors? I can do it from main, but how do I do it from a mouse listener in another class?

a JLayeredPane named window is defined in Main and a mouse listener in a card class is where I'm looking to set the order.

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class Card extends JLabel implements MouseMotionListener 
	{

	
	private static final long serialVersionUID = 1L;
	
	public Card(ImageIcon icon)
		{
		super(icon);
		super.setFocusable(true);
		addMouseMotionListener(this);
		}


	//@Override
	public void mouseDragged(MouseEvent e) 
		{
		// TODO Auto-generated method stub
		setBounds((super.getX()+  e.getX()-25) , (super.getY() + e.getY()-25), super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
		this.setRequestFocusEnabled(true);
		
		setComponentZOrder(this, TOP);      //this causes the errors
		
		repaint();	
		}


	//@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
		}
	}//*/

I'm sure the stack trace says more than "something about an unknown source". It contains the exact error, the method that generated it, and the line number. It would help if you posted that here as well - at least the first couple of lines.

at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
at java.awt.Container.checkAdding(Unknown Source)
	at java.awt.Container.setComponentZOrder(Unknown Source)
	at Card.mouseDragged(Card.java:28)
	at java.awt.Component.processMouseMotionEvent(Unknown Source)
	at javax.swing.JComponent.processMouseMotionEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
.
.
.

It goes on and on and on like that, the only part that gives a line, refers back to the setComponentZOrder

Yeah, stack traces from the event queue can be quite long and messy like that. Somewhere in all of that will be a line that tells the actual exception.

Anyway, one thing that I did notice is that you're calling setComponentZOrder() on the Card class, when it is the container holding the Card that you need to make the call on. Also, I'm not sure what the value of "TOP" is, but you want that index to be zero if you want that component on top of everything else in the container.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.