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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:00:56+00:00 2026-05-11T19:00:56+00:00

I’m working in Java, and I have a JPanel in a JFrame. In that

  • 0

I’m working in Java, and I have a JPanel in a JFrame. In that JPanel, among other things, I have a JLabel that I want to make appear and disappear at will. I’ve tried setting visibility to true/false, adding and removing it from the JFrame and JPanel, and, having looked online, I tried validate()ing and revalidate()ing ad infinitum. What can be done here to solve this problem?

  • 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-11T19:00:56+00:00Added an answer on May 11, 2026 at 7:00 pm

    In general, calling the setVisible method is sufficient to make a Swing component to be shown or hidden.

    Just to be sure that it works, I tried the following:

    public class Visibility {
      private void makeGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        final JLabel l = new JLabel("Hello");
        final JButton b = new JButton("Hide Label");
        b.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            l.setVisible(false);
          }
        });
    
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(l, BorderLayout.CENTER);
        f.getContentPane().add(b, BorderLayout.SOUTH);
        f.setSize(200, 200);
        f.setLocation(200, 200);
        f.validate();
        f.setVisible(true);
      }
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new Visibility().makeGUI();
          }
        }); 
      }
    }
    

    The above program is able to affect the visibility by clicking on a JButton.

    Could it be a Threading Issue?

    My next suspicion was that perhaps a Thread that is not on the event dispatch thread (EDT) may not be affecting the display immediately, so I added the following after initializing the JLabel and JButton.

    Thread t = new Thread(new Runnable() {
      public void run() {
        while (true) {
          b.setVisible(!b.isVisible());
    
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) { /* Handle exception /* }
        }
      }
    });
    
    t.start();
    

    With the new Thread running, it changed the toggled the visibility of the JLabel every 100 ms, and this also worked without a problem.

    Calling a Swing component off the event dispatch thread (EDT) is a bad thing, as Swing is not thread-safe. I was a little surprised it worked, and the fact that it works may just be a fluke.

    Repaint the JPanel?

    If the JLabel‘s visibility is only being affected on resizing, it probably means that the JLabel is being drawn only when the JPanel is being repainted.

    One thing to try is to call the JPanel‘s repaint method to see if the visibility of the JLabel will change.

    But this method seems to be just a band-aid to a situation, if the main cause is due to a thread off the EDT is attempting to make changes to the GUI. (Just as a note, the repaint method is thread-safe, so it can be called by off-EDT threads, but relying on repaint is a workaround than a solution.)

    Try using SwingUtilities.invokeLater

    Finally, probably the thing I would try is the SwingUtilities.invokeLater method, which can be called (and should only be called) from a thread running separate from the EDT, if it wants to affect the GUI.

    So, the earlier Thread example should be written as:

    Thread t = new Thread(new Runnable() {
      public void run() {
        while (true) {
          try {
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                b.setVisible(!b.isVisible());
              }
            });
          } catch (Exception e1) { /* Handle exception */ }
    
          try  {
            Thread.sleep(100);
          } catch (InterruptedException e) { /* Handle exception */ }
        }
      }
    });
    
    t.start();
    

    If the change to the GUI is indeed occurring on a separate thread, then I would recommend reading Lesson: Concurrency in Swing from The Java Tutorials in order to find out more information on how to write well-behaving multi-threaded code using Swing.

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

Sidebar

Ask A Question

Stats

  • Questions 271k
  • Answers 271k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have a series on my blog where I build… May 13, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer You can redirect Console.In, Console.Out and Console.Error to custom StringWriters,… May 13, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer I mean, if I have an Average of 5seconds, does… May 13, 2026 at 1:46 pm

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.