I use SwingPaintDemo2 from Java Tutorials:
http://download.oracle.com/javase/tutorial/uiswing/examples/painting/SwingPaintDemo2Project/src/painting/SwingPaintDemo2.java
I modified it like this:
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // Draw Text
    g.drawString("This is my custom Panel!",10,20);
    JLabel c = new JLabel("Label");
    c.paint(g);
}
g.drawString works fine. But how can I paint JLabel from this method? It doesn't work.
I think you have to set a size to your label.
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // Draw Text
    g.drawString("This is my custom Panel!",10,20);
    JLabel c = new JLabel("Label");
    c.setBounds(0, 0, 400, 30);
    c.paint(g);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With