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 9016775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:05:31+00:00 2026-06-16T04:05:31+00:00

I have two jlabels with an image.. I try to change the image in

  • 0

I have two jlabels with an image.. I try to change the image in a label when the other is clicked.. (such as a game)..
I have a problem..When i write the Thread.sleep the image is not change..See the code:

public class Game extends JFrame{
private JLabel l1,l2;;
private boolean isClicked = false ,isClicked2 = false;
public Game(){
    setLayout(new FlowLayout());
    l1 = new JLabel(new ImageIcon(getClass().getResource("image1.png")));
    add(l1);
    l2 = new JLabel(new ImageIcon(getClass().getResource("image1.png")));
    add(l2);


    l1.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {}
        @Override
        public void mousePressed(MouseEvent e) {}
        @Override
        public void mouseReleased(MouseEvent e) {
            if(isClicked2){
                l1.setIcon(new ImageIcon(getClass().getResource("image2.png")));
                try {
                    Thread.sleep(1000);
                    l1.setIcon(new ImageIcon(getClass().getResource("image1.png")));
                    l2.setIcon(new ImageIcon(getClass().getResource("image1.png")));
                    isClicked2 = false;
                    isClicked = false;
            }catch(InterruptedException ex){}
            }
            else{
                l1.setIcon(new ImageIcon(getClass().getResource("image2.png")));
                isClicked = true;
            }     
        }@Override public void mouseEntered(MouseEvent e){}@Override public void mouseExited(MouseEvent e){}
    });

    l2.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {}
        @Override
        public void mousePressed(MouseEvent e) {}
        @Override
        public void mouseReleased(MouseEvent e) {
            if(isClicked){
              try {
                    l2.setIcon(new ImageIcon(getClass().getResource("image2.png")));
                    Thread.sleep(1000);
                    l2.setIcon(new ImageIcon(getClass().getResource("image1.png")));
                    l1.setIcon(new ImageIcon(getClass().getResource("image1.png")));
                isClicked = false;
                    isClicked2 = false;
              }catch(InterruptedException ex){}  
            }
            else{
                l2.setIcon(new ImageIcon(getClass().getResource("image2.png")));
                isClicked2 = true;
            }     
        }@Override public void mouseEntered(MouseEvent e){}@Override public void mouseExited(MouseEvent e){}
    });
}

public static void main(String[] args) {
    Game g = new Game();
    g.setTitle("Fint the same");
    g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    g.pack();
    g.setVisible(true);
}

}

Run it and first click the fisrt label.. After click the second label and the first will change image in 1 second but the seconf label NEVER!!

  • 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-16T04:05:33+00:00Added an answer on June 16, 2026 at 4:05 am

    The changing of the images has to be done on the event thread (the thread used to process graphics events). Therefore setting the image will queue an appropriate event to be processed after your method returns. At that time though, the image has already changed back, because sleep()ing on the event thread doesn’t allow any other events to be processed.
    As dashrb said, don’t sleep() the main thread, but schedule the flip back in a Timer:

                    if (isClicked) {
    
                        l2.setIcon(new ImageIcon(getClass().getResource("image2.png")));
    
                        new javax.swing.Timer(1000, new ActionListener() {
                            @Override
                            public void actionPerformed(final ActionEvent e) {
                                l2.setIcon(new ImageIcon(getClass().getResource("image1.png")));
                                l1.setIcon(new ImageIcon(getClass().getResource("image1.png")));
                                isClicked = false;
                                isClicked2 = false;
                            }
                        }).start();
    
                    } catch (final Exception ex) {
                        ex.printStackTrace();
                    }
                } else {
                   ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame with null layout and two one character JLabels on the
I have a panel with two buttons. I'm trying to insert an image inside
I have a JPanel with BoxLayout.PAGE_AXIS, but when I add two JLabels to it,
--EDIT-- I have got a welcome window consisting of two JLabels. It has a
I have a strange problem... I have a JFrame and added two JInternalFrame I
Have two folders with approx. 150 java property files. In a shell script, how
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two tables images2 and image_data So the goal is to have 1
I have two classes (MVC view model) which inherits from one abstract base class.
I have two files client.php and server.php. The client file send a HTTP request

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.