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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:13:29+00:00 2026-06-01T14:13:29+00:00

so i’m trying to set up an application where i have multiple panels inside

  • 0

so i’m trying to set up an application where i have multiple panels inside a jframe. lets say 3 of them are purely for display purposes, and one of them is for control purposes. i’m using a borderLayout but i don’t think the layout should really affect things here.

my problem is this: i want the repainting of the three display panels to be under the control of buttons in the control panel, and i want them to all execute in sync whenever a button on the control panel is pressed. to do this, i set up this little method :

public void update(){
            while(ButtonIsOn){
                a.repaint();
                b.repaint()
                c.repaint();
                System.out.println("a,b, and c should have repainted");

                }
    }

where a,b, and c are all display panels and i want a,b,and c to all repaint continously until i press the button again. the problem is, when i execute the loop, the message prints in an infinite loop, but none of the panels do anything, ie, none of them repaint.

i’ve been reading up on the event dispatch thread and swing multithreading, but nothing i’ve found so far has really solved my problem. could someone give me the gist of what i’m doing wrong here, or even better, some sample code that handles the situation i’m describing? 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-01T14:13:30+00:00Added an answer on June 1, 2026 at 2:13 pm

    The java.util.concurrent package provides very powerful tools for concurrent programing.

    In the code below, I make use of a ReentrantLock (which works much like the Java synchronized keyword, ensuring mutually exclusive access by multiple threads to a single block of code). The other great thing which ReentrantLock provides are Conditions, which allow Threads to wait for a particular event before continuing.

    Here, RepaintManager simply loops, calling repaint() on the JPanel. However, when toggleRepaintMode() is called, it blocks, waiting on the modeChanged Condition until toggleRepaintMode() is called again.

    You should be able to run the following code right out of the box. Pressing the JButton toggle repainting of the JPanel (which you can see working by the System.out.println statements).

    In general, I’d highly recommend getting familiar with the capabilities that java.util.concurrent offers. There’s lots of very powerful stuff there. There’s a good tutorial at http://docs.oracle.com/javase/tutorial/essential/concurrency/

    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class RepaintTest {
        public static void main(String[] args) {
    
            JFrame frame = new JFrame();
            JPanel panel = new JPanel()
            {
                @Override
                public void paintComponent( Graphics g )
                {
                    super.paintComponent( g );
    
                    // print something when the JPanel repaints
                    // so that we know things are working
                    System.out.println( "repainting" );
                }
            };
    
            frame.add( panel );
    
            final JButton button = new JButton("Button");
            panel.add(button);
    
            // create and start an instance of our custom
            // RepaintThread, defined below
            final RepaintThread thread = new RepaintThread( Collections.singletonList( panel ) );
            thread.start();
    
            // add an ActionListener to the JButton
            // which turns on and off the RepaintThread
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    thread.toggleRepaintMode();
                }
            });
    
            frame.setSize( 300, 300 );
            frame.setVisible( true );
        }
    
        public static class RepaintThread extends Thread
        {
            ReentrantLock lock;
            Condition modeChanged;
            boolean repaintMode;
            Collection<? extends Component> list;
    
            public RepaintThread( Collection<? extends Component> list )
            {
                this.lock = new ReentrantLock( );
                this.modeChanged = this.lock.newCondition();
    
                this.repaintMode = false;
                this.list = list;
            }
    
            @Override
            public void run( )
            {
                while( true )
                {
                    lock.lock();
                    try
                    {
                        // if repaintMode is false, wait until
                        // Condition.signal( ) is called
                        while ( !repaintMode )
                            try { modeChanged.await(); } catch (InterruptedException e) { }
                    }
                    finally
                    {
                        lock.unlock();
                    }
    
                    // call repaint on all the Components
                    // we're not on the event dispatch thread, but
                    // repaint() is safe to call from any thread
                    for ( Component c : list ) c.repaint();
    
                    // wait a bit
                    try { Thread.sleep( 50 ); } catch (InterruptedException e) { }
                }
            }
    
            public void toggleRepaintMode( )
            {
                lock.lock();
                try
                {
                    // update the repaint mode and notify anyone
                    // awaiting on the Condition that repaintMode has changed
                    this.repaintMode = !this.repaintMode;
                    this.modeChanged.signalAll();
                }
                finally
                {
                    lock.unlock();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.