Problem displaying CheckboxGroup on JFrame

I was recently assigned the task of creating exams. I am trying to create a CheckboxGroup in a class that was passed the Graphics2D tool. This code compiles with no errors when I call it's method:

setLayout(new GridLayout(1, 5));

CheckboxGroup cbg = new CheckboxGroup();

add(new Checkbox("Never", cbg, false));

add(new Checkbox("Almost Never", cbg, false));

add(new Checkbox("Sometimes", cbg, false));

add(new Checkbox("Almost All The Time", cbg, false));

add(new Checkbox("All The Time", cbg, false));

but nothing displays on my JFrame, I tried to draw it with this line(g2 is my Graphics2D tool):

g2.draw(cbg);

but I get an error when compiling that says draw doesn't work. Am I totally off-track with this or is there a simple Graphics2D method that I use to draw this CheckboxGroup?

[855 byte] By [jasonmeiercaa] at [2007-11-27 9:35:57]
# 1
don't try to "draw" it. Let's see the rest of your code, but use code tags, please. You can read about them here: http://forum.java.sun.com/help.jspa?sec=formatting
petes1234a at 2007-7-12 23:03:52 > top of Java-index,Java Essentials,New To Java...
# 2

You should not mix Swing components with AWT ones.

Try this code:

import javax.swing.*;

import java.awt.*;

public class Jason{

public static void main(String[] args){

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container con = frame.getContentPane();

con.setLayout(new GridLayout(5, 1));

ButtonGroup bg = new ButtonGroup();

bg.add((JRadioButton)con.add(new JRadioButton("Never", false)));

bg.add((JRadioButton)con.add(new JRadioButton("Almost Never", false)));

bg.add((JRadioButton)con.add(new JRadioButton("Sometimes", false)));

bg.add((JRadioButton)con.add(new JRadioButton("Allmost All The Time", false)));

bg.add((JRadioButton)con.add(new JRadioButton("All The Time", false)));

frame.setSize(300, 300);

frame.setVisible(true);

}

}

hiwaa at 2007-7-12 23:03:53 > top of Java-index,Java Essentials,New To Java...
# 3

hiwa: Thanks for the tip, I won't mix those libraries anymore

pete: Here is all of my code so far sorry it's sloppy, I'm just getting back into coding after one semester of Intro to Computer Science using Java almost a year ago::

/*this class has main function**************************************************/

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class PDS_Questionnaire

{

public static void main(String[] args)

{

//declare JFrame window dimensions

int WIDTH = 500;

int HEIGHT = 400;

//create JFrame

JFrame frame = new JFrame();

frame.setSize(HEIGHT, WIDTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//create object of painter class to install onto frame

ScoreCard survey = new ScoreCard();

//add object to frame and set visible

frame.add(survey);

frame.setVisible(true);

}

}

/*********************end of class***************************************/

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/****************************new class**********************************/

//this class is the JFrame painter, it initiates the Graphics2D class then passes the

//Graphics object to PDS_Scorer so that the PDS_Scorer class can draw on the patientsCard

import javax.swing.JComponent;

import java.awt.Graphics;

import java.awt.Graphics2D;

public class ScoreCard extends JComponent

{

public ScoreCard()

{

}

public void paintComponent(Graphics g)

{

//recover Graphics2D

Graphics2D g2 = (Graphics2D) g;

//create another class and pass the Graphics2D tool

PDS_Scorer patientsCard = new PDS_Scorer(g2);

//call method to draw the check boxes

patientsCard.drawCheckBoxes();

}

}

/*********************end of class***************************************/

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/****************************new class**********************************/

import javax.swing.JComponent;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridLayout;

import java.awt.Checkbox;

import java.awt.CheckboxGroup;

public class PDS_Scorer extends JComponent

{

public PDS_Scorer(Graphics2D gTwo)

{

g2 = gTwo;

}

public void partA_Scorer()

{

}

public void partB_Scorer()

{

}

public void partC_Scorer()

{

}

public void partD_Scorer()

{

}

public void partE_Scorer()

{

}

public void partF_Scorer()

{

}

public void drawCheckBoxes()

{

// draw3DRect(int x, int y, int width, int height, boolean raised)

//Draws a 3-D highlighted outline of the specified rectangle.

g2.draw3DRect(10, 10, 10, 10, true);//this works

setLayout(new GridLayout(1, 5));

CheckboxGroup cbg = new CheckboxGroup();

add(new Checkbox("Never", cbg, false));

add(new Checkbox("Almost Never", cbg, false));

add(new Checkbox("Sometimes", cbg, false));

add(new Checkbox("Almost All The Time", cbg, false));

add(new Checkbox("All The Time", cbg, false));

//g2.drawString("One Lazy Fox", 22.5, 55.5);

}

private Graphics2D g2;

}

jasonmeiercaa at 2007-7-12 23:03:53 > top of Java-index,Java Essentials,New To Java...