954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to add ActionListener to JComboBox

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

vishalonne
Junior Poster in Training
70 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

have you looked at the oracle tutorials ?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

even oracle tutorial talking about ActionListener, better would be look for ItemListener

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: