Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paint another component in paintComponent

Tags:

java

swing

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.

like image 987
Dmitry D Avatar asked Feb 23 '23 13:02

Dmitry D


1 Answers

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);
}
like image 177
Martijn Courteaux Avatar answered Feb 26 '23 02:02

Martijn Courteaux