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

Thread: problem with paintComponent

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default problem with paintComponent

    import java.awt.Graphics;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Painting extends JFrame {
    	public Painting() {
    		JPanel panel = new JPanel();
    		this.setSize(333, 200);
    		this.setVisible(true);
     
    		this.add(panel);
     
    		panel.add(new Extra("Jasmin Rahimic"));
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	public static void main(String[] args) {
    		Painting c = new Painting();
    	}
    }
     
    class Extra extends JComponent {
    	String str;
     
    	public Extra(String s) {
    		this.setSize(70, 70);
    		this.str = s;
    	}
     
    	public void paintComponent(Graphics g) {
    		g.drawString(str, 50, 50);
    	}
    }

    Can someone tell me why paintComponent() is not being called.
    Thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with paintComponent

    First thing to check: add an @Override in front of it to be sure you are overriding and not creating a new method.
    Call setVisible() AFTER all your GUI has been put together.

    Override the getPreferredSize() method.
    Last edited by Norm; July 21st, 2011 at 02:51 PM.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: problem with paintComponent

    try to add LayoutManager to your panel GridLayout will be good for this example.
    By the way...u dont have to use "this" into Painting class...Methods like setSize and others refer to your class automatically...(because she inherent from JFrame)
    Last edited by remigio; July 21st, 2011 at 02:49 PM.

  4. The Following User Says Thank You to remigio For This Useful Post:

    jasox (July 21st, 2011)

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: problem with paintComponent

    Word to the wise, call the super constructor beforehand.

    So put this as the first call in your constructor:
    super();

    If that doesn't work, put the super.paintComponent call in your paintComponent method.
    So have this instead:
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    		g.drawString(str, 50, 50);
    	}

    The next thing I would do is confirm the JComponent is showing up like you'd expect. The easiest way of doing it would be to create a border around the JComponent. Putting this line in your Extra constructor will handle that for you:
    setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.black));

    If the component does not show up (you won't see a border, or it will be very small), try using the setPreferredSize() method instead of the setSize method.

    If, after you've confirmed the JComponent is showing and you've added the super call in the constructor and the paintComponent, call the repaint method on the created Extra object. If all of that doesn't work, then I'll need more info on the problem.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    jasox (July 21st, 2011)

  7. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem with paintComponent

    Thank you guys.

  8. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: problem with paintComponent

    You can just call setPreferredSize(..) in the Extra constructor (instead of calling setSize(..) or overriding setPreferredSize(..)). But the width of 70 id unlikely to be big enough to display the whole string you want to use.

Similar Threads

  1. paintComponent() instead of paint()
    By Ciaran54 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 22nd, 2011, 02:46 PM
  2. paintComponent(Graphics g)
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2011, 08:55 AM
  3. Throwing an Exception in paintComponent
    By jmack in forum Exceptions
    Replies: 1
    Last Post: January 31st, 2011, 08:12 AM
  4. why paintComponent method is not invoked?
    By ice in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 11th, 2010, 12:30 AM
  5. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM

Tags for this Thread