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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:35:07+00:00 2026-06-14T12:35:07+00:00

I have a code like that: // In MyPanel.java public void paintComponent(Graphics g) {

  • 0

I have a code like that:

// In MyPanel.java
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    // Draw something
    mypanel_count++;
}

// In Test.java
public void testLargeData()
{
    while (notDone)
    {
        panel.repaint();
        // do huge work
        test_count++;
        System.out.println("Test_count: " + test_count + ", MyPanel_count: " + mypanel_count);
    }
}

// Output !!!
Test_count: 752, MyPanel_count: 23
Test_count: 753, MyPanel_count: 23
Test_count: 754, MyPanel_count: 23
Test_count: 755, MyPanel_count: 24

But when I change panel.repaint() to panel.paintComponent(panel.getGraphics()), the out is right:

Test_count: 752, MyPanel_count: 752
Test_count: 753, MyPanel_count: 753
Test_count: 754, MyPanel_count: 754
Test_count: 755, MyPanel_count: 755

Why? paintComponent method works, but sometimes it’s blind, so I don’t want to use it. Anybody can give me some suggestions? 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-14T12:35:09+00:00Added an answer on June 14, 2026 at 12:35 pm

    If you read the documentation of repaint carefully, you will notice that it states that (emphasis mine):

    If this component is a lightweight component, this method causes a
    call to this component’s paint method as soon as possible. Otherwise,
    this method causes a call to this component’s update method as soon as
    possible
    .

    This means that AWT/Swing is allowed to optimize repainting by merging repaints that are requested in a rapid succession. There is also a repaint(long time) method, which allows you to control how long AWT/Swing is allowed to wait with fullfilling your repaint request. It might still merge requests though, especially if you do them in a loop.

    It might be helpful to read the article “Painting in AWT and Swing”, which tries to explain the various concepts involved.

    To get the panel repainted for every iteration, you would have to wait for a paint to happen and then proceed with your loop. This means you need some synchronization between your processing thread (the loop) and the AWT/Swing thread. As a rough idea, you could for example wait() on the panel object at the end of your loop if it has not been repainted since the last call to repaint() and call notifyAll() at the end of your panel’s paintComponent() method. However, this can be tricky to implement right, so you should only do this if you really need “real-time” redrawing of your component. As an alternative, paintImmediately(...) could be used, but you would have to do all your processing in the event dispatching thread, like this:

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            while(notDone) {
                // Do your processing
                panel.paintImmediately(...);
            }
        }
    });
    

    Note that this will stop any event processing including mouse and keyboard input from being processed while your loop is running. You can read more about Swing and Threading in “Concurrency in Swing”

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

Sidebar

Related Questions

I have code that looks like this: template<class T> class list { public: class
i have got a problem, lets say i have got code like that: public
I have Java code like that: //first moment of the today Calendar cal =
We have some code that looks like this: class Serializer { public: template<class Type>
I frequently have code that looks something like this: if (itm != null) {
I have code like that 010200345 i want split every first three digit please
I have code like that: var xPola = 10, //how many cols yPola =
I have a code like that: User = function(){} User.a = function(){ return try;
I have Repeater Control and in that i have code like this <div runat=server
I have code that looks like this: obj.foo(); // obj might, or might not

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.