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