Results 1 to 3 of 3
Thread: Problem with JComboBox and Jlist
- 05-06-2008, 11:17 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 35
- Rep Power
- 0
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 classLast edited by java_fun2007; 05-07-2008 at 12:18 AM.
- 05-07-2008, 01:29 AM #2
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); } }
- 05-07-2008, 08:58 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
JList problem
By zizou147 in forum Advanced JavaReplies: 1Last Post: 04-17-2008, 08:50 AM -
searching within a JList
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-10-2008, 12:12 AM -
Help with JList
By Albert in forum NetBeansReplies: 1Last Post: 07-13-2007, 03:42 PM -
add a jlist column
By Alan in forum JCreatorReplies: 1Last Post: 05-28-2007, 04:51 AM -
jcombobox
By Freddie in forum AWT / SwingReplies: 4Last Post: 05-11-2007, 12:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks