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 Getting error while trying to access Checkbox object from paint method 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 "Getting error while trying to access Checkbox object from paint method" Watch "Getting error while trying to access Checkbox object from paint method" New topic
Author

Getting error while trying to access Checkbox object from paint method

abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
Hi Everyone,

I am relatively new to Java programming. I have to design a GUI. I have one sample code which is throwing errors and I am not able to get rid of them.Here is my code -

--------------------------------------------------------------------------
<code>

package sample;
import java.awt.*;
import java.awt.event.*;

public class CheckboxSample extends Frame implements ItemListener , WindowListener {
String msg="";
/**
*
*/
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
CheckboxSample cb=new CheckboxSample();
cb.setSize(new Dimension(600,400));
cb.setTitle("An example for Checkbox");
cb.setVisible(true);

Checkbox Win98=new Checkbox("Windows 98" , true);
Checkbox WinNT=new Checkbox("WindowsNT" );
Checkbox Solaris=new Checkbox("Solaris");
Checkbox mac=new Checkbox("mac");
cb.setLayout(new FlowLayout(FlowLayout.CENTER));
cb.add(Win98);
cb.add(WinNT);
cb.add(Solaris);
cb.add(mac);

Win98.addItemListener(cb);
WinNT.addItemListener(cb);
Solaris.addItemListener(cb);
mac.addItemListener(cb);
cb.addWindowListener(cb);

}
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
repaint();

}

public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);

}

public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub

}
// Display current state of the check boxes.
public void paint(Graphics g) {


msg = "Current state: ";
g.drawString(msg, 200, 200);
msg = " Windows 98: " + Win98.getState();
g.drawString(msg, 200, 220);
msg = " Windows NT: " + WinNT.getState();
g.drawString(msg, 200, 240);
msg = " Solaris: " + Solaris.getState();
g.drawString(msg, 200, 260);
msg = " MacOS: " + mac.getState();
g.drawString(msg, 200, 280);
}


<\code>
---------------------------------------------------------------------------

I am getting the following error -

"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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
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.

--------------------------------------------------------------------------
<code>

package sample;
import java.awt.*;
import java.awt.event.*;

public class CheckboxSample extends Frame implements ItemListener , WindowListener {
String msg="";
CheckboxSample cb;
Checkbox Win98;
Checkbox WinNT;
Checkbox Solaris;
Checkbox mac;

/**
*
*/
private static final long serialVersionUID = 1L;

public CheckboxSample(){
cb=new CheckboxSample();
cb.setSize(new Dimension(600,400));
cb.setTitle("An example for Checkbox");
cb.setVisible(true);
Win98=new Checkbox("Windows 98" , true);
WinNT=new Checkbox("WindowsNT" );
Solaris=new Checkbox("Solaris");
mac=new Checkbox("mac");
cb.setLayout(new FlowLayout(FlowLayout.CENTER));
cb.add(Win98);
cb.add(WinNT);
cb.add(Solaris);
cb.add(mac);

Win98.addItemListener(cb);
WinNT.addItemListener(cb);
Solaris.addItemListener(cb);
mac.addItemListener(cb);
cb.addWindowListener(cb);
}

public static void main(String[] args) {




}
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
repaint();

}

public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);

}

public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub

}
// Display current state of the check boxes.
public void paint(Graphics g) {

super.paint(g);
msg = "Current state: ";
g.drawString(msg, 200, 200);
msg = " Windows 98: " + Win98.getState();
g.drawString(msg, 200, 220);
msg = " Windows NT: " + WinNT.getState();
g.drawString(msg, 200, 240);
msg = " Solaris: " + Solaris.getState();
g.drawString(msg, 200, 260);
msg = " MacOS: " + mac.getState();
g.drawString(msg, 200, 280);
}

<\code>
--------------------------------------------------------------------------

Now when I am running this code,I am not even getting the window.Apart from giving the solution,can you also type why this code is not working and why the new thing is working?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20


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:
abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
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)?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting error while trying to access Checkbox object from paint method