Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Creating JComboBox Components

public JComboBox() JComboBox comboBox = new JComboBox(); public JComboBox(Object listData[]) String labels[] = { "A", "B", "C", "D", "E"}; JComboBox comboBox = new JComboBox(labels); public JComboBox(Vector listData) Vector vector = aBufferedImage.getSources(); JComboBox comboBox = new JComboBox(vector); public JComboBox(ComboBoxModel model) ResultSet results = aJDBCStatement.executeQuery("SELECT columnName FROM tableName"); DefaultComboBoxModel model = new DefaultComboBoxModel(); while (result.next())   model.addElement(results.getString(1)); JComboBox comboBox = new JComboBox(model); import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JComboBox; import javax.swing.JFrame; public class Main extends JFrame {   JComboBox combo = new JComboBox();   public Main() {     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     combo.addItem("A");     combo.addItem("H");     combo.addItem("P");     combo.setEditable(true);     System.out.println("#items=" + combo.getItemCount());     combo.addActionListener(new ActionListener() {       public void actionPerformed(ActionEvent e) {         System.out.println("Selected index=" + combo.getSelectedIndex()             + " Selected item=" + combo.getSelectedItem());       }     });     getContentPane().add(combo);     pack();     setVisible(true);   }   public static void main(String arg[]) {     new Main();   } }