update jlabel text after opening jdialog

前端 未结 1 574
日久生厌
日久生厌 2021-01-26 05:48

I have to query database to get data to write on JLabel and to accelerate opening dialog

I have created JLabel with no text and set text after

相关标签:
1条回答
  • 2021-01-26 06:40

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