
i have one JCombobox with 2 values"y", "N".when i selecting some value from dropdown and cliuck on some save button . i am able to save it. but when i try to cahnge the value and when i clicked on save unable to save it. how to maKE Jcombox editable after saveing value init.

hi friend,
Try the following code may, this will be helpful for you.
package net.roseindia.swingExample;
import java.awt.event.*;
import java.sql.*;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.*;
public class EditableComboBox implements ActionListener{
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
String petName ="";
JComboBox petList;
JButton button;
private void createUI()
{
JFrame frame = new JFrame("Editable Combo Box");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JLabel label = new JLabel("Select from list : ");
button = new JButton("Insert");
button.addActionListener(this);
label.setBounds(10, 30, 100, 20);
button.setBounds(275, 30, 100, 20);
petList = new JComboBox(petStrings);
petList.setBounds(110, 30, 150, 30);
petList.setSelectedIndex(0);
frame.add(button);
frame.add(label);
frame.add(petList);
frame.setVisible(true);
frame.setSize(400, 300);
}
@Override
public void actionPerformed(ActionEvent e){
HashSet set = new HashSet();
if(e.getSource() == button)
{
String[] newList = new String[petStrings.length+1];
petName = (String)petList.getSelectedItem();
int i = 0;
while(i < petStrings.length)
{
newList[i]= petStrings[i];
set.add(newList[i]);
i++;
}
newList[i] = petName;
set.add(newList[i]);
Iterator itr = set.iterator();
while(itr.hasNext())
{
String s = (String) itr.next();
if(petName.equals(s))
{
addOperation();
}
}
petList.setEditable(true);
}
}
private void addOperation()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:swing");
String sql = "INSERT INTO PetTable(petName) Values('"+petName+"')";
Statement st = con.createStatement();
st.execute(sql);
JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added",
JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String args[])
{
EditableComboBox gvb = new EditableComboBox();
gvb.createUI();
}
}// class closed
Thanks.