I am new in working with Swing. I have a requirement where I need to get data from Database and pop up in Combo box, when the combobox listener performs, it needs to load the values. I am not sure where I went wrong the values in the below code is not displaying. Can any one please correct me if I am wrong. Code:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBoxSelectionChange extends JFrame
{
public ComboBoxSelectionChange ()
{
initialize();
}
private void initialize ()
{
setSize(300, 300);
setLayout(new FlowLayout(FlowLayout.LEFT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JComboBox comboBox = new JComboBox();
comboBox.setEditable(true);
final JTextArea textArea = new JTextArea(5, 15);
textArea.setBorder(new BevelBorder(BevelBorder.LOWERED));
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged (ItemEvent event)
{
String[] items = { "A", "B", "C", "D", "E", "F" };
final DefaultComboBoxModel model = new DefaultComboBoxModel(
items);
comboBox.setModel(model);
}
});
getContentPane().add(comboBox);
getContentPane().add(textArea);
}
public static void main (String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run ()
{
new ComboBoxSelectionChange().setVisible(true);
}
});
}
}
Try adding a popupmenu listener to JComboBox like :
comboBox.addPopupMenuListener( PopupMenuListener listener )
{
protected void popupMenuWillBecomeVisible( PopupMenuEvent event )
{
//update your model
}
}