JLabel setText not updating text

前端 未结 5 1811
日久生厌
日久生厌 2020-12-21 16:44

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?

5条回答
  •  無奈伤痛
    2020-12-21 17:07

    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.

提交回复
热议问题