Skip to main content

How to Add ActionListener to JComboBox

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
vishaldb28
Offline
Joined: 2012-01-24

Hi

I am writing one simple program for upgrading my knowlegde from C# to Java but I am failed to attach ActionListener to JComboBox which is added

to a cell of JTable at run time. Actually I want to perform update operation and delete operation on selecting Update or Delete item from JComboBox

which is generated at runtime im a JTable.

I am able to add JComboBox to the cell of JTable and also getting the seleted value from JComboBox on click event of JTable but, my requirement is not fullfilled by this.

Here is my partial code -

JComboBox comboBox;
public manage() {
initComponents();
try
{
Class .forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","ROOT");
//jDateChooser1.setDate(Calendar.getInstance().getTime());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
String[] confirm = { "Update", "Delete"};
comboBox = new JComboBox(confirm);}
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
final Object[] columnNames=new String[] {"Movie Name","Start Date","End Date","Show 1","Show 2","Show 3","Show 4","Operation"};
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
DefaultTableModel dtm=new DefaultTableModel(columnNames,0);
Calendar c=Calendar.getInstance();

try
{
String sql="SELECT * FROM manage;";
smt1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = smt1.executeQuery(sql);
int i = 0;
while(rs.next())
{
String var1=rs.getString(1);
String var2=rs.getString(2);
String var3=rs.getString(3);
String var4=rs.getString(4);
String var5=rs.getString(5);
String var6=rs.getString(6);
String var7=rs.getString(7);

dtm.addRow(new Vector());
dtm.setValueAt(var1, i, 0);
dtm.setValueAt(var2, i, 1);
dtm.setValueAt(var3, i, 2);
dtm.setValueAt(var4, i, 3);
dtm.setValueAt(var5, i, 4);
dtm.setValueAt(var6, i, 5);
dtm.setValueAt(var7, i, 6);

i++;
}
jTable1.setModel(dtm);
jTable1.getColumnModel().getColumn(7).setCellEditor(new DefaultCellEditor(comboBox));
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}

After this code I added JTable click event to get the data from table cell by using Object and converted them to string. I am getting the value of Jcombobox but I want to implement operations on click event of JComboBox.

I hope I will get some positive response fro this forum.
Thanx in Advance and any code is most appriciated.

Regards