JLabel setText not updating text

update jlabel text
jlabel set text
change jlabel text on button click
how to change the text of a jlabel in java
how to change jlabel text dynamically
java label settext not working
jlabel position
settext not working java

I am trying to update a JLabel by using the setText() method, but I can't redraw JLabel. Do I have to use the repaint() method to do that?

Here is the part of code, I do not get any errors, but it is not updating the JLabel.

public void actionPerformed(ActionEvent e) {
    fc = new JFileChooser();
    if(e.getSource() == addButton) {
         int returnVal = fc.showOpenDialog(Main.this);
         if (returnVal == JFileChooser.APPROVE_OPTION) {
                filesList = fc.getSelectedFiles();
                setFilesList(filesList);

                StringBuilder logString = new StringBuilder();
                logString.append("Files to Convert " + "\n");
                for(int i = 0; i < getFiles().length; i++) {
                    logString.append(filesList[i].getAbsolutePath());
                }
                //JLabel log = new JLabel(); created above.
                log.setText(logString.toString());
            } else {
                //log.append("Open command cancelled by user." + newline);
        }
        //log.setCaretPosition(log.getDocument().getLength());
    }
}

JLabel requires no repaint call. Simply calling setText(...) will change the label's text, and that is all that is required.

I wonder if your problem is a concurrency issue, that you are doing a long-running process on the Swing event thread and that this is preventing your label from updating its text.

If so, then consider doing your long-running process in a background thread such as that provided by a SwingWorker, and then updating your JLabel's text on the Swing thread, such as can be done via the SwingWorker's publish/process methods.

For more on this, please have a look at the Lesson: Concurrency in Swing tutorial.

Also Mario De... is correct about not being able to print simple new-lines on a JLabel. 1+ to his answer.

Help: JLabel setText not updating, Hi, I'm using a JLabel at the bottom of my application as a status bar where I'm trying to The problem I'm running into is that the setText is not updating statusLabel System.out.println("Status Bar Text = " + statusLabel. 2) What does this mean: "I wonder if your problem is a concurrency issue, that you are doing a long-running process on the Swing event thread and that this is preventing your label from updating its text."

I'm a bit stumped on how the repainting of frames/component works in Java. You can Paint(Graphics g), update(Graphics g) which according to the javadoc just calls paint(g). Finally there's also repaint()...

If none of those seem to work, wouldn't it just be easier to create the label only at the line where you are currently trying to set the text?

Edit: there is also the option of using an ineditable textArea. Not only can it display the standard newline character, but you can put it in a jScrollPane, which is handy when you have lots of files in the log, and you don't need to repaint text components to display updated text. The bonus is magnificent imo...

JLabel.setText() is NOT Updating the Text (Swing / AWT / SWT forum , When i try to update the JLabel with the New String. Its not Happening. There is a string in my code "private String populateDashes = "";" JLabel requires no repaint call. Simply calling setText() will change the label's text, and that is all that is required. I wonder if your problem is a concurrency issue, that you are doing a long-running process on the Swing event thread and that this is preventing your label from updating its text.

This simple example works for me so problem is not JLabel but some bug in other part of your source code. Please post full source code.

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Application {

public static void main(String[] args) {
    JFrame frame = new JFrame("JLabel test");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    Panel p = new Panel();
    p.setLayout(new GridLayout());

    Button button = new Button("Change");
    final JLabel label = new JLabel(Long.toString(Long.MAX_VALUE));

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            label.setText(Long.toString(e.getWhen()));
        }
    });

    p.add(button);
    p.add(label);
    frame.add(p, BorderLayout.NORTH);
    frame.pack();

}
}

setText() is not always updating Jtextfield and Jlabel items, JTextFields for user input and matching JLabels for informational output for users. I am completely puzzled why "setText" does not work in this instance, but works everywhere else I show quoted text -. > > I am completely  To update the text in a label you use label.setText("New text"). However, without seeing the code, it's hard to say why it doesn't update, as there may be other things wrong.

I have run into a similar problem. I tried to setText("Good Bye") in actionPerformed() in an exit button ActionListener before disposing my JFrame right there, but the text was not changing.

Eventually I realized that my label was not getting updated as I was disposing the frame in the anonymous ActionListener class. After I had let the code leave ActionListener.actionPerformed(), the label text got updated.

I had to dispose my JFrame in a new thread though to ensure that:

  1. actionPerformed is finished so that the main thread returns from the anonymous nested class and updates the label in the main class.

  2. A new thread is started which waits for a second to allow "Good Bye" to be read.

  3. This new thread the disposes the frame.

How can I force a JLabel to update after setText()? Solutions , However, the label text won't update until the tasks have all been completed. I can simulate this by making the thread sleep for 3 seconds (see  In a simple Java application I have a JButton "btnGo" and a JLabel "lblStatus" within a JPanel. However, the label text won't update until the tasks have all been completed. I can simulate this by making the thread sleep for 3 seconds (see code below).

repaint() won't work here.

Simply use label_name.paintImmediately(label_name.getVisibleRect());

It will get updated right away.

How to Use Labels (The Java™ Tutorials > Creating a GUI With JFC , With the JLabel class, you can display unselectable text and images. If you need to create a Below is the code from LabelDemo.java that creates the labels in the previous example. ImageIcon icon LEFT and RIGHT . void setText(String) Label setText() working, but in UI text is not updated. If this is your first visit, you may have to register before you can post. To start viewing messages, select the forum that you want to visit from the selection below.

Java Code Examples javax.swing.JLabel.setText, JLabel class. You can vote up the examples you like. Your votes will be used in our system to get more good examples. Example  setText method not updating JLabel fa21984e-8b8a-4514-b629-538b53b36423 Jun 26, 2015 8:23 AM Hi I am new to Java and I am having problem updating a label from a different actionPerformed class.

Java Program to change JLabel text after creation, Now change the above JLabel text using setText() − void main(String args[]) { JFrame frame = new JFrame("Label Example"); JLabel label;  Help: JLabel setText not updating If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register or Login before you can post: click the register link above to proceed.

Update « JLabel « Java Swing Q&A, i am using a java thread to update jlabel text value in my application but after it While an event handler is running, no other GUI processing -- which includes call setText on that? it doesnt seem right to use a getter if you want to set the text,​  The text not updating also applies to other Views as well, such as the EditText and Button. Why this happens is explained in this article. This tutorial is also useful in helping to understand why the User Interface (UI) in your Android app might not be as responsive as expected. The underlying problem is the same.

Comments
  • I'd begin by saying a jLabel cannot display the newline character. Wrap it in <html> and use <br/> tags.
  • Are you sure you're not re-initializing the log variable to a new JLabel instance after you added it to your layout manager?
  • yes, actually i intialized it with the constructor.
  • JLabel requires no repaint call. Simply calling setText(...) will change the label's text, and that is all that is required.
  • You're right on the mark about JLabel not allowing simple new-lines, and using JTextArea instead. 1+.