JComboBox:
This is the class which is used to create a combo box in swing using it's constructor.
itemStateChanged():
This is the method which receives the ItemEvent generated by the addItemListener() method of the JComboBox class. The event is generated when you select item from the combo box.
The output of the above code is as below
This is the class which is used to create a combo box in swing using it's constructor.
itemStateChanged():
This is the method which receives the ItemEvent generated by the addItemListener() method of the JComboBox class. The event is generated when you select item from the combo box.
The program code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Myprojects {
JComboBox combo;
JTextField txt;
public Myprojects() {
String course[] = {"COmputer Science", "Software Engineering", "Computer Systems", "Networking"};
JFrame frame = new JFrame("Creating a JComboBox Component");
JPanel panel = new JPanel();
combo = new JComboBox(course);
combo.setBackground(Color.green);
combo.setForeground(Color.white);
txt = new JTextField(30);
panel.add(combo);
panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
String str = (String) combo.getSelectedItem();
txt.setText(str);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
Myprojects b = new Myprojects();
}
}
import java.awt.*;
import java.awt.event.*;
public class Myprojects {
JComboBox combo;
JTextField txt;
public Myprojects() {
String course[] = {"COmputer Science", "Software Engineering", "Computer Systems", "Networking"};
JFrame frame = new JFrame("Creating a JComboBox Component");
JPanel panel = new JPanel();
combo = new JComboBox(course);
combo.setBackground(Color.green);
combo.setForeground(Color.white);
txt = new JTextField(30);
panel.add(combo);
panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
String str = (String) combo.getSelectedItem();
txt.setText(str);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
Myprojects b = new Myprojects();
}
}
The output of the above code is as below
0 comments:
Post a Comment