Draw another component in paintComponent

I am using SwingPaintDemo2 from the Java tutorials:

http://download.oracle.com/javase/tutorial/uiswing/examples/painting/SwingPaintDemo2Project/src/painting/SwingPaintDemo2.java

I changed 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 great. But how can I draw a JLabel from this method? This does not work.

+4
source share
3 answers

I think you should set the size of 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); } 
+4
source

See the source LabelRenderTest.java for this thread . The label, after all, is drawn on the screen, but before displaying it, it is displayed before the BufferedImage .

BUWfe.png

Important source line:

 textLabel.setSize(textLabel.getPreferredSize()); 
+4
source
 JLabel label_name = new JLabel("Some text"); label_name.setBounds(position_x, position_y, width, height); label_name.setFont(new Font("Dialog", Font.PLAIN, 10)); add(label_name); 
-1
source

All Articles