Expert:vandana
Hi, This is a similar kind of question i had asked before, with a little difference. Two JCombobox and a JtextField. When I select an Item from the first Combo(i.e.,Select,First,Second or Third),the set of items in the second combo should change(i.e.,when "Select" is selected 2nd combo and textfield is disabled, when "First" is selected 2nd combo should have -[Select,One,Two,Three], when "Second" is selected 2nd combo should have -[Select,Two,Three], when "Third" is selected 2nd combo should have -[Select,Three]). Now when I select One,Two,Three from second combo, the textfield should display 1,2,3 respectively. I have given my code below in which the 2nd combo changes according to first.But the textfield is displaying nothing.I have commented the part I have problem with. public class Compare extends JFrame implements ItemListener { JComboBox combo,lcombo; JTextField number;
public Compare() { setLayout(null); String a[]={"Select","First","Second","Third"}; combo=new JComboBox(a);combo.setBounds(50,50,100,20); combo.addItemListener(this); add(combo);
lcombo=new JComboBox(); lcombo.addItemListener(this);lcombo.setEnabled(false); add(lcombo);lcombo.setBounds(50,100,100,20);
number=new JTextField(5);number.setEditable(false); add(number);number.setBounds(50,150,100,20); setSize(300,300); }
public void itemStateChanged(ItemEvent e) { if(e.getSource() == combo) { if(combo.getSelectedItem().equals("Select")) { lcombo.setEnabled(false); number.setEditable(false); } else if(combo.getSelectedItem().equals("First")) { lcombo.setEnabled(true); number.setEditable(true); String b[]={"Select","One","Two","Three"}; lcombo.removeAllItems(); for(int i=0;i<b.length;i++) { lcombo.addItem(b[i]); } } else if(combo.getSelectedItem().equals("Second")) { lcombo.setEnabled(true); number.setEditable(true); String b[]={"Select","Two","Three"}; lcombo.removeAllItems(); for(int i=0;i<b.length;i++) { lcombo.addItem(b[i]); } } else if(combo.getSelectedItem().equals("Third")) { lcombo.setEnabled(true); number.setEditable(true); String b[]={"Select","Three"}; lcombo.removeAllItems(); for(int i=0;i<b.length;i++) { lcombo.addItem(b[i]); } } } /*else if(e.getSource() == lcombo) { if(lcombo.getSelectedItem().equals("Select")) { number.setText("");number.setEditable(false); } else if(lcombo.getSelectedItem().equals("One")) { number.setText("1");number.setEditable(true); } else if(lcombo.getSelectedItem().equals("Two")) { number.setText("2");number.setEditable(true); } else if(lcombo.getSelectedItem().equals("Three")) { number.setText("3");number.setEditable(true); } }*/ } public static void main(String args[]) { (new Compare()).setVisible(true); } } |