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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:13:40+00:00 2026-05-26T04:13:40+00:00

I have a JPanel on which I’ve painted four rectangles. The color for each

  • 0

I have a JPanel on which I’ve painted four rectangles. The color for each of these rectangles is selected at random. The colors should change only when the user clicks on a particular rectangle.

The problem is, while a user is resizing the window, everything on the JPanel is ‘repainted’ repeatedly. This causes the rectangles to rapidly change color.

Ideally, I would need the colors of the rectangles to stay the same during a resize. Otherwise, I could also manage with a solution where the JPanel is repainted only once after a resize has been completed.

Do you have any general ideas on how I could implement this? I feel like it would have been a lot easier if there was a onStartResize and onFinishResize callback method in ComponentListener.

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-05-26T04:13:41+00:00Added an answer on May 26, 2026 at 4:13 am

    This example may serve to illustrate the violation adduced by @kleopatra. As a component is resized, the event dispatch mechanism helpfully invokes repaint() for you. If you change the state of what you’re rendering, say in paintComponent(), you’ll see it cycle rapidly. In the example below, the bottom row flickers as you resize, while the top row remains unchanged.

    Addendum: AnimationTest is a related example that takes advantage of this effect to perform animation in a ComponentAdapter.

    ResizeMe

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /** @https://stackoverflow.com/questions/7735774 */
    public class ResizeMe extends JPanel {
    
        private static final int N = 4;
        private static final int SIZE = 100;
        private static final Random rnd = new Random();
        private final List<JLabel> list = new ArrayList<JLabel>();
        private boolean randomize;
    
        public ResizeMe(boolean randomize) {
            this.randomize = randomize;
            this.setLayout(new GridLayout(1, 0));
            for (int i = 0; i < N; i++) {
                JLabel label = new JLabel();
                label.setPreferredSize(new Dimension(SIZE, SIZE));
                label.setOpaque(true);
                list.add(label);
                this.add(label);
            }
            initColors();
            this.addComponentListener(new ComponentAdapter() {
    
                @Override
                public void componentResized(ComponentEvent e) {
                    System.out.println(e);
                }
            });
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (randomize) {
                initColors();
            }
        }
    
        private void initColors() {
            for (JLabel label : list) {
                label.setBackground(new Color(rnd.nextInt()));
            }
        }
    
        private static void display() {
            JFrame f = new JFrame("ResizeMe");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLayout(new GridLayout(0, 1));
            f.add(new ResizeMe(false), BorderLayout.NORTH);
            f.add(new ResizeMe(true), BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    display();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application where I have a JPanel with some rectangles painted on
Hey guys. I have got a JPanel which changes color when it is clicked
I have a JPanel which draws .png images. each image has 2 copies to
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JPanel to which I'd like to add JPEG and PNG images
I have a JPanel subclass on which I add buttons, labels, tables, etc. To
I have a JPanel, which I would like to detect the following events (1)
I have a Graphics object of JPanel and that is working fine: import java.awt.Color;
I have a JPanel which has a line, circle, etc. Now when I click
I have a JPanel panel which contains a JTree tree . Sometimes the JTree

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.