Reputation: 53
I found several questions about the issue and spend an hour on it, but still can not solve my problem myself. What I want to do is simply open a form and change the values of jLabel1 and jTextField1 using setText().
Main.java:
package defaultpackage;
public class Main {
public void main(String args[]) {
new InfoWindow().setVisible(true);
InfoWindow.Refresh("a", "b");
}
}
InfoWindow.java:
package defaultpackage;
public class InfoWindow extends javax.swing.JFrame {
public static void Refresh(String label, String textfield) {
jLabel1.setText(label);
jTextField1.setText(textfield);
}
public InfoWindow() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jTextField1.setText("jTextField1");
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
//<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(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InfoWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InfoWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
public static javax.swing.JLabel jLabel1;
public static javax.swing.JTextField jTextField1;
// End of variables declaration
}
The values are not updated in the window. What am I missing?
Upvotes: 0
Views: 1796
Reputation: 55594
Your main
-method in class Main
is missing the static
keyword, so you cannot run this one.
I assume that you did run the main
method of InfoWindow
, which does not call Refresh
.
Few more points:
JLabel
, JTextField
) should not be declared static
. You cannot share them between instances of your window.Refresh
non-static toorefresh
instead of Refresh
)That's how your Main
-class could look like:
public class Main {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
InfoWindow infoWindow = new InfoWindow();
infoWindow.setVisible(true);
infoWindow.refresh("a", "b");
}
});
}
}
Upvotes: 2
Reputation: 44250
Refresh
method on an instance of InfoWindow
I suggest you make Refresh
a non-static method and follow Java naming conventions. Also create and modify component in the EDT via SwingUtilities.invokeLater
.
Upvotes: 2