Results 1 to 3 of 3
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default Problem with JComboBox and JRadioButton

    I want the program to add array in the combobox according to my selectedindex of the radiobuttons.

    Java Code:
    import java.awt.*;
    import javax.swing.JFrame;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class AnimalType extends JFrame implements ActionListener, ItemListener
    {	private JRadioButton cat;
    	private JRadioButton dog;
    	private JRadioButton bird;
    	private ButtonGroup animals;
    	private JComboBox Zoo;
    	private String catnames[]= {"Tom", "Meow", "Pete"};
    	private String dognames[] =  {"bobby", "Roy", "Lassie"};
    	private String birdnames[] =  {"kiko", "tweety", "birdy"};
    	private DefaultComboBoxModel cmb;
    	
    	public AnimalType()
    	{	super("zoo!");
    	setLayout( new FlowLayout() );
    	cat = new JRadioButton("cat",true);
    		dog = new JRadioButton("dog",false);
    		bird = new JRadioButton("bird",false);
    		animals = new ButtonGroup();
    		animals.add(cat);
    		animals.add(dog);
    		animals.add(bird);
    		add(cat);
    		add(dog);
    		add(bird);
    	
    	
    
    		
    	cmb = new DefaultComboBoxModel(catnames);
    	Zoo = new JComboBox(cmb);
    	add(Zoo);
    	Zoo.addItemListener(this);
    		cat.addItemListener(this);
    		dog.addItemListener(this);
    		bird.addItemListener(this);
    			bird.addActionListener(this);
    	}//end const.
    	public void actionPerformed(ActionEvent evt)
    	{
    		
    
    		if (bird.isSelected())
    		{cmb.removeAllElements();
    	 cmb.addElement(birdnames);
    		}
    	}
    	
    	public void itemStateChanged(ItemEvent evt)
    	{ 
    	Object source = evt.getSource();
    	if (cat.isSelected()){cmb.removeAllElements();
    	 //cmb.addElement(catnames);
    	 }
    
    	else if (dog.isSelected()){
    if (cmb != null)
    {cmb.removeAllElements();
    }
    
    cmb.addElement(dognames); 
    	 	}
    	 
    	else if (bird.isSelected()){cmb.removeAllElements();
    	// cmb.addElement(birdnames);	
    	}
    
    	}
    	public static void main(String[] args) {
          AnimalType test = new AnimalType();
          test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       	test.setSize(400,400);
        test.setVisible(true);
        }
    	}//end class
    Last edited by java_fun2007; 05-07-2008 at 12:18 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class AnimalTypeRx extends JFrame implements ActionListener,
                                                        ItemListener
    {
        private JRadioButton cat;
        private JRadioButton dog;
        private JRadioButton bird;
        private JComboBox Zoo;
        private String[] catNames =
        {
            "Tom", "Meow", "Pete"
        };
        private String[] dogNames =
        {
            "bobby", "Roy", "Lassie"
        };
        private String[] birdNames =
        {
            "kiko", "tweety", "birdy"
        };
        private DefaultComboBoxModel cmb;
    
        public AnimalTypeRx()
        {
            super("zoo!");
            setLayout( new FlowLayout() );
            cat = new JRadioButton("cat",true);
            dog = new JRadioButton("dog",false);
            bird = new JRadioButton("bird",false);
            ButtonGroup animals = new ButtonGroup();
            animals.add(cat);
            animals.add(dog);
            animals.add(bird);
            add(cat);
            add(dog);
            add(bird);
    
            cmb = new DefaultComboBoxModel(catNames);
            Zoo = new JComboBox(cmb);
            Dimension d = Zoo.getPreferredSize();
            System.out.printf("d = [%d, %d]%n", d.width, d.height);
            d.width = 85;
            Zoo.setPreferredSize(d);
            add(Zoo);
    
    //        Zoo.addActionListener(this);
            // This is all we need.
            cat.addActionListener(this);
            dog.addActionListener(this);
            bird.addActionListener(this);
    
            // Just to see what this can do.
            cat.addItemListener(this);
            dog.addItemListener(this);
            bird.addItemListener(this);
        }
    
        public void actionPerformed(ActionEvent evt)
        {
            JRadioButton rb = (JRadioButton)evt.getSource();
            String[] items = null;
            if(rb == cat)
                items = catNames;
            if(rb == dog)
                items = dogNames;
            if(rb == bird)
                items = birdNames;
            resetModelData(items);
        }
    
        private void resetModelData(String[] items)
        {
            cmb.removeAllElements();
            // The view component listens to the model and
            // updates itself when changes are made to the model.
            for(int i = 0; i < items.length; i++)
            {
                cmb.addElement(items[i]);
            }
        }
    
        public void itemStateChanged(ItemEvent evt)
        {
            if(evt.getStateChange() == ItemEvent.SELECTED)
            {
                JRadioButton rb = (JRadioButton)evt.getSource();
                if (rb == cat)
                {
                    System.out.println("cat selected");
                }
                else if (rb == dog)
                {
                    System.out.println("dog selected");
                }
                else if (rb == bird)
                {
                    System.out.println("bird selected");
                }
            }
        }
    
        public static void main(String[] args)
        {
            AnimalTypeRx test = new AnimalTypeRx();
            test.setDefaultCloseOperation(EXIT_ON_CLOSE);
            test.setSize(400,400);
            test.setVisible(true);
        }
    }

  3. #3
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

Similar Threads

  1. JList problem
    By zizou147 in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 08:50 AM
  2. searching within a JList
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 12:12 AM
  3. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 03:42 PM
  4. add a jlist column
    By Alan in forum JCreator
    Replies: 1
    Last Post: 05-28-2007, 04:51 AM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •