Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-07-2008, 01:17 AM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
Problem with JComboBox and JRadioButton
I want the program to add array in the combobox according to my selectedindex of the radiobuttons.

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 02:18 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-07-2008, 03:29 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-07-2008, 10:58 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
thanks hardwired now it's working fine.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JList problem zizou147 Advanced Java 1 04-17-2008 10:50 AM
searching within a JList newtojava7 New To Java 1 03-10-2008 02:12 AM
Help with JList Albert NetBeans 1 07-13-2007 05:42 PM
add a jlist column Alan JCreator 1 05-28-2007 06:51 AM
jcombobox Freddie AWT / Swing 4 05-11-2007 02:48 AM


All times are GMT +3. The time now is 03:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org