GUI Designer Question: How to dynamically set JLabel Text

I'm trying to create a JLabel for a form whose text I want to pass in through the constructor, e.g.

public class MyPanel {

     private JLabel versionLabel;
     private String version;

     public MyPanel(String version) {
          this.version = version;
     }

     private void createUIComponents() {
          versionLabel = new JLabel(version);
     }

///rest of class

}

However, the $$$setupUI$$() method insists on setting the text in the JLabel to whatever is set in the UI designer screen.  If nothing is set in the UI Designer Screen, it sets the text to "".  Is there any way I tell the UI Designer to not set the text value for the JLabel?


Thanks in advance,
Adam

5 comments
Comment actions Permalink

I figured it out.  I needed to :
1. right click on the JLabel in the UI designer and set it to its default value, which will keep the $$$setupUI$$$() from setting the value
2. remove the call to $$$setupUI$$$ that was in an init block and put it in the constructor, after I set the version string.


Thanks

0
Comment actions Permalink

A correct way is to check Custom Create property of that label.

0
Comment actions Permalink

Correct.  I neglected to mention that detail, but I did have the Custom Create option checked.  It was the fact that I had previously entered text into the designer that was making the auto-generated code block set the text.

0
Comment actions Permalink

Hello,

 

I have the same problem. I clicked the Custom Create and manually created the JLabel as follow:

todayDate = new JLabel();
todayDate.setText("La date aujourd'hui: " + LocalDate.now().toString());
However, this is not working as the text of the JLabel is always set to the default "Label" in the $$$setupUI$$$
todayDate.setText("Label");

Even after setting the text to empty on the GUI form designer, it doesn't work.

Basically, the call to CreateUIComponents() is done in the beginning of the $$$setupUI$$$, and then todayDate.setText("Label") is called, which basically reset the text to "Label". The issue here is that todayDate.setText("Label") should not be set automatically by the designer in $$$setupUI$$$. Any manual changes to $$$setupUI$$$ are reverted, so I don't know how to deal with this.

Any suggestions, please?

0
Comment actions Permalink

Oh! right clicking the text property on the designer and pressing "Restore default value" solved the problem!!

0

Please sign in to leave a comment.