JLabel setText not updating text

前端 未结 5 1788
日久生厌
日久生厌 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:05

    repaint() won't work here.

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

    It will get updated right away.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-21 17:11

    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...

    0 讨论(0)
  • 2020-12-21 17:15

    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.
    0 讨论(0)
  • 2020-12-21 17:21

    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();
    
    }
    }
    
    0 讨论(0)
提交回复
热议问题