Hi, ive got a bit of a problem in that i am trying to make text appear in a Jpanel at an exact xy coordinate e.g 200,300 but have found that Jlabel only allows relative layouts e.g Center, south etc
Any Ideas how i could fix this problem
cheers
Printable View
Hi, ive got a bit of a problem in that i am trying to make text appear in a Jpanel at an exact xy coordinate e.g 200,300 but have found that Jlabel only allows relative layouts e.g Center, south etc
Any Ideas how i could fix this problem
cheers
A simple solution would be to hard-code a replacement paint method for your JPanel. Note that the reason JLabel's are relative is because the panel can be re-sized, or other components can be added and the JPanel is trying to keep as much stuff from overlapping while still scaling stuff in the correct places. Simply re-design the way your GUI is organized to get the JLabel in the correct location.
Code :// over-written paint method public void paint(Graphics g) { super.paint(g); g.setColor(Color.black); g.drawString("This is my stubborn string.",300,200); }
Re: HelloWorld's response, there are two caveats.
First and more important, the method to override for performing custom painting in a JComponent subclass is paintCOmponent(...), not paint(...).
Second, the code shown iwll place the baseline of the String at 500 pixels from teh left and 200 pixels from the top of the JPanel, which may not fit the requirement.
db
::blush::
My bad. While technically the default paint method is called to draw the component and all sub-components, it is not the recommended method that should be over-ridden for custom component painting (which is strange, because it is in applets even though they just have a JPanel. Stupid double standards).
Firstly why do you use the JLabel for this. Use the paintComponent method in swing.