update jlabel text after opening jdialog

こ雲淡風輕ζ 提交于 2019-12-02 13:13:30

To make your Swing Worker to do the background work you need to call the SwingWorker#execute() method. If you don't add it, well... it won't know when to start working. It's like any other variable like:

JLabel label = new JLabel("Hello world");

But if you never add that label to anything (or like an ActionListener not added to the JButton) it will never be added / executed.

worker.execute();

Inside your constructor or wherever you want it to start working.


As per @MadProgrammer's comment:

Remember, SwingWorker can return data, so instead of returning Boolean, you could return String and use get in the done method to get the returns of the worker, somewhat safer and more usable then using an instance field

He refers to change your worker's code like this:

SwingWorker worker = new SwingWorker<Boolean, Void>() {
    @Override
    public String doInBackground() {
        return "<html><center>Lecture X : " + ticketDAO.getLectureX() + "</center></html>";
    }

    @Override
    public void done() {
        lectureXlabel.setText(get());
        lectureXlabel.revalidate();
        lectureXlabel.repaint();
        componentsPanel.revalidate();
        componentsPanel.repaint();
        getContentPane().revalidate();
        getContentPane().repaint();
    }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!