Reputation: 1069
I hope I won't bother all java gurus all around stackoverflow with this question that seem to be silly but cause a lot of trouble for quite newbie java coder I am.
In my defense I must mention I was googling for this and could not find anything on my level that could explain if I can achieve what I want to achieve (and eventualy how).
I'm parsing xml and showing it in nice user-friendly graphical form. I'm trying to show a progress of loading of this xml so user does know what happens.
So I created simple form with two panels - one on the top that fills whole working space and another on the bottom that plays the role of status bar. There is also simple menu bar on the top so user can click file -> load
to lad the file. I'm loading my file in on-click event of that file -> load
menu button.
So when user clicks file -> load
this is what happens:
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();
The code above works fine - except one little detail. I think revalidate() is being executed after event is fired so as label is being added and then removed during event was executed I can't actualy see any effect.
And here is my question - is there a way to revalidate my panel from inside of event?
Great thanks for any advice.
edit - following advice I prepared some sample code that explains what I'm trying to do.
/*
* 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
}
Upvotes: 0
Views: 309
Reputation: 324118
I think revalidate() is being executed after event is fired so as label is being added and then removed during event was executed I can't actualy see any effect
Sounds like your code is executing on the EDT. So you end up only seeing the end result.
Read the Swing tutorial on Concurrency. You will probably want to use a SwingWorker
so your long running code executes on a separate Thread and then you publish results which can be painted on the EDT.
Upvotes: 2