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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:08:16+00:00 2026-05-27T06:08:16+00:00

I’ve made a custom subclass of JLabel . I have a single instance of

  • 0

I’ve made a custom subclass of JLabel. I have a single instance of this, inside a single JPanel, inside a single JFrame. I do not override the paintComponent() method; all the class does is change the background color when the cursor hovers over it.

The JFrame loads immediately, but for several seconds the JPanel is left undrawn. I verified that this is because of my custom class by overriding paintComponent() and adding some debug println() statements.

public void paintComponent(Graphics context)
{
    System.out.println("Painting...");
    super.paintComponent(context);
    System.out.println("Painted.");
}

The strange thing is, it’s drawn instantly when I use Panel instead of JPanel or Label instead of JLabel.

Where is this lag coming from?

EDIT: Some example code. Nothing is actually drawn; look at the console message delay.

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.GridLayout;
import javax.swing.JPanel;

public class Example extends JLabel implements MouseListener
{
    private static final long serialVersionUID = 0;

    public Example()
    {
        super();
        System.out.println("Constructed.");
    }

    public void paintComponent(java.awt.Graphics g)
    {
        System.out.println("Painting component...");
        super.paintComponent(g);
        System.out.println("Painted.");
    }

    public void mouseEntered(MouseEvent event) { }
    public void mouseExited(MouseEvent event) { }
    public void mouseReleased(MouseEvent event) { }
    public void mousePressed(MouseEvent event) { }
    public void mouseClicked(MouseEvent event) { }

    public static void main(final String[] arguments)
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 1));
        panel.add(new Example());

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(panel);
        frame.setVisible(true);
        System.out.println("Set visible.");
    }
}
  • 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-27T06:08:16+00:00Added an answer on May 27, 2026 at 6:08 am

    My code doesn’t lag:

    My SSCCE:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class LabelTest extends JPanel {
       public LabelTest() {
          add(new MyLabel("Fubar!"));
       }
    
       private static void createAndShowGui() {
          LabelTest mainPanel = new LabelTest();
    
          JFrame frame = new JFrame("LabelTest");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class MyLabel extends JLabel {
       private static final Color BACKGROUND_DEFAULT = new Color(200, 200, 255);
       private static final Color BACKGROUND_MOUSEOVER = new Color(255, 200, 200);
       private static final int PREF_W = 200;
       private static final int PREF_H = 100;
    
       public MyLabel(String text) {
          super(text, SwingConstants.CENTER);
          setOpaque(true);
          setBackground(BACKGROUND_DEFAULT);
          addMouseListener(new MouseAdapter() {
    
             @Override
             public void mouseEntered(MouseEvent arg0) {
                setBackground(BACKGROUND_MOUSEOVER);
             }
    
             @Override
             public void mouseExited(MouseEvent e) {
                setBackground(BACKGROUND_DEFAULT);
             }
    
          });
       }
    
       @Override
       public Dimension getPreferredSize() {
          int width = Math.max(super.getPreferredSize().width, PREF_W);
          int height = Math.max(super.getPreferredSize().height, PREF_H);
          return new Dimension(width, height);
       }
    }
    

    This suggests to me that the problem isn’t in the concept of a JLabel whose background changes via a MouseListener, but rather you’ve got a bug somewhere in your code. Where? Who knows until you post compilable runnable code, an SSCCE, like the one I’ve posted above.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
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.