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

  • Home
  • SEARCH
  • 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 8090459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:43:48+00:00 2026-06-05T19:43:48+00:00

I am having trouble moving this JLabel across this JPanel? I put the code

  • 0

I am having trouble moving this JLabel across this JPanel? I put the code below. Basically what is supposed to happen, is the JLabel called “guy” slowly moves to the right. The only problem is, that the JLabel isn’t refreshing it just disappears after the first time I move it.

public class Window extends JFrame{

    JPanel panel = new JPanel();
    JLabel guy = new JLabel(new ImageIcon("guy.gif"));
    int counterVariable = 1;

    //Just the constructor that is called once to set up a frame.
    Window(){
        super("ThisIsAWindow");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(panel);
        panel.setLayout(null);
    }

    //This method is called once and has a while loop to  exectue what is inside.
    //This is also where "counterVariable" starts at zero, then gradually
    //goes up. The variable that goes up is suposed to move the JLabel "guy"...
    public void drawWorld(){
        while(true){
            guy.setBounds(counterVariable,0,50,50);
            panel.add(guy);
            counterVarialbe++;
            setVisible(true);
            try{Thread.sleep(100)}catch(Exception e){}
        }

    }

Any thoughts as to why the JLabel is just disappearing instead of moving to the right after I change the variable “counterVariable”.
-Thanks! 🙂

  • 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-05T19:43:51+00:00Added an answer on June 5, 2026 at 7:43 pm

    Your code is causing a long-running process to run on the Swing event thread which is preventing this thread from doing its necessary actions: paint the GUI and respond to user input. This will effectively put your entire GUI to sleep.

    Issue & suggestions:

    • Never call Thread.sleep(...) on the Swing Event Dispatch Thread or EDT.
    • Never have a while (true) on the EDT.
    • Instead use a Swing Timer for all of this.
    • No need to keep adding the JLabel to the JPanel. Once added to the JPanel, it remains there.
    • Likewise, no need to keep calling setVisible(true) on the JLabel. Once visible, it remains visible.
    • Call repaint() on the container holding the moving JLabel after you’ve moved it to request that the container and its children be re-drawn.

    e.g.,

    public void drawWorld(){
      guy.setBounds(counterVariable,0,50,50);
      int timerDelay = 100;
      new javax.swing.Timer(timerDelay, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          countVariable++;
          guy.setBounds(counterVariable,0,50,50);
          panel.repaint();
        }
      }).start;
    }
    

    caveat: code not compiled, run, or tested in any way

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just learning Clojure and am having trouble moving my code into different
I am having trouble moving from parser grammar to tree grammar, the problem comes
I'm having trouble moving a website from one server to another. It seems to
I'm using Oracle 11 and I'm having trouble moving tables between two databases. I've
I've been having some trouble moving a C project compiled and running in unix
I am moving from Apache 1.3 to Apache 2.063 and am having trouble getting
I'm having trouble moving from MySQL SELECT methods to PDO methods. I want to
Having trouble with an sql statement to retrieve a record between two timestamps. This
I'm having trouble optimizing this query: SELECT a.id FROM a JOIN b ON a.id=b.id
I'm having trouble controlling the position of div blocks. Here is the html code.

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.