I am trying to create a character creator using JCheckBox
. I have created all my classes, however, when extending each class into the next, I am unable to see the return results for the prior class. For example, I have a Gender
class and a Race
class. The Gender
class has two check boxes, one for male and one for female. By itself, this class works. However, when I use the Race
class to extend the Gender
class, everything shows up, but clicking the boxes does not output into the JTextField
. Below is my Code for Gender
and for Race
.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gender extends JFrame implements ItemListener
{
FlowLayout flow = new FlowLayout();
JLabel label = new JLabel("What gender would you like to be?");
JCheckBox male = new JCheckBox("Male", false);
JCheckBox female = new JCheckBox("Female", false);
JTextField GENDER = new JTextField(12);
String output, insChosen;
String Gender = "";
public Gender()
{
super("Character Creation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label.setFont(new Font("Arial", Font.ITALIC, 15));
GENDER.setText("Gender" + Gender + ".");
male.addItemListener(this);
female.addItemListener(this);
add(label);
add(male);
add(female);
add(GENDER);
}
public void itemStateChanged(ItemEvent event)
{
Object source = event.getSource();
int selects = event.getStateChange();
if(male.isSelected())
Gender = "Male";
else
if(female.isSelected())
Gender = "Female";
GENDER.setText("Gender: " + Gender);
}
public static void main(String[] args)
{
final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 600;
Gender frame = new Gender();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
}
}
and here is the code for Race.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Race extends Gender
{
FlowLayout flow = new FlowLayout();
JLabel label = new JLabel("What race would you like to play as?");
JCheckBox elf = new JCheckBox("Elf", false);
JCheckBox troll = new JCheckBox("Troll", false);
JCheckBox human = new JCheckBox("Human", false);
JCheckBox dwarf = new JCheckBox("Dwarf", false);
JCheckBox orc = new JCheckBox("Orc", false);
JTextField RACE = new JTextField(12);
String output, insChosen;
String Race = "";
public Race()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label.setFont(new Font("Arial", Font.ITALIC, 15));
RACE.setText("Race" + Race + ".");
elf.addItemListener(this);
troll.addItemListener(this);
human.addItemListener(this);
dwarf.addItemListener(this);
orc.addItemListener(this);
add(label);
add(elf);
add(troll);
add(human);
add(dwarf);
add(orc);
add(RACE);
}
public void itemStateChanged(ItemEvent event)
{
Object source = event.getSource();
int select = event.getStateChange();
if(elf.isSelected())
Race = "Elf";
else
if(troll.isSelected())
Race = "Troll";
else
if(human.isSelected())
Race = "Human";
else
if(dwarf.isSelected())
Race = "Dwarf";
else
if(orc.isSelected())
Race = "Orc";
RACE.setText("Race: " + Race);
}
public static void main(String[] args)
{
final int FRAME_WIDTH = 350;
final int FRAME_HEIGHT = 300;
Race frame = new Race();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
}
}
Each work seperately, but will not work in conjuction. Any help would be appreciated.
Because you've overridden itemStateChanged
in Race
, which was inherited from Gender
but failed to call super.itemStateChanged
, it is no longer been called in Gender
.
Add super.itemStateChanged(event);
to Race
's itemStateChanged
method, for example:
public void itemStateChanged(ItemEvent event) {
super.itemStateChanged(event);
Consider using JRadioButton
s, grouped through the use of a ButtonGroup
for the gender, as it's (typically) and either/or selection, maybe even a JComboBox
which will limit what the user can select
I would avoid extending directly from JFrame
as you've automatically limited any future expansion or use of the class.
Instead, consider using a JPanel
instead. This would mean you wouldn't "need" to extend from Gender
but could simply "include" within the Race
class.
I'd also encourage you to separate the model/data from the UI itself, so the UI modifies the model and responds to changes made to the model, but that's just me