Table of Contents
hide
Example : How to add values to a combo box statically using the Property window.
Select the Combo Box-'Properties Window'-choose 'Model' and click on browse button(...)-Enter the values - ok.
Example : How to add values to a combo box dynamically using code.
public UserRegistrion()
{
initComponents(); //java's load event
jComboBox1.addItem("Select Anyone Item");
jComboBox1.addItem("Item1");
jComboBox1.addItem("Item2");
jComboBox1.addItem("Item3");
jComboBox1.addItem("Item4");
}
Example : How to open a combo box list data automatically when gaining focus on it.
private void jComboBox1FocusGained(java.awt.event.FocusEvent evt)
{
// TODO add your handling code here:
jComboBox1.showPopup();
}
Example : How to remove/clear/erase Combo box data.
UrfNat1.removeAllItems();
Example : How to load and add data into a combo box automatically from Database when page load occurs.
try
{
String sql = "SELECT NAT5 FROM REGISTRATION";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
UrfNat1.removeAllItems();
UrfNat1.addItem("Select Nationality");
while(rs.next())
{
UrfNat1.addItem(rs.getString("NAT5"));
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e);
}
Example : How to Get and Set data to/from a combo box.
String UrfNat3= UrfNat1.getSelectedItem().toString();
UrfNat1.setSelectedItem(rs.getString("NAT5"));
Example : How to Get data from an Array and add it into a combo box.
String nation[] = {
"Select Nationality",
"Indian",
"American",
"British",
"Australian",
"Canadian"
};
for(String n : nation)
{
UrfNat1.addItem(n);
}
![]()
0 Comments