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 Error in one of my classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Error in one of my classes" Watch "Error in one of my classes" New topic
Author

Error in one of my classes

Keshan Pillay
Greenhorn

Joined: May 21, 2008
Posts: 29
Good evening everyone!

Ive made an object class called Rectangle.java
and im playing around with it, just trying to get things to work.
Ive put this method into it


but It gives me this error:



Can anyone help me understand this?
Many thanks!
K Pillay
Keshan Pillay
Greenhorn

Joined: May 21, 2008
Posts: 29
Sorry the name of the class is Rectangles.java and not Rectangle.java as I stated in the above post.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Have you imported java.awt.Graphics or java.awt.*?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Keshan Pillay
Greenhorn

Joined: May 21, 2008
Posts: 29
hehe thanks for the reply, I worked out that I had forgotten to import awt maybe 2 mins before checking this thread. However I have another problem now...



My code has to switch from the title screen to the other section of the paintcomponent method when a key is pressed. Can anyone work out why it isnt?
Keshan Pillay
Greenhorn

Joined: May 21, 2008
Posts: 29
By sticking in a bunch of System.out.println statements into all my methods to narrow where my error is.
And ive worked out that the problem is that when I press a key, the keyPressed method isnt called at all. I cant seem to work out why.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
KeyEvents always come from some source, most usually some sort of text field. A KeyListener receives KeyEvents from that source only after it has been registered to the source via the addKeyListener() method. A class that does not have this method most likely never issues key events, maning that it is somewhat unaware of keyboard imput. In your case, however, it's possible: A JPanel can receive keyboard interactions and has an addKeyListener() method - inherited from java.awt.Component, by the way. So you just have to add the KeyListener to your JPanel, in case of your (pretty un-clean) way of implementing JPanel and KeyListener in one class, this would be one line in your constructor:
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
The tutorial located here says:
Note: To fire keyboard events, a component must have the keyboard focus.


I would suggest checking to ensure the component has focus.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Sorry, missed the line in your constructor where you add the listener ... too quick at reading
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I suggest this thread be moved to the AWT / Swing forum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
All right, Rob, here goes . . . to the Swing Forum!
Keshan Pillay
Greenhorn

Joined: May 21, 2008
Posts: 29
oops sorry for posting in the wrong forum. My lecturer suggested (Through email) that I put requestFocusInWindow in my constructor method, but this hasn't worked either.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Originally posted by Keshan Pillay:
My lecturer suggested (Through email) that I put requestFocusInWindow in my constructor method, but this hasn't worked either.
Not something I am familiar with myself, but I would have thought requesting focus goes inside a Listener associated with a particular component, not the constructor. Read this.

BTW: It's constructor or method, not both ("constructor method").
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in one of my classes