**
//When an item from the list is selected, a checkbox along with the drawn graphics schould appear. but only the graphics appear. Help? *basic**
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Checkbox;
public class GetSelectedItemExample extends Applet implements ItemListener
{
Choice c;
CheckboxGroup g1 = new CheckboxGroup();
Checkbox checkBox1 = new Checkbox("Red");
public void init()
{
c = new Choice();
c.add("Select Option");
c.add("Primary Colors");
c.add("Secondary Colors");
c.add("Tertiary Colors");
add(c);
c.addItemListener(this);
add(checkBox1);
checkBox1.setVisible(false);
}
public void itemStateChanged(ItemEvent e)
{
repaint();
}
public void paint(Graphics g)
{
String s1 = c.getSelectedItem();
if(s1 == "Primary Colors")
{
g.drawString("You have chosen:"+c.getSelectedItem(),100,100);
g.setColor(Color.RED);
g.drawRect(90,120,100,100);
g.fillRect(90,120,100,100);
add(checkBox1);
checkBox1.setVisible(true);
}
if(s1 == "Secondary Colors")
{
g.drawString("You have chosen:"+c.getSelectedItem(),100,100);
}
if(s1 == "Tertiary Colors")
{
g.drawString("You have chosen:"+c.getSelectedItem(),100,100);
}
}
}
SYvonneMendoza
0
Newbie Poster
Recommended Answers
Jump to PostI've never seens anyone add GUI components inside a paint method before. It wouldn't surprise me if that didn't get painted, at least not until there was some otehr event that caused another repaint.
Anyway, it's a terrible idea. Paint gets called for all kinds of reasons, at any time, …
Jump to PostWithout knowing exactly what yuir program is supposed to do, it's hard to give a definite answer. At a guess I would suggest testing the selected item and adding check boxes (if necessary) inside the itemStateChanged method. Your paint should do painting and nothing else.
Jump to PostRather than adding checkboxes at various times (and when do you remove them?), it would be much safer to add them all when initialising, but make them all invisible. Then, depending on which choice the user makes you can make the relevant check boxes visible (and all the others invisible …
All 9 Replies
SYvonneMendoza
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
SYvonneMendoza
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
SYvonneMendoza
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
SYvonneMendoza
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
SYvonneMendoza
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.