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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:09:38+00:00 2026-06-18T06:09:38+00:00

I am trying to make a Java application which on Clicking a button, displays

  • 0

I am trying to make a Java application which on Clicking a button, displays random colors in the Panel for a particular time duration.

But my problem is that after clicking the button the color of the frame changes only once and also the the title of the Button doesn’t changes to “U Clicked me”.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class MyDrawPanel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        // g.fillRect(0, 0, this.getWidth(), this.getHeight())
        int red = (int) (Math.random() * 255);
        int green = (int) (Math.random() * 255);
        int blue = (int) (Math.random() * 255);
        Color randomizecolor = new Color(red, green, blue);
        g.setColor(randomizecolor);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
}

public class CustomWidget implements ActionListener {

    JButton button;
    JFrame frame;

    public void Gui() {
        frame = new JFrame();
        MyDrawPanel pan = new MyDrawPanel();
        button = new JButton("-- Click Me to Change Me --");
        frame.add(BorderLayout.SOUTH, button);
        frame.add(BorderLayout.CENTER, pan);
        button.addActionListener(this);
        frame.setSize(500, 500);
        frame.setTitle("Random Color GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void asd() {
        button.setText("U clicked Me");
        for (int i = 0; i < 150; i++) {
            frame.repaint();
            try {
                Thread.sleep(10);
            } catch (Exception x) {
            }
        }
        button.setText("Again Click me");
    }

    public static void main(String[] args) {
        CustomWidget obj = new CustomWidget();
        obj.Gui();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.asd();
        // this.button.setText("-- Click Me to Change Me --");
    }
}
  • 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-18T06:09:39+00:00Added an answer on June 18, 2026 at 6:09 am

    Don’t call Thread.sleep(...) on the Swing event thread as all this does is put the entire GUI to sleep including its ability to paint. Instead use a Swing Timer. Check the link for the tutorial. Incidentally, 10 ms is awfully short and may be too short for time slices or for folks to notice. Also, I’d randomize and create the new Color in the Swing Timer’s ActionListener and not in the paintComponent(...) method itself.

    Edit:
    Note that Swing uses a single thread, the Event Dispatch Thread or EDT, to update all graphics and to do all user interactions. If you put this thread to sleep by calling Thread.sleep(...) or by calling a long-running bit of code on this thread, then the entire Swing application will go to sleep, and no user interactions or Swing drawing will occur until the sleep ends. The key to a solution is to do all long-running tasks on a background thread. The Swing Timer will do this for you, and the tutorial will show you how.

    Edit 2:
    in semi-pseudocode:

      button.setText(BTN_CLICKED_TEXT);
      // TIMER_DELAY is some constant int
      Timer myTimer = new Timer(TIMER_DELAY, new ActionListener() {
         private int count = 0;
    
         @Override
         public void actionPerformed(ActionEvent timerActionEvt) {
            if the count variable is >= some maximum count
              // stop the timer by calling stop on it
              // I'll show you this one since it is a bit complex
              ((Timer)timerActionEvt.getSource()).stop();
              // set the button text to its original state
              // return from this method
    
            else 
              // randomize pan's color and repaint it
              count++; // increment the counter variable
         }
      });
    
      myTimer.start();
    

    Note that the pan variable must be declared in the Gui class, not in its constructor for the Timer to be able to get to it.

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

Sidebar

Related Questions

I am trying to make a application in java which connects to a mysql
I am trying to make an Java application, which needs a connection with a
I'm trying to make Microsoft Translator API in my Java application. I registered in
I am trying to make a Java implementation of the Park-Miller-Carta PRNG random number
I'm trying to make sure my Java application takes reasonable steps to be robust,
I am trying to make an android application, which uses some jni code. This
I'm trying to make application where user has an option to click on button
I am trying to make a Java web application where users can log into
I've been trying to make an application which has very rich Database mySQL with
I am trying to make android application which should take ms word files i.e.(

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.