**
        //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);

                }
            }
        }

Recommended Answers

All 9 Replies

I'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, so you have no idea how many times it will be called, and thus no idea how many times you will add the check box.

ps: Do not bump your thread, especially after only 5 minutes. People will answer when they are ready, not because you are nagging.

Sorry about that, so are there any possible things I can do to achieve the output? It should only be in basic coding though, thank you.

Without 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.

What am I supposed to do again? Should I delete this part? and add it where?add(checkBox1); checkBox1.setVisible(true); The program's output well, is supposed to be a list that when you select among the Primary etc will display shapes + checkboxes that will serve as the choice to determine what color the shape has.

Rather 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 again). You can do that in the itemStateChanged method.
And yes, you should definitely remove those lines from the paint method.

How should I add it in the itemstatechanged when the checkboxes appear when a user selects a choice from the list? In here:

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);



        }

Sorry, I don't know how to make that any clearer without actually doing your homework for you. Just re-read the previous posts carefully, and then give it a try

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.