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.

Results 1 to 5 of 5

Thread: Text in Swing

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    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.

    // 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);
    }

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    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

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    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).

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text in Swing

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

Similar Threads

  1. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  2. Swing JTable sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 30th, 2010, 08:51 AM
  3. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM
  4. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 06:42 AM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM