update jlabel text after opening jdialog

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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 done a SwingWorker process

but JLabel text doesn't be updated

Is there any way to achieve this

Here is my dialog's src code:

package com.caisse.caisseFrame.dialogs;  import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Image; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException;  import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.SwingWorker;  import com.caisse.ConnectDB; import com.caisse.CustomComponent.JButtonCustom; import com.caisse.CustomComponent.JPanelCustom; import com.caisse.caisseFrame.CaisseFrame; import com.soqop.caisse.dao.TicketDAO;  public class LectureXDialog extends JDialog {      /**      *       */     private static final long serialVersionUID = 1L;     JButton ok_btn;     TicketDAO ticketDAO;      public LectureXDialog(final CaisseFrame parent) {         setAlwaysOnTop(true);         setSize(new Dimension(300, 280));         setLocationRelativeTo(parent);         setTitle("Lecture X");         setVisible(true);         setModalityType(ModalityType.DOCUMENT_MODAL);         parent.setEnabled(false);          ticketDAO = new TicketDAO(ConnectDB.getInstance());          final JPanelCustom componentsPanel = new JPanelCustom();         GridBagLayout componentsPanelLayout = new GridBagLayout();         componentsPanel.setLayout(componentsPanelLayout);          final JLabel lectureXlabel = new JLabel();         lectureXlabel.setFont(new Font("Cambria", Font.BOLD, 24));         lectureXlabel.setPreferredSize(new Dimension(250, 55));         lectureXlabel.setMinimumSize(new Dimension(250, 55));         GridBagConstraints gbc_lectureXlabel = new GridBagConstraints();         gbc_lectureXlabel.insets = new Insets(2, 2, 2, 2);         gbc_lectureXlabel.gridx = 0;         gbc_lectureXlabel.gridy = 0;         gbc_lectureXlabel.anchor = GridBagConstraints.CENTER;         componentsPanel.add(lectureXlabel, gbc_lectureXlabel);          ok_btn = new JButtonCustom("ok");         ok_btn.setFont(new Font("Cambria", Font.BOLD, 24));         try {             Image img = ImageIO.read(getClass().getResource("/resources/ok.png"));             ok_btn.setIcon(new ImageIcon(img));         } catch (IOException e1) {             e1.printStackTrace();         }         ok_btn.setPreferredSize(new Dimension(150, 55));         ok_btn.setMinimumSize(new Dimension(150, 55));         ok_btn.addActionListener(new ActionListener() {             @Override             public void actionPerformed(ActionEvent e) {                 parent.setEnabled(true);                 parent.setFocus();                 LectureXDialog.this.dispose();             }         });         GridBagConstraints gbc_ok_btn = new GridBagConstraints();         gbc_ok_btn.insets = new Insets(2, 2, 2, 2);         gbc_ok_btn.gridx = 0;         gbc_ok_btn.gridy = 1;         componentsPanel.add(ok_btn, gbc_ok_btn);          add(componentsPanel);          addWindowListener(new java.awt.event.WindowAdapter() {             @Override             public void windowClosing(java.awt.event.WindowEvent windowEvent) {                 parent.setEnabled(true);                 parent.setFocus();                 LectureXDialog.this.dispose();             }         });           SwingWorker worker = new SwingWorker<Boolean, Void>() {             String text = "";             @Override             public Boolean doInBackground() {                     text = "<html><center>Lecture X : " + ticketDAO.getLectureX() + "</center></html>";                 return true;             }              @Override             public void done() {                 lectureXlabel.setText(text);                 lectureXlabel.revalidate();                 lectureXlabel.repaint();                 componentsPanel.revalidate();                 componentsPanel.repaint();                 getContentPane().revalidate();                 getContentPane().repaint();             }         };      }  } 

回答1:

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();     } }; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!