Hello everyone, I have an issue I posted in "What's wrong with my code" section.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello everyone, I have an issue I posted in "What's wrong with my code" section.
I working on an assignment. My Swing application should display errors that may occur as a pop-up (not a separate new window, by a smooth pop-up). I wanted to use JLayer eventDispatched() method for that.
I have created my
I have picked MOUSE_EVENT_MASK to track if eventDispatched() handles any events at all (in fact, it does seem to be able to Listen to mouse events).package event; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ErrorEvent extends ActionEvent{ public static final long ERROR_MASK = MouseEvent.MOUSE_EVENT_MASK; public static final int ID = 2006; public ErrorEvent(Object source) { super(source, ID, "errorCommand"); //I HAVE TRIED LIKE THIS: ((JComponent) source).dispatchEvent(this); } public void fire() { //AND LIKE THIS: Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(this); //THIS LINE SHOWS THAT EVENT IS ACTUALLY BEING POSTED: System.out.println(Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent(2006)); this.consume(); } }
Here is code for JLayer:
JLayer<JComponent> layer = new JLayer<JComponent>(loginPanel, new LayerUI<JComponent>() { @Override public void installUI(JComponent c) { super.installUI(c); JLayer<JComponent> layer = (JLayer<JComponent>)c; layer.setLayerEventMask(ErrorEvent.ERROR_MASK); } @Override public void uninstallUI(JComponent c) { super.uninstallUI(c); JLayer<JComponent> layer = (JLayer<JComponent>)c; layer.setLayerEventMask(0); } @Override public void eventDispatched(AWTEvent e, JLayer<? extends JComponent> l) { System.out.println(e); } }
For some reason no matter how I try to dispatch my event (any event for that matter), they are not being handled by JLayer:
layer.dispatchEvent(new ErrorEvent(layer)); ErrorEvent errorEvent = new ErrorEvent(loginPanel); errorEvent.fire();
Please help, I have been trying to solve this issue for 5 hours now and nothing seems to help
Could you post a small, complete program that can be compiled and executed for testing?
If you don't understand my answer, don't ignore it, ask a question.
Thanks javaenjoyer, the information you shared is very useful Among Us