Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-03-2010, 06:43 PM
Junior Member
 

Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
whoismrsaxon is on a distinguished road
Default Text in Swing

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



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 11-03-2010, 05:33 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
Default Re: Text in Swing

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.

Java 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);
}
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #3 (permalink)  
Old 11-03-2010, 09:35 AM
Darryl.Burke's Avatar
Member
 

Join Date: Mar 2010
Location: Madgaon, Goa, India
Posts: 158
Thanks: 3
Thanked 13 Times in 13 Posts
Darryl.Burke is on a distinguished road
Default Re: Text in Swing

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
Reply With Quote
  #4 (permalink)  
Old 11-03-2010, 03:36 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
Default Re: Text in Swing

::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).
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #5 (permalink)  
Old 26-03-2010, 11:22 AM
Junior Member
 

Join Date: Mar 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
vinay is on a distinguished road
Default Re: Text in Swing

Firstly why do you use the JLabel for this. Use the paintComponent method in swing.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
java program to copy a text file to onother text file francoc File I/O & Other I/O Streams 3 23-04-2010 08:10 PM
Swing JTable sorting chronoz13 Algorithms & Recursion 2 30-01-2010 12:51 PM
Swing Timers Sterzerkmode AWT / Java Swing 5 11-11-2009 01:10 AM
java swing help JM_4ever AWT / Java Swing 3 07-10-2009 11:42 AM
Help - Swing Timer, 2 KeyEvents Gheta AWT / Java Swing 2 29-07-2009 07:46 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jbutton java jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:48 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.