JComboBox Insert Edited Value Into Table

In this section we will read about how to make JComboBox an editable and then how to insert the new edited value into the table.

JComboBox Insert Edited Value Into Table

In this section we will read about how to make JComboBox an editable and then how to insert the new edited value into the table.

JComboBox Insert Edited Value Into Table

JComboBox Insert Edited Value Into Table

In this section we will read about how to make JComboBox an editable and then how to insert the new edited value into the table.

This section will describe you all the steps involved in to insert an editable value of JComboBox into a table. We will use an Java editor to write a Java program and then we will compile and execute this Java program through command prompt. To insert a value into the table we will use MS-Access.

Example

As we have been already discussed what we are going to do. In this example we will create a frame and then we will add the components such as combo box, button, labels and other components (if needed). To create a frame and other components we will use classes of javax.swing package and to add events on the components we will use classes of java.awt.event package. In this example we will first create a database table (How To Create Table In MS-Access) and then we will create a class in Java inside which we will create a frame using JFrame, button using JButton, label using JLabel and then we will add an action event on button clicking after which the selected item of combo box will be inserted to the table and the combo box will made editable and then if you changed the value and click on the button then this value will also be inserted into the table.

PetTable.mdb

EditableComboBox.java

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

Output :

When you will compile and execute the above EditableComboBox.java class using command prompt as below you will get the output as follows :

When you will compile the Java file it may happens that the warning message will be displayed, this is because since, JDK 7 the JComboBox requires generics to describe the data which it holds.

When you will choose the items from combo box and click on insert button then the output will be as follows :

After clicking on "OK" button of dialog box combo box will be made editable as below :

And the data will be inserted into table then the data of table will be as follows :

And when you will edit the combo box and click on insert button then the output will be as follows :

And the table will become as follows :

Download Source Code