
My program has different combo boxes i want to select all my components but when i select one JComboBox it's work if i selected next one the program terminates could you please tell me the answer for this

Hi Friend,
Try the following code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class JComboBoxExample {
public static void main(String[] args)
{
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab1=new JLabel("Qualification:");
final JComboBox combo1=new JComboBox();
combo1.addItem("B tech");
combo1.addItem("MBA");
combo1.addItem("MCA");
combo1.addItem("BBA");
JButton b=new JButton("Get");
JLabel lab2=new JLabel("Department:");
final JComboBox combo2=new JComboBox();
combo2.addItem("Technologies");
combo2.addItem("Finance");
combo2.addItem("Marketing");
combo2.addItem("Computer Science");
lab1.setBounds(20,20,100,20);
combo1.setBounds(120,20,150,20);
lab2.setBounds(20,50,100,20);
combo2.setBounds(120,50,150,20);
b.setBounds(120,80,80,20);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String value1 = combo1.getSelectedItem().toString();
String value2 = combo2.getSelectedItem().toString();
JOptionPane.showMessageDialog(null,"You have selected '"+value1+"' from Combo1 and '"+value2+"' from Combo2");
}
});
f.add(lab1);
f.add(combo1);
f.add(lab2);
f.add(combo2);
f.add(b);
f.setVisible(true);
f.setSize(400,150);
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.