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.

Results 1 to 4 of 4

Thread: Swing JLayer doesn't handle my AWTEvent

  1. #1
    Junior Member
    Join Date
    May 2024
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi

    Hello everyone, I have an issue I posted in "What's wrong with my code" section.

  2. #2
    Junior Member
    Join Date
    May 2024
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Swing JLayer doesn't handle my AWTEvent

    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
    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();
    	 }
    }
    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).

    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

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Swing JLayer doesn't handle my AWTEvent

    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.

  4. #4
    Junior Member
    Join Date
    Oct 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing JLayer doesn't handle my AWTEvent

    Thanks javaenjoyer, the information you shared is very useful Among Us

Similar Threads

  1. How to handle this particular Exception
    By jordileft in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 20th, 2014, 04:37 AM
  2. Jlayer, playing mp3!
    By sakonpure6 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 22nd, 2013, 02:52 PM
  3. how to handle big numbers
    By veera in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 13th, 2013, 10:49 PM
  4. Try : handle an exception by yourself.
    By try in forum Member Introductions
    Replies: 1
    Last Post: July 12th, 2011, 10:37 AM
  5. how to handle many more exceptions
    By shailaja in forum Member Introductions
    Replies: 1
    Last Post: December 27th, 2010, 01:02 AM

Tags for this Thread