ActionEvent Listener in JCombobox array

Hi there,

I give you some of my java code. My problem is in action listener how to I understand who jCombobox jComboBoxparam[i] make the event.

any idea?

for (int i=0; i<rowCount; i++){
           
                jComboBoxparam[i] = new javax.swing.JComboBox();
               
       
                String [][] strObj_cb = new  String [1][1];
                SelectCount = "SELECT COUNT(*) FROM (" + strObj[i][3] + ")";
               
                strObj_cb = loadResults4Select(SelectCount, 1, 1);
                row_cb = Integer.parseInt(strObj_cb[0][0]);
                strObj_cb = loadResults4Select(strObj[i][3], col_cb, row_cb);
                for (int i_cb=0; i_cb<row_cb; i_cb++){
                    jComboBoxparam[i].addItem(strObj_cb[i_cb][0]);
                }

                jComboBoxparam[i].addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                             //????????????
                    }  
                });
                     
        }
glaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:
for (int i=0; i<rowCount; i++){
           
                jComboBoxparam[i] = new javax.swing.JComboBox();
               
       
                String [][] strObj_cb = new  String [1][1];
                SelectCount = "SELECT COUNT(*) FROM (" + strObj[i][3] + ")";
               
                strObj_cb = loadResults4Select(SelectCount, 1, 1);
                row_cb = Integer.parseInt(strObj_cb[0][0]);
                strObj_cb = loadResults4Select(strObj[i][3], col_cb, row_cb);
                for (int i_cb=0; i_cb<row_cb; i_cb++){
                    jComboBoxparam[i].addItem(strObj_cb[i_cb][0]);
                }

                jComboBoxparam[i].addActionListener(new MyActionListener ());

                      class MyActionListener extends java.awt.event.ActionListener {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                             if(evt.getSource()==jComboBoxparam[i])
                            {
                                     //do your job here
                            }
                    }  
                };
                     
        }
0
sudhakar_koundinyaCommented:
Basically your code should be somethinglike this below format

class YourFrameClass  extends JFrame
{

  public void initComponents()
   {
            for (int i=0; i<rowCount; i++){
           
                jComboBoxparam[i] = new javax.swing.JComboBox();
               
       
                String [][] strObj_cb = new  String [1][1];
                SelectCount = "SELECT COUNT(*) FROM (" + strObj[i][3] + ")";
               
                strObj_cb = loadResults4Select(SelectCount, 1, 1);
                row_cb = Integer.parseInt(strObj_cb[0][0]);
                strObj_cb = loadResults4Select(strObj[i][3], col_cb, row_cb);
                for (int i_cb=0; i_cb<row_cb; i_cb++){
                    jComboBoxparam[i].addItem(strObj_cb[i_cb][0]);
                }

                jComboBoxparam[i].addActionListener(new MyActionListener ());

                   
                     
        }
   }
   class MyActionListener extends java.awt.event.ActionListener {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                             if(evt.getSource()==jComboBoxparam[i])
                            {
                                     //do your job here
                            }
                    }  
                }

}
0
sudhakar_koundinyaCommented:
This is opther model

class YourFrameClass
    extends JFrame {
  MyActionListener listener = new MyActionListener();
 
  public void initComponents(int rowCount) {

    for (int i = 0; i < rowCount; i++) {

      jComboBoxparam[i] = new javax.swing.JComboBox();

      String[][] strObj_cb = new String[1][1];
      SelectCount = "SELECT COUNT(*) FROM (" + strObj[i][3] + ")";

      strObj_cb = loadResults4Select(SelectCount, 1, 1);
      row_cb = Integer.parseInt(strObj_cb[0][0]);
      strObj_cb = loadResults4Select(strObj[i][3], col_cb, row_cb);
      for (int i_cb = 0; i_cb < row_cb; i_cb++) {
        jComboBoxparam[i].addItem(strObj_cb[i_cb][0]);
      }

      jComboBoxparam[i].addActionListener(new MyActionListener());
      listener.bind(jComboBoxparam[i]);

    }
  }

}

class MyActionListener
    extends java.awt.event.ActionListener {
  static Vector components = new Vector();
  public void bind(Component c) {
    c.addActionListener(this);
    components.add(c);
  }

  public void actionPerformed(java.awt.event.ActionEvent evt) {
    for (int i = 0; i < components.size(); i++) {
      if (evt.getSource() == components.get(i)) {
        //do your job here
        return;
      }
    }
  }
}
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

sudhakar_koundinyaCommented:
for (int i = 0; i < rowCount; i++) {

      jComboBoxparam[i] = new javax.swing.JComboBox();

      String[][] strObj_cb = new String[1][1];
      SelectCount = "SELECT COUNT(*) FROM (" + strObj[i][3] + ")";

      strObj_cb = loadResults4Select(SelectCount, 1, 1);
      row_cb = Integer.parseInt(strObj_cb[0][0]);
      strObj_cb = loadResults4Select(strObj[i][3], col_cb, row_cb);
      for (int i_cb = 0; i_cb < row_cb; i_cb++) {
        jComboBoxparam[i].addItem(strObj_cb[i_cb][0]);
      }

      jComboBoxparam[i].addActionListener(new MyActionListener());
      listener.bind(jComboBoxparam[i]);






class MyActionListener
    implements java.awt.event.ActionListener {
  static Vector components = new Vector();
  public void bind(JComponent c) {
    if(c instanceof JComboBox)
    {
      ((JComboBox)c).addActionListener(this);
    }
          components.add(c);
  }

  public void actionPerformed(java.awt.event.ActionEvent evt) {
    for (int i = 0; i < components.size(); i++) {
      if (evt.getSource() == components.get(i)) {
        //do your job here
        return;
      }
    }
  }
}
0
glaAuthor Commented:
I need java code that return the index of Jcombobox array that make the event.

For example  jComboBoxparam.getIndex if somethinh like that exists....
                           
0
sudhakar_koundinyaCommented:
OK

Ignore all previous

try this model

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class YourFrameClass
    extends JFrame {
  MyActionListener listener = new MyActionListener();

  public void initComponents(int rowCount, JComboBox jComboBoxparam[]) {
    for (int i = 0; i < rowCount; i++) {
      jComboBoxparam[i] = new javax.swing.JComboBox();
      listener.bind(jComboBoxparam[i]);

    }
  }

}

class ComboActionListener
    implements java.awt.event.ActionListener {
  static Vector components = new Vector();
  public void bind(JComboBox c) {

    c.addActionListener(this);
    components.add(c);
  }

  public void actionPerformed(java.awt.event.ActionEvent evt) {

    int index = -1;

    if (components.contains(evt.getSource())) {
      index = components.indexOf(evt.getSource());
      //do your job here

      return;

    }
  }
}
0
objectsCommented:
               jComboBoxparam[i].addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        int index = -1;
                        for (int j=0; index==-1 && j<jComboBoxparam.length; j++) {
                             if (evt.getSource()==jComboBoxparam[j]) index = j;
                        }
                        JComboBox combo = jComboBoxparam[index];
                        // process event
                    }  
                });
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudhakar_koundinyaCommented:
OK

Ignore all previous

try this model


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class YourFrameClass
    extends JFrame {
  ComboActionListener listener = new ComboActionListener();

  public void initComponents(int rowCount, JComboBox jComboBoxparam[]) {
    for (int i = 0; i < rowCount; i++) {
      jComboBoxparam[i] = new javax.swing.JComboBox();
      listener.bind(jComboBoxparam[i]);

    }
  }

}

class ComboActionListener
    implements java.awt.event.ActionListener {
  static Vector components = new Vector();
  public void bind(JComboBox c) {

    c.addActionListener(this);
    components.add(c);
  }

  public void actionPerformed(java.awt.event.ActionEvent evt) {

    int index = -1;

    if (components.contains(evt.getSource())) {
      index = components.indexOf(evt.getSource());
      //do your job here

      return;

    }
  }
}
0
sudhakar_koundinyaCommented:
:)
0
objectsCommented:
(:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.