I have a simple JPanel (otherJPanel) with a label below, both inside another JPanel (mainJPanel). mainJPanel
overrides paintComponent
, and repaint()
is called manually elsewhere and the label text and the background of otherJPanel
are set dynamically.
However, when I move the label inside otherJPanel
then paintComponent
is never called. Any idea why?
To test this I have created the test project below with labels in both positions. However, with this paintComponent
is not called within the processing loop, what am I doing wrong?
package uk.co.moons.gui.components.testapps;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DisplayJFrame extends javax.swing.JFrame {
/** Creates new form DisplayJFrame */
public DisplayJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
displayJPanel1 = new uk.co.moons.gui.components.DisplayJPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N
displayJPanel1.setName("displayJPanel1"); // NOI18N
getContentPane().add(displayJPanel1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
public void draw() {
displayJPanel1.revalidate();
displayJPanel1.repaint();
}
public void setTO(TestObject to) {
displayJPanel1.setTo(to);
}
public static void main(String args[]) {
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(DisplayJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DisplayJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DisplayJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DisplayJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
DisplayJFrame df = new DisplayJFrame();
df.setVisible(true);
TestObject to = new TestObject();
df.setTO(to);
for (int i = 10; i < 20; i++) {
to.setValue(i);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(DisplayJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
df.draw();//df.repaint();
}
}
});
}
// Variables declaration - do not modify
private uk.co.moons.gui.components.DisplayJPanel displayJPanel1;
// End of variables declaration
}
package uk.co.moons.gui.components;
import java.awt.Graphics;
import uk.co.moons.gui.components.testapps.TestObject;
public class DisplayJPanel extends javax.swing.JPanel {
private TestObject to = null;
/** Creates new form DisplayJPanel */
public DisplayJPanel() {
initComponents();
}
public TestObject getTo() {
return to;
}
public void setTo(TestObject to) {
this.to = to;
}
@Override
public void paintComponent(Graphics g) {
System.out.println("paintComponent");
super.paintComponents(g);
if (to != null) {
int i = to.getValue();
System.out.println("paintComponent " + i);
jLabel1.setText(String.valueOf(i));
jLabel2.setText(String.valueOf(i + 1));
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setName("Form"); // NOI18N
setLayout(new java.awt.BorderLayout());
jPanel1.setName("jPanel1"); // NOI18N
jPanel1.setLayout(new java.awt.BorderLayout());
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(uk.co.moons.gui.controlpanel.ControlPanelApp.class).getContext().getResourceMap(DisplayJPanel.class);
jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel2.setName("jLabel2"); // NOI18N
jPanel1.add(jLabel2, java.awt.BorderLayout.CENTER);
add(jPanel1, java.awt.BorderLayout.CENTER);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel1.setName("jLabel1"); // NOI18N
add(jLabel1, java.awt.BorderLayout.PAGE_END);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
package uk.co.moons.gui.components.testapps;
public class TestObject {
private int value=0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}