I need a little help with swing. This is my code:
public class UIdostawca extends javax.swing.JFrame {
/** Creates new form UIdostawca */
public UIdostawca() {
initComponents();
setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
}
/* This is my function */
public void loadStuff() {
jLabel2.setText("Works or not?");
}
/*
A lot of code generated by NETBEANS
*/
// Variables declaration - do not modify
private javax.swing.JLabel jLabel2;
}
I use it like this:
UIdostawca a = new UIdostawca();
a.loadStuff();
and the jLabel2 didn’t change ;(
However when in execute
jLabel2.setText("Works or not?");
in function like formWindowOpened
everything works
It’s a multi-threading issue. When
formWindowOpenedis called, it’s called by the Swing event dispatch thread (EDT), which is the same thread that draws the interface, so it has the right value of text to draw.If you call
setTextfrom some other thread, the EDT will probably not get the right value for the text. Wherever you callsetTextin some other thread, you have to wrap it in aSwingUtilities.invokeLater(), which whill change the text value on the EDT.