打开jdialog后更新jlabel文本

时间:2017-02-15 17:33:52

标签: java swing jlabel swingworker

我必须查询数据库以获取要在JLabel上写入的数据并加速打开对话框

在完成JLabel流程

后,我创建了SwingWorker,没有文字和设置文字

JLabel文字未更新

有没有办法实现这个

这是我的对话框的src代码:

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 个答案:

答案 0 :(得分:2)

要让Swing Worker进行后台工作,您需要调用SwingWorker#execute()方法。如果你不添加它,那么...它将不知道何时开始工作。它就像任何其他变量一样:

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

但是,如果您从未将label添加到任何内容(或ActionListener未添加到JButton),则永远不会添加/执行。

worker.execute();

在构造函数中或您希望它开始工作的任何位置。

根据@ MadProgrammer的评论:

  

请注意,SwingWorker可以返回数据,因此您可以返回Boolean而不是返回String,并使用get方法中的done来获取返回工作者,使用实例字段

更安全,更有用

他指的是改变你的工人代码:

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