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
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
// 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.