Results 1 to 11 of 11
  1. #1
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Moving a JLabel around, errors at run

    Hello,
    I am trying to move a JLabel in a JPanel, but can't seem to make it work.
    I can't provide a compiling code, but a few fragments hoping you could point me in the right direction.

    I have a JLabel [] array. i want to move with the mouse elements of this array.
    Let's say i want to move JLabel[i]. I implemented this:
    Java Code:
    static int labelY;
    static Point labelX;
    static Boolean flag = false;
                                                                     jLabel[i].addMouseListener(new MouseAdapter(){
    									public void mousePressed(MouseEvent evt){
    										labelY=evt.getY();
    										
    										
    									}
    								});
    								jLabel[i].addMouseMotionListener(new MouseAdapter(){
    									public void mouseDragged(MouseEvent evt){
    										Component parent = evt.getComponent().getParent();
    										Point mouse = parent.getMousePosition();
    										if(mouse.y!=labelY){
    											flag = true;
    											labelX.x=(int) mouse.getX();
    											labelX.y=(int) mouse.getY();
    										}
    									}
    								});
    								if(flag==true)
    									jLabel[i].setBounds(labelX.x, labelX.y, 75, 75);
    I used the bool flag because i can't make changes to the label when it is inside an anonymous class.
    I also tried using setLocation but that did not work also.
    I don't get any errors when compiling

    BUT when i run it and try to drag the JLabel[i] i get a long list of errors:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at puzzle.hw$1$2.mouseDragged(hw.java:276)
    	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.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Moving a JLabel around, errors at run

    What value is null on line 276 of your hw class? (Side note, you should use standard naming conventions, classes start with an uppercase letter.)

    The best way to figure out what's going on is to step through your program with a debugger, or at least add some print statements, or at least step through it with a piece of paper and a pencil. If you can't do that, you'll have to create an SSCCE that demonstrates the problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Moving a JLabel around, errors at run

    line 276 is flag = true;
    I find it hard to believe that, that is the problem.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Moving a JLabel around, errors at run

    Quote Originally Posted by tzeus View Post
    line 276 is flag = true;
    I find it hard to believe that, that is the problem.
    Yes, somehow I doubt it. Time to track down what's actually going on using the advice I gave you, or time to post an SSCCE so we can take a look.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Moving a JLabel around, errors at run

    In a way i understand where i went wrong, but don't know how to fix it.
    Here it is.
    I created the mouse events as functions, but i added the code to move the jLabel[i] outside those functions. So there is no reason why the JLabels would move when i click on them. However i cannot use the following line
    Java Code:
     jLabel[i].setBounds(labelX.x, labelX.y, 75, 75);
    inside the mouseDragged function because then i would get an error saying that
    Java Code:
    Cannot refer to a non-final variable i inside an inner class defined in a different method
    Any idea how i can avoid that?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Moving a JLabel around, errors at run

    You're using anonymous inner classes, which come with some strange rules. I would advice against using them until you really understand what's going on. Instead, use a regular named class (in a separate file) and pass in whatever information the new class needs.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Moving a JLabel around, errors at run

    I will try, thanks.

  8. #8
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Moving a JLabel around, errors at run

    What could be happening is that you are tracking the mouse position, and if you go out of the frame, it will through a null pointer exception. Try wrapping a try catch around the mouse.getX() and mouse.getY()

  9. #9
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Moving a JLabel around, errors at run

    I fixed it, since then. I believe that was a part of the problem. Thanks for the answers !

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: Moving a JLabel around, errors at run

    Perhaps I am missing something here. But you add a new mouse listener to each JLabel. All you need is one mouse listener instance. Add the same instance to each JLabel. Then when your listener catches the event, just get the source of the event, cast it to JLabel and set the bounds right there. Now I have not tried this particular solution on your example but I use the technique often. And there may be other issues to deal with. But using the source of events to effect change on the source itself is a common practice.

    And annonymous classes are fine but you may want to check out inner classes as they are cleaner and usually clearer.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Moving a JLabel around, errors at run

    To be honest, i know almost nothing about inner and anonymous classes or even java, i am only learning. The puzzle is a personal project / school project. Though i am learning about them as i use them. I know i am using an anonymous class.
    I had a problem with that anonymous class, since i could not use the jLabel[] inside it and that was a huge problem. I managed to solve that. Nevertheless you are proposing a very interesting solution. When i get back to optimizing the code, i will sure use it.
    Thank you guys again for the answers !

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  2. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  3. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 12:29 PM
  4. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •