• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

paintComponent() Method?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
sameer khazi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Alexander McDougall
Greenhorn
Posts: 5
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic