"Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: Win98 cannot be resolved WinNT cannot be resolved Solaris cannot be resolved mac cannot be resolved
at sample.CheckboxSample.paint(CheckboxSample.java:82) at sun.awt.RepaintArea.paintComponent(Unknown Source) at sun.awt.RepaintArea.paint(Unknown Source) at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) "
The problem is when I am trying to access the Checkbox instance objects (Win98,Solaris etc.) from the paint method. Please suggest me a way out.
Another problem is whenever I run similar code,the frame window appears but I do not get the awt controls(button,labels,etc) in it.Only when I maximise the window,then those Labels and Buttons appear in the window.After once maximising the window,the awt controls remain even after restoring the window to its original size.Can anyone tell me why is this happening and how can I correct it?
1) You declare the checkboxes in the main method, but try to access them in a different method. Declare these as instance fields of your class, and initialize them and the rest of the user interface in the Frame constructor instead of the main method.
2) In you paint(Graphics g) method, make sure you call super.paint(g); on your first line.
Thanks for replying to my query.I knew that I can not use the Checkbox object declared in main() in paint().But I was not able to find a way out of it.I applied the changes suggested by you.Below is the changed code.If I shift evrything to the Frame constructor what will remain in the main method?Since I have initialised the objects in the class,when at all I use them in main() it gives an error.
Inside your constructor, you are creating another instance. Which is creating another instance. Etc.
Remove the second line I quoted, and replace all references to "cb" with "this" (without quotes). Afterwards, remove the "cb" field.
Second, the main method is now empty. Place the following line in it:
Joined: Nov 29, 2007
Thanks a lot for your reply..my code is working fine now,with one small problem.When I run the code,a wondow of given size opens but the awt controls(Checkboxes) are not visible.When I maximise the window,they become visible.After maximising once,they are permanently visible even after I retstore them to the original size. Can you please take some more pains in explaining me couple of things.. Why was not the previous code working when I was using "cb" instead of "this" in the constructor?Can you explain this? Please explain me one more thing.Previosuly,the paint method eas throwing error when I was accessing the Checkboxes "Window98","WindowNT" etc. from it.(Window98.getState) What exactly removed this error?what is the significance od using super.paint(g)?
Originally posted by abhinav sinha: Why was not the previous code working when I was using "cb" instead of "this" in the constructor?Can you explain this?
In the first code, you had to use a reference to the frame, called cb. In the new code, you already had a reference, "this". "this" means a reference to the object you are in. Actually most times you can just ommit "this." in front of a field or method, because it is implicitly there.
Please explain me one more thing.Previosuly,the paint method eas throwing error when I was accessing the Checkboxes "Window98","WindowNT" etc. from it.(Window98.getState) What exactly removed this error?
Before, they were defined outside of the class but referenced inside the class. Now you define them inside the class as instance variables, and therefore they can be referenced inside methods without a problem.
what is the significance od using super.paint(g)?
super.paint(g) will do the default painting, which includes removing any previously paintings and the painting of the controls inside the frame.
subject: Getting error while trying to access Checkbox object from paint method