Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Mouse Dragged Event

Mouse Dragged Event

Scheduled Pinned Locked Moved Java
help
6 Posts 2 Posters 20 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MallardsReach
    wrote on last edited by
    #1

    I've looked at various articles and examples none of which are working for me. I have a jPanel inside of which is a jlabel. In the MouseDragged event I have the following code.

    int x=evt.getX();
    int y=evt.getY();
    jLabel3.setLocation(x, y);
    jLabel3.repaint();
    

    The Label moves but it flickers and sometimes show a copy of it self. Any help in the right direction would be appreciated.

    L 1 Reply Last reply
    0
    • M MallardsReach

      I've looked at various articles and examples none of which are working for me. I have a jPanel inside of which is a jlabel. In the MouseDragged event I have the following code.

      int x=evt.getX();
      int y=evt.getY();
      jLabel3.setLocation(x, y);
      jLabel3.repaint();
      

      The Label moves but it flickers and sometimes show a copy of it self. Any help in the right direction would be appreciated.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The problem is that dragging produces many events so your code will be repeatedly moving the label a couple of points, and repainting it. You need to limit the number of repaints somehow. It's a long time since I wrote any GUI code in Java, so I am not sure exactly how this is best achieved. I expect Google will find some samples that help, or even some of the example code found in The Java™ Tutorials[^].

      M 1 Reply Last reply
      0
      • L Lost User

        The problem is that dragging produces many events so your code will be repeatedly moving the label a couple of points, and repainting it. You need to limit the number of repaints somehow. It's a long time since I wrote any GUI code in Java, so I am not sure exactly how this is best achieved. I expect Google will find some samples that help, or even some of the example code found in The Java™ Tutorials[^].

        M Offline
        M Offline
        MallardsReach
        wrote on last edited by
        #3

        Thanks Richard, It seems like Java is more of a behind the scenes type of language and not really GUI orientated despite the best efforts of netbeans and the like. I guess I'll revert to VB as that does it everything you need for a windows environment even though it is platform dependent.

        L 2 Replies Last reply
        0
        • M MallardsReach

          Thanks Richard, It seems like Java is more of a behind the scenes type of language and not really GUI orientated despite the best efforts of netbeans and the like. I guess I'll revert to VB as that does it everything you need for a windows environment even though it is platform dependent.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Not true. You would face exactly the same issue in VB, C# C++ etc.

          1 Reply Last reply
          0
          • M MallardsReach

            Thanks Richard, It seems like Java is more of a behind the scenes type of language and not really GUI orientated despite the best efforts of netbeans and the like. I guess I'll revert to VB as that does it everything you need for a windows environment even though it is platform dependent.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            See How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)[^]

            M 1 Reply Last reply
            0
            • L Lost User

              See How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)[^]

              M Offline
              M Offline
              MallardsReach
              wrote on last edited by
              #6

              Thanks Richard I seem to remember it being easier in vb but could be looking through rose coloured specs. I looked at the links you suggested but to no avail mind they are 12yrs old now so maybe not surprising. I will try to use another idea instead. I was trying to get a question with three possible answers and the user had to drag the correct answer in place, so maybe I'll look at seeing if a jTextField can be dragged around. If that fails three radio buttons and they can click the one they think is the right answer. I will stick with Java

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups