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!
As far as I'm concerned, there's no direct way to do this. But you could try some of the following:
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;
}
}