java - JFrame Glasspane is also over JDialog but shouldn't -
i have jframe (undecorated) glasspane. frame opens jdialog (also undecorated , has glasspane) , hides (setvisible(false)). glasspanes set .setglasspane(). dialog opened frame owner.
the glasspane extends jpanel , implements awteventlistener. use resizing frames , dialogs, knows it's parent (the frame/dialog) - called "target".
the events inside glasspane handled this:
public void eventdispatched(awtevent event) { if (target instanceof jframe) { e = swingutilities.convertmouseevent( ((mouseevent) event).getcomponent(), (mouseevent) event, ((jframe) target).getglasspane()); } else if (target instanceof jdialog) { e = swingutilities.convertmouseevent( ((mouseevent) event).getcomponent(), (mouseevent) event, this); } if (e.getid() == mouseevent.mouse_pressed) { this.startpos = target.getlocationonscreen(); } }
at "target.getlocationonscree" illegalcomponentstateexception, when jframe hidden , click on jdialog. says "component must showing on screen determine location". because glasspane of jframe gets event. glasspane of jdialog should it. think, glasspane of jframe in front of jdialog. why?
thanks helping!
edit:
here example:
import java.awt.awtevent; import java.awt.frame; import java.awt.toolkit; import java.awt.event.awteventlistener; import javax.swing.jdialog; import javax.swing.jframe; public class main { static jframe frame; static jdialog dialog; public static void main(string[] args) { frame = new jframe(); frame.setsize(600,600); glasspane frameglas = new glasspane(frame); frame.setglasspane(frameglas); frame.setvisible(true); frameglas.setvisible(true); dialog = new jdialog(frame); dialog.setsize(100, 100); glasspane dialogglas = new glasspane(dialog); dialog.setglasspane(dialogglas); awteventlistener al = (awteventlistener) frameglas; toolkit.getdefaulttoolkit().addawteventlistener( al, awtevent.mouse_motion_event_mask | awtevent.mouse_event_mask); dialogglas.setvisible(true); dialog.setvisible(true); } } import java.awt.awtevent; import java.awt.point; import java.awt.window; import java.awt.event.awteventlistener; import java.awt.event.mouseevent; import javax.swing.jdialog; import javax.swing.jpanel; import javax.swing.swingutilities; public class glasspane extends jpanel implements awteventlistener { /** * */ private static final long serialversionuid = 5110857185182004819l; private final window target; public glasspane(window target) { super(null); this.target = target; } public void eventdispatched(awtevent event) { if (event instanceof mouseevent) { mouseevent originalevent = (mouseevent) event; mouseevent e = originalevent; if (target instanceof jdialog) { e = swingutilities.convertmouseevent( ((mouseevent) event).getcomponent(), (mouseevent) event, this); } if (e.getid() == mouseevent.mouse_pressed) { point p = target.getlocationonscreen(); system.out.println(p.getx()); } } } }
looking @ source code, ever register frame
's glass pane awtlistener. now, on surface, doesn't seem bad thing. awtlistener
notified of mouse events in system, instance of glasspane
receiving events know frame
...
basically, means dialogglas
never receive events, it's not registered.
first, need register both frameglas
, dialogglas
listeners.
second, shouldn't trying "guess" target. mouseevent
(in fact events) have source. should comparing source target
can react events when occur on components you're interested in...
import java.awt.awtevent; import java.awt.component; import java.awt.eventqueue; import java.awt.point; import java.awt.toolkit; import java.awt.window; import java.awt.event.awteventlistener; import java.awt.event.mouseevent; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class main { static jframe frame; static jdialog dialog; public static void main(string[] args) { new main(); } public main() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } frame = new jframe(); frame.setsize(600, 600); glasspane frameglas = new glasspane(frame); frame.setglasspane(frameglas); frame.setvisible(true); frameglas.setvisible(true); dialog = new jdialog(frame); dialog.setsize(100, 100); glasspane dialogglas = new glasspane(dialog); dialog.setglasspane(dialogglas); dialogglas.setvisible(true); dialog.setvisible(true); // register listener frameglas toolkit.getdefaulttoolkit().addawteventlistener( frameglas, awtevent.mouse_motion_event_mask | awtevent.mouse_event_mask); // register listener dialogglas toolkit.getdefaulttoolkit().addawteventlistener( dialogglas, awtevent.mouse_motion_event_mask | awtevent.mouse_event_mask); } }); } public class glasspane extends jpanel implements awteventlistener { private static final long serialversionuid = 5110857185182004819l; private final window target; public glasspane(window target) { super(null); this.target = target; } @override public void eventdispatched(awtevent event) { if (event instanceof mouseevent) { mouseevent originalevent = (mouseevent) event; mouseevent e = originalevent; component source = e.getcomponent(); system.out.println("source: " + source); system.out.println("target: " + target); if (target != null && target.equals(source)) { e = swingutilities.convertmouseevent( ((mouseevent) event).getcomponent(), (mouseevent) event, this); if (e.getid() == mouseevent.mouse_pressed) { point p = target.getlocationonscreen(); system.out.println(p.getx()); } } } } } }
now, off top of head, problem you're having mouselistener
greedy, prevent events cascading beyond component registered on.
Comments
Post a Comment