Search code examples
javaswingjlabel

JLabel does not update when using setText method


In the project I am currently working on I have several pieces of information that I would like to make visible via Jlabel. There are a few buttons and textfields elsewhere in the GUI that allow for altering said information I would like to update the JLabel but the text never changes, or updates upon startup.

I have attempted to use concurrency to update the labels, as suggested in other questions on this site, but I have had no luck with the labels updating. The concurrency does work with updating textfields and comboBoxes as needed.

The current iteration of my code looks as follows,

The JFrame

//// This is a snippet from the JFrame

public void start()
{
    this.setSize(900, 700);
    this.setVisible(true);

    devicePanel.populateDeviceDefinitions();
    updateServiceInfo();
    updateCommandInfo();
    startUpdateTimer();
}

public void updateServiceInfo()
{
    EventService service = JetstreamApp.getService();

    generalPanel.updateServiceInfo(service.getBaseUri(), 
            service.getAccessKey(), String.valueOf(service.getWindowTime()));
}

public void updateCommandInfo()
{
    JetstreamServiceClient client = JetstreamApp.getClient();

    generalPanel.updateCommandInfo(client.getBaseUri(), client.getAccessKey());
}

The JPanel named generalPanel

//// This is a snippet from the generalPanel
//// All of the variables in the following code are JLabels

public void updateServiceInfo(String baseUrl, String accessKey,
        String windowTime)
{
    serviceUrl.setText(baseUrl);
    serviceAccessKey.setText(accessKey);
    serviceWindowTime.setText(windowTime);
}

public void updateCommandInfo(String baseUrl, String accessKey)
{
    commandUrl.setText(baseUrl);
    commandAccessKey.setText(accessKey);
}

The labels start with an Empty string for their text and upon window start it is intended that they be updated by grabbing the information from the relevant sources. Can I please have some insight as to why the JLabels never update and display their information?


Solution

  • How did you create the JLabel? If the text starts out as "", and you've created it with new JLabel(""), the width of the JLabel may be initialized to 0 and then none of your text would show up when you update it. I believe I've had that sort of problem in the past. As a test, try using new JLabel("aaaaaaaaaa") or some longer string to create the label, then setText(""); then later, when you setText(somethingElse), see if that causes text to show up. If it does, then the width is probably the problem and you can work on it from there. – ajb 19 mins ago

    This comment is the actual answer, when creating a JLabel with an empty string as the text the label's dimensions do not get set properly when using WindowBuilderPro. My labels did exist, and were being updated with the code provided in my question but the labels were not visible.

    Starting with a label that has text in it, then setting the text to an empty string works properly.