File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Changing Labels on an Checkbox Group Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Changing Labels on an Checkbox Group" Watch "Changing Labels on an Checkbox Group" New topic
Author

Changing Labels on an Checkbox Group

Michael Steen
Greenhorn

Joined: Feb 13, 2002
Posts: 4
Can anyone point me in the correct direction on how to change the label string based on the particular checkbox selected? Instead of the if else if statements I would like to change the gravity string assigned to the checkbox label to represent the distacne from the sun when the button is selected. A portion of the code is below.
CheckboxGroup planetGroup = new CheckboxGroup();
Checkbox mercuryPlanetBox = new Checkbox("Mercury's Gravity = .38",false,planetGroup);
Checkbox venusPlanetBox = new Checkbox("Venus's Gravity = .91",false,planetGroup);
Checkbox earthPlanetBox = new Checkbox("Earth's Gravity = 1",false,planetGroup);
Checkbox marsPlanetBox = new Checkbox("Mars's Gravity = .38",false,planetGroup);
Checkbox jupiterPlanetBox = new Checkbox("Jupiter's Gravity = 2.54",false,planetGroup);
Checkbox saturnPlanetBox = new Checkbox("Saturn's Gravity = 1.08",false,planetGroup);
Checkbox uranusPlanetBox = new Checkbox("Uranus's Gravity = .91",false,planetGroup);
Checkbox neptunePlanetBox = new Checkbox("Neptune's Gravity = 1.19",false,planetGroup);
Checkbox plutoPlanetBox = new Checkbox("Pluto's Gravity = .06",false,planetGroup);
//Checkbox hiddenBox = new Checkbox("",true,planetGroup);
Label outputLabel = new Label(" ");

public void init()
{
//Add components to window
setLayout(new GridLayout(15, 30));
setBackground(Color.yellow);
add(planetNameLabel);
add(planetChoiceLabel);
add(gravityLabel);
add(outputLabel);
add(mercuryPlanetBox);
mercuryPlanetBox.addItemListener(this);
add(venusPlanetBox);
venusPlanetBox.addItemListener(this);
add(earthPlanetBox);
earthPlanetBox.addItemListener(this);
add(marsPlanetBox);
marsPlanetBox.addItemListener(this);
add(jupiterPlanetBox);
jupiterPlanetBox.addItemListener(this);
add(saturnPlanetBox);
saturnPlanetBox.addItemListener(this);
add(uranusPlanetBox);
uranusPlanetBox.addItemListener(this);
add(neptunePlanetBox);
neptunePlanetBox.addItemListener(this);
add(plutoPlanetBox);
plutoPlanetBox.addItemListener(this);
}
public void itemStateChanged(ItemEvent choice)
{
//Output
if (mercuryPlanetBox.getState())
{
sundistance = "Mercury's distance from the Sun is 36 million miles.";
}
else if (venusPlanetBox.getState())
{
sundistance = "Venus's distance from the Sun is 67.2 million miles.";
}
else if (earthPlanetBox.getState())
{
sundistance = "Earth's distance from the Sun is 93 million miles.";
}
else if (marsPlanetBox.getState())
{
sundistance = "Mars's distance from the Sun is 141.6 million miles.";
}
else if (jupiterPlanetBox.getState())
{
sundistance = "Jupiter's distance from the Sun is 483.6 million miles.";
}
else if (saturnPlanetBox.getState())
{
sundistance = "Saturn's distance from the Sun is 886.7 million miles.";
}
else if (uranusPlanetBox.getState())
{
sundistance = "Uranus's distance from the Sun is 1,784 million miles.";
}
else if (neptunePlanetBox.getState())
{
sundistance = "Neptune's distance from the Sun is 2,794.4 million miles.";
}
else if (plutoPlanetBox.getState())
{
sundistance = "Pluto's distance from the Sun is 3,674.5 million miles.";
}
outputLabel.setForeground(Color.blue);
outputLabel.setText(sundistance);
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Micheal,
Since you are doing such repetitions I would simplify your init method and use a method for creation of the checkboxes. You don't need to keep track of all those checkboxes as class variables if you make use of the name tag that goes with any component. For your case, a good name might be:
planetName, distance
Then you can make use of the StringTokenizer class to pull it apart and use the pieces for your various labels.
You should also use static variables for the names and values since they will be constant no matter how many classes we create.
The code below illustrates what I have just described.

Enjoy,
Manfred.
Michael Steen
Greenhorn

Joined: Feb 13, 2002
Posts: 4
Reply thanks for the help. This is more advanced than my Java knowledge but should prove to be useful to expanding that knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing Labels on an Checkbox Group