Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:29:17+00:00 2026-06-13T18:29:17+00:00

i try to move it to the right(x++) every seconds i try to move

  • 0

i try to move it to the right(x++) every seconds

i try to move it with thread..

  1. how to do it? (and can see it move every second)
  2. there are another way to do it without use thread?
  3. what layout manager that i should use?

heres i try..

public class help {
    JFrame frame = new JFrame();
    JLabel label = new JLabel("target");

    public help() {
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(800,600);
        frame.setLayout(new GridLayout());
        frame.add(label);
        label.setPreferredSize(new Dimension(100,100));
        label.setLocation(400, 300);

        frame.getContentPane().validate();
        frame.repaint();
        frame.setVisible(true);

        mysterious();
    }

    void mysterious(){
     ////////////////////////////////
     // part of edit responding David kroukamp  
    Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
         try{

            for (int z=0; z<10; z++){
            label.setLocation((label.getLocationOnScreen().x+10), label.getLocationOnScreen().y);
            Thread.sleep(1000);  
            } 
        }catch(Exception ae){

    }
    }
});
t.start();
    //////////////////////////////



    }
    public static void main(String[]args){
        new help();  
        }
}

thanks a lot for any kind of help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:29:18+00:00Added an answer on June 13, 2026 at 6:29 pm
    • Class names begin with capital letters i.e Help
    • Swing components should be created and modified on Event Dispatch Thread
    • A new Thread is created like this:

      Thread t = new Thread(new Runnable() {
          @Override
          public void run() {
              //work here
          }
      });
      t.start();//start thread
      

    however I’d suggest a Swing Timer as it runs on EDT:

    • How to Use Swing Timers

    EDIT:

    As per your questions I suggest using a Timer the creating thread point was for general knowledge.

    The probelm is the Thread is not run on EDT Thread of your swing GUI where as a Timer does:

     int delay = 1000; //milliseconds
      ActionListener taskPerformer = new ActionListener() {
          int count=0;
          public void actionPerformed(ActionEvent evt) {
               if(count==10) {//we did the task 10 times
                     ((Timer)evt.getSource()).stop();
                }
    
                label.setLocation((label.getLocationOnScreen().x+10), label.getLocationOnScreen().y);
                System.out.println(SwingUtilities.isEventDispatchThread());
               count++;
          }
      };
      new Timer(delay, taskPerformer).start();
    

    Reference:

    • http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello there I try to move down some pixels an image I just centered,
I've used the 'hg convert' command to try to move my SVN repository to
Every time I try to do something seemingly-simple in CSS, it doesn't work. I
I am try to move all my static content to a CDN to improve
I have any image which I would like to rotate and move from right
I am using Zend Framework and also try to move towards DDD approach (Domain-Driven
I see plenty of answers to this riddle on here, but every time I
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
try { if (myBoolean) { while (true) ; } else { System.exit(1); } }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.