Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 874
  • Last Modified:

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) {
                             //????????????
                    }  
                });
                     
        }
0
gla
Asked:
gla
  • 7
  • 2
2 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now