实现Runnable的JDialog不显示内容

时间:2014-06-04 23:20:18

标签: java swing runnable jdialog

对于Java应用程序,我正在尝试创建一个非常简单的“加载”对话框,以便在按下按钮时执行长时间操作。我通过在对话框中打开一个带有gif的线程来完成它,并在函数返回时关闭对话框和线程。

对话框打开,但不显示图像或文本。

这是按钮代码:

private void botonResolverActionPerformed(java.awt.event.ActionEvent evt) {
    if(areaTematicaSeleccionada != ""){
        Cargando c = new Cargando();
        Thread performer = new Thread(c);
        performer.start();
        cpresentacion.resuelveAreaTematica(areaTematicaSeleccionada);
        cpresentacion.actualizaAreaTematicaSeleccionadaGestores(areaTematicaSeleccionada);
        c.dispose();
        try {
            performer.join();
        } catch(InterruptedException e) {
            System.out.println("Ooops");
        }
    }
}

这是加载类,在另一个类中:

private static class Cargando extends javax.swing.JDialog implements Runnable {
    /** Creates new form test */
    public Cargando() {
        super((JDialog)null, true);
        initComponents();
    }

    public void run() {
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentacion/loading_small.gif"))); // NOI18N

        jLabel2.setText("Cargando...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(62, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(54, 54, 54))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents              

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration 

}

但是如果我在内部类上放置一个main并单独运行它,那就完美了。关于如何显示对话框内容或如何实现简单的“加载”事件的任何想法?

1 个答案:

答案 0 :(得分:1)

Thread performer = new Thread(c);
//...
try {
    performer.join();
} catch(InterruptedException e) {
    System.out.println("Ooops");
}

向被阻止的事件调度线程问好。基本上这意味着EDT无法处理新事件(包括绘画请求),因为它在performer.join电话被阻止。

请查看Concurrency in Swing了解详情。

您有几个基本选项。

你可以......

使用SwingWorker执行长时间运行的操作,并利用它的done方法在用户完成后更新用户界面。

这也提供了有用的方法,可以在需要时轻松地将更新同步回UI

根据您的示例代码,我会将对话框的引用传递给SwingWorker并覆盖它的done方法,以便您可以dispose对话框。