Reputation: 737
I made a simple GUI using Swing and everything is just working fine but the JLabel isn't updating when I used the .setText method. I'm really getting confused about the problem as the JLabel should work properly.
// Variables declaration
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
public NewClass() {
initComponents();
}
private void initComponents() {
jDesktopPane1 = new javax.swing.JDesktopPane();
jLabel1 = new javax.swing.JLabel();
jLabel1.setText("Hello JLabel!");
jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewClass().setVisible(true);
new NewClass().start();
}
});
}
private void start() {
this.jLabel1.setText("Hello!");
}
Upvotes: 0
Views: 146
Reputation: 4974
I don't exactly understand what you are trying to do but if you are trying to update the jLabelText at the start of execution try this
public NewClass() {
initComponents();
this.setVisible(true);
jLabel1.setText("YourText");
}
Upvotes: 0
Reputation: 324118
new NewClass().setVisible(true);
new NewClass().start();
You have created two instances of the NewClass class.
You only want one instance, then you can set the text for the label on the visible frame:
NewClass frame = new NewClass();
frame.setVisible(true);
frame.start();
If you want to change a property of any Object, then you need a reference to the Object. You can't just keep using the "new" statement.
Upvotes: 1