2 Replies - 5558 Views - Last Post: 26 January 2010 - 05:04 PM Rate Topic: -----

#1 UrbanTwitch  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 233
  • Joined: 27-September 09

Checkboxes in Java Applets

Posted 25 January 2010 - 07:02 PM

I am trying to create a java applet to add three checkboxes... however this is not working...

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class ComputerCheck extends Applet {
	public void paint(Graphics g) {
		g.drawString("Hello World",50,25);

			JCheckBox chk = new JCheckBox("This is the Check Box");
			g.add(chk);
			g.setSize(400, 400);
			g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	g.setVisible(true);
}
}


What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Checkboxes in Java Applets

#2 pinio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: Checkboxes in Java Applets

Posted 26 January 2010 - 04:40 PM

View PostUrbanTwitch, on 25 Jan, 2010 - 06:02 PM, said:

I am trying to create a java applet to add three checkboxes... however this is not working...

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class ComputerCheck extends Applet {
	public void paint(Graphics g) {
		g.drawString("Hello World",50,25);

			JCheckBox chk = new JCheckBox("This is the Check Box");
JCheckBox chk = new JCheckBox("This is the Check Box");
			add(chk); // not g.add(chk);
			setSize(400, 400); // not g.setSize(400, 400);
			//g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  <-- it's for a JFrame not for an Applet I think
				   //g.setVisible(true); <-- i don't thik you need this.
}
}


What am I doing wrong?



I'm newbie, but it work on Eclipse now.
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Checkboxes in Java Applets

Posted 26 January 2010 - 05:04 PM

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class ComputerCheck extends Applet {
	public void paint(Graphics g) {
		g.drawString("Hello World",50,25);

		JCheckBox chk = new JCheckBox("This is the Check Box");
		g.add(chk);
		g.setSize(400, 400);
		g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		g.setVisible(true);
	}
}



Well...you need to make up your mind on some things. Are you going to make an Applet or an Application? You cannot choose both, and since you are extending Applet, I'll assume you are doing just that. Also, all the methods that you have reference g, the Graphics object, not the Applet. As pinio said, you must reference the Applet, not the Graphics Object. However, it is also important for me to mention structure here. It is not a good idea, or even advisable to manipulate core properties, such as adding components, setting size, etc anywhere except in init(). (That is the first method called on any applet).

Therefore, the ONLY line of code that should be inside paint() is:g.drawString("Hello World",50,25);

Here is a revision.
import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class ComputerCheck extends Applet {
	JCheckBox chk = new JCheckBox("This is the Check Box");

	public void init() {
		add(chk); // Make that change to the APPLET, not the Graphics
		setSize(400, 400); // Make that change to the APPLET, not the Graphics
	}

	public void paint(Graphics g) {
		g.drawString("Hello World",50,25); // This call does manipulate Graphics object
	}
}



The other 2 lines are completely unnecessary in an Applet. The the main methods of an Applet object are as follows.
public void init ()
public void start ()
public void stop ()
public void destroy ()
public void paint (Graphics)



You can override those to do what you need to do. Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1