File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes paintComponent() Method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "paintComponent() Method?" Watch "paintComponent() Method?" New topic
Author

paintComponent() Method?

sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Hi,

i am working on a example given below, using the paintComponent() method of the JPanel class, which has to be overridden if in case we have to embed code for the widget that we want to display on a JPanel and in turn on a Frame.



Output Result 1: This program compiles well but does not run and outputs the error as :Exception in thread "main" java.lang.NoSuchMethodError: main

Output Result 2: Now when i embed this paintComponent() method inside a main() method, i get the following error at compile time :

MyWidget.java:12: illegal start of expression
public void paintComponent(Graphics g)
^
MyWidget.java:19: ';' expected
^
2 errors.

I am aware of the fact that the paintComponent() should not be called by us, i.e the system calls this method. Please correct me if am wrong and help in resolving the above problem .

thank you.

[edit]Add code tags. CR[/edit]
[ September 27, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
You will always get an error if you try to start an application from a class without a main method.
If you put "public" inside a method the compiler will always flag that as an error; you can't have methods inside methods.

By the way: it isn't public void paintComponent(Graphics g). If you use Java 1.4.2 or older it's protected void paintComponent(Graphics g) and if you use Java 5 or newer it's
@Override
protected void paintComponent(Graphics g)
.
And you always put
super.paintComponent(g);
as the first line of that method.
sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Originally posted by Campbell Ritchie:

By the way: it isn't public void paintComponent(Graphics g). If you use Java 1.4.2 or older it's protected void paintComponent(Graphics g) and if you use Java 5 or newer it's
@Override
protected void paintComponent(Graphics g)
.



Hi Campbell, That was really an important input from you.


First of all since i had to get this widget on a panel and in turn on a frame, i had not invoked this paintComponent() method from the other class that adds the widget/panel on the frame.

Now i have got that worked out.

sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Originally posted by Campbell Ritchie:

And you always put
super.paintComponent(g);
as the first line of that method.[/QB]


Now what i understand is that whatever code we want to code for the graphics(our own widget) that goes into the paintComponent()method which i will be overriding in my class.

Is calling the paintComponent() method of the super class mandatory,now since i know that even if i don't the compiler will place a call to the method of the super class. Am i right?


Now having said that..

can you please tell me what role does the super class's paintComponent() method will play in my code if i call it or think the compiler makes a call.

Thank you ...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
now since i know that even if i don't the compiler will place a call to the method of the super class. Am i right?

No. The compiler will insert that call in a constructor, but not in a method.

what role does the super class's paintComponent() method will play in my code

You might want to work through the Java Tutorial's chapter on Swing Painting; it explains this (and contains a lot of other useful knowledge).


Ping & DNS - my free Android networking tools app
sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Thank you Mr Campbell and Dittmer,

One thing I learned is 'read to understand' is very important.

What Mr. Dittmer has told in his reply that 'a compiler places a automatic call only in the constructor only in the constructor but not in the method' was really an eye opening point. since even though i had read from different books about that i did not lay much stress on that point.

Also, the link was really helpful, Mr Dittmer.

Thanks to all..
daniela butnar
Greenhorn

Joined: Mar 16, 2009
Posts: 1
Campbell Ritchie wrote:You will always get an error if you try to start an application from a class without a main method.
If you put "public" inside a method the compiler will always flag that as an error; you can't have methods inside methods.

By the way: it isn't public void paintComponent(Graphics g). If you use Java 1.4.2 or older it's protected void paintComponent(Graphics g) and if you use Java 5 or newer it's
@Override
protected void paintComponent(Graphics g)
.
And you always put
super.paintComponent(g);
as the first line of that method.


Hi,

I still don't understand.

I have this example, I don't have errors but it doesn't shows me anything.


I don't know what I do wrong. If you can, pleas help me.

Thank you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Hello Dannielle,

Please read your private messages regarding an important announcement.

Thank you,

Rob

Also, http://faq.javaranch.com/java/DontWakeTheZombies

The problem with your code is that you only create a panel, but a panel cannot be displayed by itself. You must add it to a JFrame (or JDialog), and call setVisible(true) on the frame.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alexander McDougall
Greenhorn

Joined: Jan 08, 2013
Posts: 5
Hi there,

I am currently experiencing the same problem.

This is the code:



I checked in a previous post that you need:

super.paintComponent(g);

in the paintComponent method.

Any one able to help?

Regards

Alex
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Alexander McDougall wrote:I am currently experiencing the same problem.
/snip/
Any one able to help?


Which same problem? There's not just one problem discussed in this thread.

Also, the problems are resolved. If you can't figure out a solution from what's posted above, chances are you have a different problem.


luck, db
There are no new questions, but there may be new answers.
Alexander McDougall
Greenhorn

Joined: Jan 08, 2013
Posts: 5
Hi Darryl,

I experienced a similar problem to Sameer Khazi in his original post.

I realised that the code I provided is used together with another class.

The class that extends JPanel is called each time a user clicks a button. This other class
creates a JFrame, creates a button, adds a listener to the button, creates a panel, adds the button and panel to the frame,
and when the user clicks on the button, the code in the actionPerformed method is executed, which in
this case is frame.repaint(). This causes the paintComponent method to be called. I did not need to add a
main method to the original code I posted.

Thanks for the reply.

Regards

Alex
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: paintComponent() Method?
 
Similar Threads
NoSuchMethodError!!!
Simple JPanel question
how to implement the main() in the following code?
Swing probelm
Not able to compile this piece of code