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 14 of 14

Thread: why paintComponent method is not invoked?

  1. #1
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default why paintComponent method is not invoked?

    Hello guys

    Below code can't display text "Hello world", I guess it's because paintComponent method is not invoked, but why it is not invoked?

    Thanks heaps

    package xxx;
     
    import java.awt.*;
    import javax.swing.*;
     
    class TextPanel extends JPanel
    {
    // override the paintComponent method
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Font f = new Font("SansSerif", Font.BOLD, 14);
    Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);
    FontMetrics fm = g.getFontMetrics(f);
     
    int x = 75;
    int y = 100;
    g.setFont(f);
    g.drawString("Hello, ", x, y);
    x += fm.stringWidth("Hello, ");
    g.setFont(fi);
    g.drawString("World!", x, y);
    } //paintComponent
     
    public static void main(String[] args)
    {
    JFrame f = new JFrame("Your Frame");
    f.show();
    }
    }
    Last edited by helloworld922; November 10th, 2010 at 10:29 AM.


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: why paintComponent method is not invoked?

    Hi ice,
    maybe you lost this code:
    public static void main(String[] args)
    {
    JFrame f = new JFrame("Your Frame");
    f.setSize(300, 300);
    TextPanel panel = new TextPanel();
    f.add(panel);
    f.show();
    }
    If you don't set size for f, f is too small to see "Hello Word!" String

    Tell me somethig!
    Bye
    Last edited by helloworld922; November 10th, 2010 at 10:29 AM.

  3. #3
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: why paintComponent method is not invoked?

    Hi Lucasantos
    Thank you so much for your suggestion. I really tried to add your extra code in, but it still shows the same, whcih is showing an empty window with only the title ("Your Frame") on top after I maximumed the window to the same size of the computer screen.
    Any more thought?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: why paintComponent method is not invoked?

    You never add a TextPanel to your JFrame. You're showing a blank JFrame.

  5. #5
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: why paintComponent method is not invoked?

    Hi guys

    Thank you for your attention, I have absorbed member Lucasantos and Kevinworkman's advice: please see below code. but paintComponent method is still not invoked.
    Does anyone know why?

    Thanks heaps

    ////////////////////////////////////////////////////
    package xxx;
     
    import java.awt.*;
    import javax.swing.*;
     
    class TextPanel extends JPanel
    {
    // override the paintComponent method
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Font f = new Font("SansSerif", Font.BOLD, 14);
    Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);
    FontMetrics fm = g.getFontMetrics(f);
     
    int x = 75;
    int y = 100;
    g.setFont(f);
    g.drawString("Hello, ", x, y);
    x += fm.stringWidth("Hello, ");
    g.setFont(fi);
    g.drawString("World!", x, y);
    } //paintComponent
     
    public static void main(String[] args)
    {
    JFrame f = new JFrame("Your Frame");
    f.setSize(300, 300);
    TextPanel panel = new TextPanel();
    f.add(panel);
    f.show();
    }
     
    }
    Last edited by copeg; November 9th, 2010 at 10:35 PM.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: why paintComponent method is not invoked?

    Please surround your code with the [highlight=java][/highlight] tags, it makes it much easier to read and help troubleshoot.
    Some tips:
    1) f.show is deprecated, you should use JFrame.setVisible(boolean b)
    2) JFrame.getContentPane().add is more proper than JFrame.add
    But neither of these is the problem. Some tips for the problem at hand
    1) Add some System.out.println statements in the paintComponent to let you know it is being called.
    2) Set the preferred size of the JPanel, and call pack() on the JFrame before setting it to visible. Make sure the size you set incorporates what you are drawing, so you can see the results without having to resize the JFrame.
    Last edited by copeg; November 9th, 2010 at 11:11 PM.

  7. #7
    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: why paintComponent method is not invoked?

    paintComponent method is still not invoked.
    You keep saying that, but I don't see any code that lets you know whether the method is invoked or not.

    What is your assumption based on?

    db

  8. #8
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: why paintComponent method is not invoked?

    Hi Copeg

    Thank for very much your advice, now I add your tips in and put the code inside the tag. But it still shows a completely empty frame after I run it.

    Any more thought?

    package xxx;
     
    import java.awt.*;
    import javax.swing.*;
     
    class TextPanel extends JPanel
    {
    // override the paintComponent method
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Font f = new Font("SansSerif", Font.BOLD, 14);
    Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);
    FontMetrics fm = g.getFontMetrics(f);
     
    int x = 75;
    int y = 100;
    g.setFont(f);
    g.drawString("Hello, ", x, y);
    x += fm.stringWidth("Hello, ");
    g.setFont(fi);
    g.drawString("World!", x, y);
    } //paintComponent
     
    public static void main(String[] args)
    {
    JFrame f = new JFrame("Your Frame");
    f.pack();
                f.setVisible(true);
                f.setSize(300, 300);
                TextPanel panel = new TextPanel();
                f.getContentPane().add(panel);}
     
    }
    Last edited by ice; November 10th, 2010 at 04:48 AM.

  9. #9
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: why paintComponent method is not invoked?

    Hi Darryl
    Thank you for interesting in this thread, I run the code in Netbean, it only comes out an empty window. So I guess the method is not called?
    Last edited by ice; November 9th, 2010 at 11:35 PM.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: why paintComponent method is not invoked?

    That code seems to work fine for me. Are you sure you're recompiling? Are you sure you're running the correct project and file? Try compiling and running from the command prompt.

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: why paintComponent method is not invoked?

    now I add your tips in and put the code inside the tag. But it still shows a completely empty frame after I run it.
    You did not add any println statements in there. Add these, recompile, and run and you should see these lines being printed to the console. These will prove one way or another whether paintComponent is being called.

  12. #12
    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: why paintComponent method is not invoked?

    Quote Originally Posted by KevinWorkman View Post
    That code seems to work fine for me. Are you sure you're recompiling? Are you sure you're running the correct project and file? Try compiling and running from the command prompt.
    Kevin, I think you've hit the nail on the head.

    db

  13. #13
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: why paintComponent method is not invoked?

    Thank you so much, all of you kind guys! Today I successfully run this simple programming and it showed the string, finally solved it and I am so glad!

  14. #14
    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: why paintComponent method is not invoked?

    And as a good member of the forum community, you will share with us where the problem lay, won't you? That has the potential to benefit future readers of this thread who might be facing the same problem.

    db

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  2. [SOLVED] paintComponent is not reading my private variables
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 25th, 2010, 02:31 PM
  3. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM