Re: Use of JComboBox class
A JComboBox class component is used to combine a button or editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request. If you make the combo box editable, then the combo box includes an editable field into which the user can type a value. A JComboBox class is originally taken from the 'javax.swing.JComponent' of java programming.
Re: Use of JComboBox class
Hi,
If you want to aware about the use of JComboBox class of java, then please go through the following example of JComboBox class:
Code:
import javax.swing.JComboBox;
import javax.swing.JTextArea;
public class EditComboBoxDM {
public static void main(String args[]) {
String labels1[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J" };
JFrame frame1 = new JFrame("Editable JComboBox");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame1.getContentPane();
final JComboBox comboBox1 = new JComboBox(labels);
comboBox1.setMaximumRowCount(5);
comboBox1.setEditable(true);
contentPane.add(comboBox1, BorderLayout.NORTH);
final JTextArea textArea1 = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea1);
contentPane.add(scrollPane, BorderLayout.CENTER);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
textArea1.append("Selected: " + comboBox1.getSelectedItem());
textArea1.append(", Position: " + comboBox1.getSelectedIndex());
textArea1.append(System.getProperty("line.separator"));
}
};
comboBox1.addActionListener(actionListener);
frame1.setSize(400, 200);
frame1.setVisible(true);
}
}
Re: Use of JComboBox class
Hi friend,
Please refer following methods of JComboBox class along with their different parameters:
- setSelectedIndex(int Index1)
- setRenderer(ListCellRenderer aRenderer1)
- setPrototypeDisplayValue(Object prototypeDisplayValue1)
- setPopupVisible(boolean bl1)
- setModel(ComboBoxModel aModel1)
- setMaximumRowCount(int cnt)
Re: Use of JComboBox class
Below I have given the example JComboBox class, please study it carefully:
Code:
import javax.swing.JComboBox;
public class JFontComboBox extends JFrame implements ActionListener {
JLabel fontLabel = new JLabel("The fox jumps over the lazy dog.");
private JComboBox fontComboBox01;
public FontComboBox() {
setTitle("ComboBoxTest");
setSize(300, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
fontComboBox01 = new JComboBox();
fontComboBox01.setEditable(true);
fontComboBox01.addItem("Serif");
fontComboBox01.addItem("DialogInput");
fontComboBox01.addActionListener(this);
JPanel p1 = new JPanel();
p1.add(fontComboBox01);
getContentPane().add(p1, "North");
getContentPane().add(fontLabel, "Center");
}
public void actionPerformed(ActionEvent evt) {
JComboBox source = (JComboBox) evt.getSource();
String item = (String) source.getSelectedItem();
fontLabel.setFont(new Font(item, Font.PLAIN, 12));
}
public static void main(String[] args) {
JFrame frame1 = new JFontComboBox();
frame1.show();
}
}
Re: Use of JComboBox class
Hello friend,
If you want to know about the constructors of JComboBox class, then refer following:
- JComboBox(Vector itm) : It is used to for the creation of JComboBox with specified Vector.
- JComboBox(): It is used to for the creation of JComboBox with a default data model.
- JComboBox(Object[] itm): It is used to for the creation of JComboBox with specified arrar list.
- JComboBox(ComboBoxModel aMdl): It is used to for the creation of JComboBox with specified ComboBoxModel.