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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:44:02+00:00 2026-05-21T10:44:02+00:00

I have JLabel with an icon and text. Is there any possibility to hide

  • 0

I have JLabel with an icon and text. Is there any possibility to hide only JLabel’s text? I don’t want to hide whole component (setVisible(false)), but only text, so an icon remains visible. I’d like to still use getText and setText methods.

Thanks for Your help!

  • 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-21T10:44:02+00:00Added an answer on May 21, 2026 at 10:44 am

    As far as I’m concerned, there’s no direct way to do this. But you could try some of the following:

    1. Extend JLabel and override the setText() and getText() methods. These should store the text you give it in a new String field. Every time you call setText it should only delegate to super.setText() is your label-text is not invisible. Then you could add a method that switches visibility. If you call setTextVisibility() with true, the class should call super.setText() with a string of spaces.

    Here’s an example of what I mean:

      public class MyLabel extends JLabel {
        private String labelText;
        private boolean labelTextVisible = true;
    
        private MyLabel( String text, Icon icon, int horizontalAlignment ) {
          super( text, icon, horizontalAlignment );
          labelText = text;
        }
    
        private MyLabel( String text, int horizontalAlignment ) {
          super( text, horizontalAlignment );
          labelText = text;
        }
    
        private MyLabel( String text ) {
          super( text );
          labelText = text;
        }
    
        @Override
        public void setText( String text ) {
          if ( labelTextVisible ) {
            super.setText( text );
          }
          labelText = text;
        }
    
        @Override
        public String getText() {
          return labelText;
        }
    
        public void setLabelTextVisible( boolean labelVisible ){
          if(labelVisible){
            if(!labelText.equals( super.getText() )){
              super.setText( labelText );
            }
          }else{
            int spaceCount = super.getText().length();
            String hiddenText = "";
            for ( int i = 0; i < spaceCount; i++ ) {
              hiddenText+=" ";
            }
            super.setText(hiddenText);
          }
          this.labelTextVisible = labelVisible;
        }
    
        public boolean getLabelTextVisible(){
          return labelTextVisible;
        }
      }
    
    1. (This is more of a hack, but it could work) Make the foregroundcolor of the label match the background color so the text is no longer visible.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JLabel which has a lot of text on it. Is there
I have a JLabel that needs to display some html-formatted text. However, I want
I have a JFrame window with GridBagLayout(). There is a JLabel with Icon -
I have a JLabel with a Icon and text aligned to bottom, the icon
I have this code want to make JList talk text and icon what must
Hi is it possible to have a JLabel text growing to the right instead
I have an applet. In this I have a JLabel component. When the user
I have a subclass of JLabel that forms a component of my GUI. I
I have a JScrollPane that holds a JLabel using the following code: //Create TEXT
I am trying to get the JLabel icon to appear above the text for

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.