0

ばかげているように見えますが、私が非常に初心者の Java コーダーにとって多くの問題を引き起こしているこの質問で、stackoverflow の周りのすべての Java 教祖を悩ませないことを願っています。

私の弁護において、私はこれについてグーグルで調べていて、達成したいことを達成できるかどうか(そして最終的にはどのように達成できるか)を説明できるものを私のレベルで見つけることができなかったことを言及しなければなりません。

xml を解析し、ユーザー フレンドリーなグラフィカルな形式で表示しています。この xml の読み込みの進行状況を表示しようとしているので、ユーザーは何が起こるかを知ることができます。

そこで、2 つのパネルを持つシンプルなフォームを作成しました。1 つは作業スペース全体を埋める上部、もう 1 つはステータス バーの役割を果たす下部です。上部にはシンプルなメニュー バーもあり、ユーザーはクリックfile -> loadしてファイルを読み込むことができます。file -> loadそのメニューボタンのクリックイベントでファイルをロードしています。

ユーザーがクリックfile -> loadすると、次のようになります。

JLabel progress_label = new JLabel("0%");
this.status_bar.add(progress_label);
this.status_bar.revalidate();
//some code that loads my xml
//inside loading loop I try to do this:
progress_label.setText(some_formula_to_calculate_percentage);
this.status_bar.revalidate();
//finish loading xml file
this.status_bar.remove(progress_label);
this.status_bar.revalidate();

上記のコードは問題なく動作しますが、細かい点が 1 つあります。イベントが発生した後に revalidate() が実行されていると思うので、イベントの実行中にラベルが追加されてから削除されるため、実際には何の効果も見られません。

ここに私の質問があります。イベント内からパネルを再検証する方法はありますか?

アドバイスありがとうございます。


編集 - アドバイスに従って、私がやろうとしていることを説明するサンプルコードを用意しました。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication6;

import javax.swing.JLabel;
/**
 *
 * @author
 */
public class NewJFrame extends javax.swing.JFrame {

/**
 * Creates new form NewJFrame
 */
public NewJFrame() {
    initComponents();
}

/**
 * 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">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jPanel1MouseClicked(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 476, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 358, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    JLabel test_label = new JLabel("test");
    test_label.setBounds(1, 1, 100, 100);
    this.jPanel1.add(test_label);
    this.jPanel1.revalidate();
    this.jPanel1.repaint();
    try
    {
        System.in.read();
    }
    catch ( Exception e )
    {
    }
    this.jPanel1.remove(test_label);
    this.jPanel1.revalidate();
}                                    

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}
4

1 に答える 1