'action performed' on array of buttons.

Hi everyone!
I am trying to set up an array of buttons on a panel in the main frame of my application. I can get them on to the panel, displaying them correctly but i can;t seem to attach an action performed method to them. Ive attached the following code - if you have any ideas on how to solve this problem i would be very grateful. DO you think it is because they are on this panel and not directly on the main Frame.?
If i try to attach say Button1_action performed then it clashes with my original button 1!
Thanks in advance.....
static int N=10;
String lett[]={"a","b","c","d","e","f","g","h","i","j"};
Button button[]=new Button[N];
for(int k=0;k<N;++k)
{button[k]=new Button();
button[k].setLabel(lett[k]);
button[k].setBackground(Color.darkGray);
button[k].addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button[k]_actionPerformed(e);
this.panel1.add(button[k]);

A: 'action performed' on array of buttons.

viravan is right - your code should work except that button[k]_actionPerformed won't compile. Also, you don't need a separate ActionListener for each button - use a common one and test for the source.
e.g.
import java.awt.*;
import java.awt.event.*;
...code to define Frame, Panel etc...
static int N=10;
String lett[]={"a","b","c","d","e","f","g","h","i","j"};
Button button[]=new Button[N];
ActionListener common = (new ActionListener() {
     public void actionPerformed(ActionEvent e) {
            Button source = (Button)e.getSource();
             System.out.println("Label of button pressed = " + source.getLabel() );
for(int k=0;k<N;++k) {
    button[k]=new Button();
    button[k].setLabel(lett[k]);
    button[k].setBackground(Color.darkGray);
    button[k].addActionListener(common);
    panel1.add(button[k]);
}Another alternative is to addActionListener(this) and code the actionPerformed method as part of the Frame class.

viravan is right - your code should work except that button[k]_actionPerformed won't compile. Also, you don't need a separate ActionListener for each button - use a common one and test for the source.
e.g.
import java.awt.*;
import java.awt.event.*;
...code to define Frame, Panel etc...
static int N=10;
String lett[]={"a","b","c","d","e","f","g","h","i","j"};
Button button[]=new Button[N];
ActionListener common = (new ActionListener() {
     public void actionPerformed(ActionEvent e) {
            Button source = (Button)e.getSource();
             System.out.println("Label of button pressed = " + source.getLabel() );
for(int k=0;k<N;++k) {
    button[k]=new Button();
    button[k].setLabel(lett[k]);
    button[k].setBackground(Color.darkGray);
    button[k].addActionListener(common);
    panel1.add(button[k]);
}Another alternative is to addActionListener(this) and code the actionPerformed method as part of the Frame class.

Similar Messages

  • Mechanical Action of an array of buttons

    I have an array of buttons that get dynamically built when the front panel is loaded. When I select one of the buttons it remains "pressed in". If I right click on the button the "Mechanical Action" is greyed out. I pulled the button out of the array and checked the settings and its set to latch when released, its just not happening. Is their any way to set this property?

    Hi Rob,
    enclosed you will find two other solutions, simulating a radio button array based on a standard button array,
    this solution use a event loop.
    Version 1: Simulates Radio Buttons without "Allow False" Option
    Version 2: Simulates Radio Buttons with "Allow False" Option
    Attachments:
    ButtonArray1.vi ‏10 KB
    ButtonArray2.vi ‏10 KB

  • Pass a variable number into an Action method from an array of buttons

    Hello,
    I have created an array of buttons like so:
                       private javax.swing.JButton[] barray = new JButton[8];
    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(K8055_Card_Control.JavaElectrodeSwitches1App.class).getContext().getActionMap(JavaElectrodeSwitches1View.class, this);
                 for (int i=0;i<8;i++)    // Create Output Check Boxes
              barray[i] = new JButton();
                            barray.setAction(actionMap.get("ButtonPressed"));
              barray[i].setName("Outbox"+ String.valueOf(i+1));
              barray[i].setText("Electrode " + String.valueOf(i+1));
    BoxLayout layout = new BoxLayout(jPanel1,BoxLayout.PAGE_AXIS);
    barray[i].setAlignmentX(JCheckBox.CENTER_ALIGNMENT) ;
    jPanel1.setLayout(layout);
    jPanel1.add(barray[i]);
    When a button is pressed it calls the method ButtonPressed()@Action
    public void ButtonPressed()
    // button pressed code here
    My problem is that once the button has been pressed I don't know how to find out which button called the method. Each button operates a different switch.
    I tried testing barray.isSelected(); however the ButtonPressed() method is only called once the button has released and is no longer selected.
    Any ideas?
    Thanks,

    Actually the answer to both of these questions is ActionEvent
    do for instance:
    JButton b = new JButton("test");
    JButon b2 = new JButton("test2");
    ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
      Object src = ae.getSource();
      String cmd = ae.getActionCommand();
      if (src == b) { //won't work unless you make b final
      //or
      if ("test".equals(cmd)) {
    b.addActionListener(al);
    b2.addActionListener(al);

  • Help needed with Button action performed

    Hi,
    I am new to AWT prgramming .
    I have created a menu called " Menu System Test window " and it contains toplevel menu items of "File" and "Help" and File Menu contains subitems "Product Evaluation" and "Exit".
    When i click File->Product Evaluation it displays a new Frame named "ProductEvaluationMeasurementTool" with some check boxes ,radiobuttons, buttons and text fields. So when i select some checkboxes in that new frame and click the button labeled "Metricslevel" it displays some resultin textField1 , similarly when i click Button labeled "Measurementlevel" it displays some result in textField2.
    My question is i have another button in Frame Called "Reset". When i click the Reset button all the check boxes which have been checked previously should be unchecked and moreover the result in both the textfield should be cleared.
    I am sending my code. Kindly help me.
    Thanks in advance.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Component;
    import java.awt.Checkbox;
    import javax.swing.*;
    // Make a main window with two top-level menus: File and Help.
    // Help has a submenu and demonstrates a few interesting menu items.
    public class MainWindow extends Frame {
      public MainWindow() {
        super("Menu System Test Window");
        setSize(500, 500);
        // make a top level File menu
        FileMenu fileMenu = new FileMenu(this);
        // make a top level Help menu
        HelpMenu helpMenu = new HelpMenu(this);
        // make a menu bar for this frame 
        // and add top level menus File and Menu
        MenuBar mb = new MenuBar();
        mb.add(fileMenu);
        mb.add(helpMenu);
        setMenuBar(mb);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            exit();
      public void exit() {
        setVisible(false); // hide the Frame
        dispose(); // tell windowing system to free resources
        System.exit(0); // exit
      public static void main(String args[]) {
        MainWindow w = new MainWindow();
        w.setVisible(true);
    // Encapsulate the look and behavior of the File menu
    class FileMenu extends Menu implements ActionListener {
      MainWindow mw;  // who owns us?
      public FileMenu(MainWindow m) {
        super("File");
        mw = m;
        MenuItem mi;
        add(mi = new MenuItem("ProductEvaluation"));
        mi.addActionListener(this);
        add(mi = new MenuItem("Exit"));
        mi.addActionListener(this);
      // respond to the Exit menu choice
      public void actionPerformed(ActionEvent e) { String item = e.getActionCommand();
        if (item.equals("ProductEvaluation"))
         Frame f = new Frame("ProductMeasurementEvaluationTool");
         f.setSize(1290,1290);
         f.setLayout(null);
         TextField t1 = new TextField("textField1");
         t1.setBounds(230, 630, 50, 24);
         f.add(t1);
         TextField t2 = new TextField("textField2");
         t2.setBounds(430, 630, 50, 24);
         f.add(t2);
         ActionListener al = new MyActionListener(f, t1);
         ActionListener a2 = new MyActionListener(f, t2);
         Label l1 = new Label("Select the appropriate metrics for Measurement Process Evaluation");
         l1.setBounds(380, 50, 380, 20);
         f.add(l1);
         Label l2 = new Label("Architecture Metrics");
         l2.setBounds(170, 100, 110, 20);
         f.add(l2);
         Label l3 = new Label("RunTime Metrics");
         l3.setBounds(500, 100, 110, 20);
         f.add(l3);
         Label l4 = new Label("Documentation Metrics");
         l4.setBounds(840, 100, 130, 20);
         f.add(l4);
         JRadioButton rb1 = new JRadioButton("Componenent Metrics",false);
         rb1.setBounds(190, 140, 133, 20);
         f.add(rb1);
         JRadioButton rb2 = new JRadioButton("Task Metrics",false);
         rb2.setBounds(540, 140, 95, 20);
         f.add(rb2);
         JRadioButton rb3 = new JRadioButton("Manual Metrics",false);
         rb3.setBounds(870, 140, 108, 20);
         f.add(rb3);
         JRadioButton rb4 = new JRadioButton("Configuration Metrics",false);
         rb4.setBounds(190, 270, 142, 20);
         f.add(rb4);
         JRadioButton rb5 = new JRadioButton("DataBase Metrics",false);
         rb5.setBounds(190, 420, 122, 20);
         f.add(rb5);
         JRadioButton rb6 = new JRadioButton("DataHandling Metrics",false);
         rb6.setBounds(540, 270, 142, 20);
         f.add(rb6);
         JRadioButton rb7= new JRadioButton("HumanInterface Metrics",false);
         rb7.setBounds(540, 420, 156, 20);
         f.add(rb7);
         JRadioButton rb8 = new JRadioButton("Development Metrics",false);
         rb8.setBounds(870, 270, 141, 20);
         f.add(rb8);
         JRadioButton rb9= new JRadioButton("Marketing Metrics",false);
         rb9.setBounds(870, 420, 121, 20);
         f.add(rb9);
         Checkbox  c10 = new Checkbox("Size");
         c10.setBounds(220, 170, 49, 20);
         f.add(c10);
         Checkbox c11 = new Checkbox("Structure");
         c11.setBounds(220, 190, 75, 20);
         f.add(c11);
         Checkbox c12 = new Checkbox("Complexity");
         c12.setBounds(220, 210, 86, 20);
         f.add(c12);
         Checkbox c13 = new Checkbox("Size");
         c13.setBounds(220, 300, 49, 20);
         f.add(c13);
         Checkbox c14 = new Checkbox("Structure");
         c14.setBounds(220, 320, 75, 20);
         f.add(c14);
         Checkbox c15 = new Checkbox("Complexity");
         c15.setBounds(220, 340, 86, 20);
         f.add(c15);
         Checkbox c16 = new Checkbox("Size");
         c16.setBounds(220, 460, 49, 20);
         f.add(c16);
         Checkbox c17 = new Checkbox("Structure");
         c17.setBounds(220, 480, 75, 20);
         f.add(c17);
         Checkbox c18 = new Checkbox("Complexity");
         c18.setBounds(220, 500, 86, 20);
         f.add(c18);
         Checkbox c19 = new Checkbox("Size");
         c19.setBounds(580, 170, 49, 20);
         f.add(c19);
         Checkbox c20 = new Checkbox("Structure");
         c20.setBounds(580, 190, 75, 20);
         f.add(c20);
         Checkbox c21 = new Checkbox("Complexity");
         c21.setBounds(580, 210, 86, 20);
         f.add(c21);
         Checkbox c22 = new Checkbox("Size");
         c22.setBounds(580, 300, 49, 20);
         f.add(c22);
         Checkbox c23 = new Checkbox("Structure");
         c23.setBounds(580, 320, 75, 20);
         f.add(c23);
         Checkbox c24 = new Checkbox("Complexity");
         c24.setBounds(580, 340, 86, 20);
         f.add(c24);
         Checkbox c25 = new Checkbox("Size");
         c25.setBounds(590, 460, 49, 20);
         f.add(c25);
         Checkbox c26 = new Checkbox("Structure");
         c26.setBounds(590, 480, 75, 20);
         f.add(c26);
         Checkbox c27 = new Checkbox("Complexity");
         c27.setBounds(590, 500, 86, 20);
         f.add(c27);
         Checkbox c28 = new Checkbox("Size");
         c28.setBounds(920, 170, 49, 20);
         f.add(c28);
         Checkbox c29 = new Checkbox("Structure");
         c29.setBounds(920, 190, 75, 20);
         f.add(c29);
         Checkbox c30 = new Checkbox("Complexity");
         c30.setBounds(920, 210, 86, 20);
         f.add(c30);
         Checkbox c31 = new Checkbox("Size");
         c31.setBounds(920, 300, 49, 20);
         f.add(c31);
         Checkbox c32 = new Checkbox("Structure");
         c32.setBounds(920, 320, 75, 20);
         f.add(c32);
         Checkbox c33 = new Checkbox("Complexity");
         c33.setBounds(920, 340, 86, 20);
         f.add(c33);
         Checkbox c34 = new Checkbox("Size");
         c34.setBounds(930, 450, 49, 20);
         f.add(c34);
         Checkbox c35 = new Checkbox("Structure");
         c35.setBounds(930, 470, 75, 20);
         f.add(c35);
         Checkbox c36 = new Checkbox("Complexity");
         c36.setBounds(930, 490, 86, 20);
         f.add(c36);
         Button b1  = new Button("MetricsLevel");
         b1.setBounds(230, 600, 120, 24);
         f.add(b1);
         b1.addActionListener(al);
         Button b2  = new Button("MeasurementLevel");
         b2.setBounds(430, 600, 120, 24);
         f.add(b2);
         b2.addActionListener(a2);
         Button b3  = new Button("Reset");
         b3.setBounds(630, 600, 120, 24);
         f.add(b3);
         f.show();
        else
       { mw.exit();}
    class MyActionListener implements ActionListener
        Frame f;
        TextField textField1;
        TextField textField2;
        public MyActionListener(Frame f, TextField tf)
            this.f = f;
            textField1 = tf;
            textField2 = tf;
        public void actionPerformed(ActionEvent e)
           String s = e.getActionCommand();
        if (s.equals("MetricsLevel"))
            Component[] components = f.getComponents();
      int numOfCheckBoxes = 81;
      int numChecked = 0;
      for ( int i = 0; i < components.length; i++ )
       if ( components[i] instanceof Checkbox )
        Checkbox checkBox = (Checkbox) components;
    if ( checkBox.getState() )
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    textField1.setText( Double.toString( ratio ) );
    else
    if (s.equals("MeasurementLevel"))
    Component[] components = f.getComponents();
    int numOfCheckBoxes = 81;
    int numChecked = 0;
    for ( int i = 0; i < components.length; i++ )
    if ( components[i] instanceof Checkbox )
    Checkbox checkBox = (Checkbox) components[i];
    if ( checkBox.getState() )
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    textField2.setText( Double.toString( ratio ) );
    else
    if (s.equals("Reset"))
    Code for Reset Button action performed.
    // Encapsulate the look and behavior of the Help menu
    class HelpMenu extends Menu implements ActionListener {
    MainWindow mw; // who owns us?
    public HelpMenu(MainWindow m) {
    super("Help");
    mw = m;
    MenuItem mi;
    add(mi = new MenuItem("Description"));
    mi.addActionListener(this);
    // respond to a few menu items
    public void actionPerformed(ActionEvent e) {
    String item = e.getActionCommand();
    if (item.equals("Description"))
    System.out.println("You can get description at our website");

    import java.awt.*;
    import java.awt.event.*;
    // Make a main window with two top-level menus: File and Help.
    // Help has a submenu and demonstrates a few interesting menu items.
    public class MainWindow extends Frame
      public static void main(String args[])
        new MainWindow();
      public MainWindow()
        super("Menu System Test Window");
        setSize(500, 500);
        // Why not make 1 menubar class that you can add with
        // this.setMenuBar(new MyMenuBar(this)); ?
        // make a top level File menu
        FileMenu fileMenu = new FileMenu(this);
        // make a top level Help menu
        HelpMenu helpMenu = new HelpMenu(this);
        // make a menu bar for this frame
        // and add top level menus File and Menu
        MenuBar mb = new MenuBar();
        mb.add(fileMenu);
        mb.add(helpMenu);
        this.setMenuBar(mb);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter()
          public void windowClosing(WindowEvent e)
            System.exit(0);
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JRadioButton;
    //Encapsulate the look and behavior of the File menu
    public class FileMenu extends Menu implements ActionListener
      private MainWindow mw; // who owns us?
      private MenuItem itmPE   = new MenuItem("ProductEvaluation");
      private MenuItem itmExit = new MenuItem("Exit");
      public FileMenu(MainWindow main)
        super("File");
        this.mw = main;
        this.itmPE.addActionListener(this);
        this.itmExit.addActionListener(this);
        this.add(this.itmPE);
        this.add(this.itmExit);
      // respond to the Exit menu choice
      public void actionPerformed(ActionEvent e)
        if (e.getSource() == this.itmPE)
          Frame f = new Frame("ProductMeasurementEvaluationTool");
          f.setSize(1290, 1290);
          f.setLayout(null);
          TextField t1 = new TextField("textField1");
          t1.setBounds(230, 630, 50, 24);
          f.add(t1);
          TextField t2 = new TextField("textField2");
          t2.setBounds(430, 630, 50, 24);
          f.add(t2);
          // Way to ugly..
          // ActionListener al = new MyActionListener(f, t1);
          // ActionListener a2 = new MyActionListener(f, t2);
          // see below...
          Label l1 = new Label("Select the appropriate metrics for Measurement Process Evaluation");
          l1.setBounds(380, 50, 380, 20);
          f.add(l1);
          Label l2 = new Label("Architecture Metrics");
          l2.setBounds(170, 100, 110, 20);
          f.add(l2);
          Label l3 = new Label("RunTime Metrics");
          l3.setBounds(500, 100, 110, 20);
          f.add(l3);
          Label l4 = new Label("Documentation Metrics");
          l4.setBounds(840, 100, 130, 20);
          f.add(l4);
          JRadioButton rb1 = new JRadioButton("Componenent Metrics", false);
          rb1.setBounds(190, 140, 133, 20);
          f.add(rb1);
          // Please do not use AWT and Swing components in the same frame.
          JRadioButton rb2 = new JRadioButton("Task Metrics", false);
          rb2.setBounds(540, 140, 95, 20);
          f.add(rb2);
          JRadioButton rb3 = new JRadioButton("Manual Metrics", false);
          rb3.setBounds(870, 140, 108, 20);
          f.add(rb3);
          JRadioButton rb4 = new JRadioButton("Configuration Metrics", false);
          rb4.setBounds(190, 270, 142, 20);
          f.add(rb4);
          JRadioButton rb5 = new JRadioButton("DataBase Metrics", false);
          rb5.setBounds(190, 420, 122, 20);
          f.add(rb5);
          JRadioButton rb6 = new JRadioButton("DataHandling Metrics", false);
          rb6.setBounds(540, 270, 142, 20);
          f.add(rb6);
          JRadioButton rb7 = new JRadioButton("HumanInterface Metrics", false);
          rb7.setBounds(540, 420, 156, 20);
          f.add(rb7);
          JRadioButton rb8 = new JRadioButton("Development Metrics", false);
          rb8.setBounds(870, 270, 141, 20);
          f.add(rb8);
          JRadioButton rb9 = new JRadioButton("Marketing Metrics", false);
          rb9.setBounds(870, 420, 121, 20);
          f.add(rb9);
          Checkbox c10 = new Checkbox("Size");
          c10.setBounds(220, 170, 49, 20);
          f.add(c10);
          Checkbox c11 = new Checkbox("Structure");
          c11.setBounds(220, 190, 75, 20);
          f.add(c11);
          Checkbox c12 = new Checkbox("Complexity");
          c12.setBounds(220, 210, 86, 20);
          f.add(c12);
          Checkbox c13 = new Checkbox("Size");
          c13.setBounds(220, 300, 49, 20);
          f.add(c13);
          Checkbox c14 = new Checkbox("Structure");
          c14.setBounds(220, 320, 75, 20);
          f.add(c14);
          Checkbox c15 = new Checkbox("Complexity");
          c15.setBounds(220, 340, 86, 20);
          f.add(c15);
          Checkbox c16 = new Checkbox("Size");
          c16.setBounds(220, 460, 49, 20);
          f.add(c16);
          Checkbox c17 = new Checkbox("Structure");
          c17.setBounds(220, 480, 75, 20);
          f.add(c17);
          Checkbox c18 = new Checkbox("Complexity");
          c18.setBounds(220, 500, 86, 20);
          f.add(c18);
          Checkbox c19 = new Checkbox("Size");
          c19.setBounds(580, 170, 49, 20);
          f.add(c19);
          Checkbox c20 = new Checkbox("Structure");
          c20.setBounds(580, 190, 75, 20);
          f.add(c20);
          Checkbox c21 = new Checkbox("Complexity");
          c21.setBounds(580, 210, 86, 20);
          f.add(c21);
          Checkbox c22 = new Checkbox("Size");
          c22.setBounds(580, 300, 49, 20);
          f.add(c22);
          Checkbox c23 = new Checkbox("Structure");
          c23.setBounds(580, 320, 75, 20);
          f.add(c23);
          Checkbox c24 = new Checkbox("Complexity");
          c24.setBounds(580, 340, 86, 20);
          f.add(c24);
          Checkbox c25 = new Checkbox("Size");
          c25.setBounds(590, 460, 49, 20);
          f.add(c25);
          Checkbox c26 = new Checkbox("Structure");
          c26.setBounds(590, 480, 75, 20);
          f.add(c26);
          Checkbox c27 = new Checkbox("Complexity");
          c27.setBounds(590, 500, 86, 20);
          f.add(c27);
          Checkbox c28 = new Checkbox("Size");
          c28.setBounds(920, 170, 49, 20);
          f.add(c28);
          Checkbox c29 = new Checkbox("Structure");
          c29.setBounds(920, 190, 75, 20);
          f.add(c29);
          Checkbox c30 = new Checkbox("Complexity");
          c30.setBounds(920, 210, 86, 20);
          f.add(c30);
          Checkbox c31 = new Checkbox("Size");
          c31.setBounds(920, 300, 49, 20);
          f.add(c31);
          Checkbox c32 = new Checkbox("Structure");
          c32.setBounds(920, 320, 75, 20);
          f.add(c32);
          Checkbox c33 = new Checkbox("Complexity");
          c33.setBounds(920, 340, 86, 20);
          f.add(c33);
          Checkbox c34 = new Checkbox("Size");
          c34.setBounds(930, 450, 49, 20);
          f.add(c34);
          Checkbox c35 = new Checkbox("Structure");
          c35.setBounds(930, 470, 75, 20);
          f.add(c35);
          Checkbox c36 = new Checkbox("Complexity");
          c36.setBounds(930, 490, 86, 20);
          f.add(c36);
          ActionListener action = new MyActionListener(f, t1, t2);
          Button b1 = new Button("MetricsLevel");
          b1.setBounds(230, 600, 120, 24);
          b1.addActionListener(action);
          f.add(b1);
          Button b2 = new Button("MeasurementLevel");
          b2.setBounds(430, 600, 120, 24);
          b2.addActionListener(action);
          f.add(b2);
          Button b3 = new Button("Reset");
          b3.setBounds(630, 600, 120, 24);
          b3.addActionListener(action);
          f.add(b3);
          f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
              System.exit(0);
          f.show();
        else if (e.getSource() == this.itmExit)
          System.exit(0);
    import java.awt.*;
    import java.awt.event.*;
    //Encapsulate the look and behavior of the Help menu
    public class HelpMenu extends Menu implements ActionListener
      private MainWindow main; // who owns us?
      private MenuItem itmDescription = new MenuItem("Description");
      public HelpMenu(MainWindow main)
        super("Help");
        this.main = main;   
        this.itmDescription.addActionListener(this);
        this.add(this.itmDescription);
      // respond to a few menu items
      public void actionPerformed(ActionEvent e)
        if (e.getSource() == this.itmDescription)
          System.out.println("You can get description at our website");
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JRadioButton;
    public class MyActionListener implements ActionListener
      private Frame     frame;
      private TextField textField1;
      private TextField textField2;
      public MyActionListener(Frame frame, TextField tf1, TextField tf2)
        this.frame = frame;
        this.textField1 = tf1;
        this.textField2 = tf2;
      public void actionPerformed(ActionEvent e)
        String s = e.getActionCommand();
        if (s.equals("MetricsLevel"))
          Component[] components = this.frame.getComponents();
          int numOfCheckBoxes = 81;
          int numChecked = 0;
          for (int i = 0; i < components.length; i++)
            if (components[i] instanceof Checkbox)
              if (((Checkbox)components).getState())
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    this.textField1.setText(Double.toString(ratio));
    else if (s.equals("MeasurementLevel"))
    Component[] components = frame.getComponents();
    int numOfCheckBoxes = 81;
    int numChecked = 0;
    for (int i = 0; i < components.length; i++)
    if (components[i] instanceof Checkbox)
    if (((Checkbox)components[i]).getState())
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    this.textField2.setText(Double.toString(ratio));
    else if (s.equals("Reset"))
    this.textField1.setText("");
    this.textField2.setText("");
    for (int i = 0; i < this.frame.getComponentCount(); i++)
    Component c = this.frame.getComponent(i);
    if (c instanceof JRadioButton)
    ((JRadioButton)c).setSelected(false);
    else if (c instanceof Checkbox)
    ((Checkbox)c).setState(false);

  • 9x9 array of buttons and not a single button worked

    im trying to build a 9 x 9 block composed of buttons. I used array of buttons.
    here's my code with my explanation and poor logic thinking details.
    public class ButtonArrayDemo extends JFrame implements ActionListener
    private JButton[] sButton;
    private int[] x;//manipulation purposes only
    public ButtonArrayDemo()
         JPanel buttonPanel = new JPanel();
         buttonPanel.setLayout( new GridLayout(9, 9) );
         sButton = new JButton[81];
         x = new int[81];
    // creates the buttons,action listeners, syntax is correct but
    // i doubt the logic
         for (int i = 0; i < sButton.length ; i++)
              JButton button = new JButton();
              button.setText(" ");
              button.addActionListener(this);
              sButton[i] = button;
              buttonPanel.add(sButton);
              x[i] = 0;
         getContentPane().add(buttonPanel,BorderLayout.SOUTH);
         setResizable(false);
    // what i want for this is that, when a button is clicked, it displays the
    //number 1 then increments by 1 if clicked again. Like,when i click a
    //blank button, it displays the number "1", if i clicked it again, it displays
    //the number 2. This goes on until number 9. When the button is clicked
    //on the 10th time, it leaves the button's text blank. Syntax is fine but
    //i doubt the logic.
    public void actionPerformed(ActionEvent e)
         int y;
         for(y = 0; y < sButton.length; y++)
              if(e.getActionCommand().equals(sButton[y]))
                   sButton[y].setText(Integer.toString(++x[y]));
                   if(x[y] == 10)
                   sButton[y].setText(" ");
                   x[y] = 0;
    Now, i created the main and compiled the program. The frames appeared complete with 81 buttons. But, when I click on any button,
    it does nothing. The button's text which is supposedly to change is not displaying anything. this is a vital thinking process for me and any help from someone would be very much appreciated.
    thanks for reading and i hope someone will respond to my problem.
    lots of thanks.

    I believe the problem is that you are using the actionCommand but you did not set the actioncommand for the buttons. I suggest you either set the action command for each button to a unique item or use getSource instead.
    Also, you might want to break out of your for loop once you find the correct button.
    Also, if the future use the code formatting tags and this probably should have been in the Swing forum.

  • Array of buttons with different boolean text ?

    I'm looking for a method of displaying a 2D button matrix with
    different boolean text on each button. It seems an array of booleans
    must all have the same text. Perhaps a cluster could be used, but the
    array makes button alignment easy and easy to determine which had been
    pressed (radiobutton style with only one button true at a time).
    Steve

    On 6 Dec 2002 05:31:21 -0800, [email protected] (humenik) wrote:
    >[email protected] (Steve Parus) wrote in message news:<[email protected]>...
    >> I'm looking for a method of displaying a 2D button matrix with
    >> different boolean text on each button. It seems an array of booleans
    >> must all have the same text. Perhaps a cluster could be used, but the
    >> array makes button alignment easy and easy to determine which had been
    >> pressed (radiobutton style with only one button true at a time).
    >It's easy to make them individuals in a cluster, and on the block
    >diagram use Cluster To Array to get it into array form for
    >manipulation. This can be superior to an array of buttons, not merely
    >because the buttons can thereby have differing colors and text. An
    >array of buttons cannot have the buttons Latched When Pressed; when in
    >a cluster, however, the latching action is available.
    When might that Latched When Pressed behavior be useful ? Using a 2D
    array of buttons (not cluster), in LV 6.1, a Value Changed event for
    the array can be used as you mention below to XOR the event's OldValue
    and NewValue and wire that to a local variable copy of the array.
    This easily gives a radio button effect for the entire 2D button
    matrix. The array control automatcially arranges the visual
    alignment of the buttons.
    For the cluster method, I'd like to programmatically change which
    button is true. All the approaches I come up with end up using
    Array-to-Cluster which then requires the cluster size to be specified.
    I may end up wanting to programmatically vary the number of buttons in
    both dimensions and hence the cluster size. LV's Align Objects and
    Distribue Objects ends up making the button visual alignment not
    difficult (for a 12 x 8 matrix).
    >Try this: take a cluster of four PB's (latched when pressed) put
    >inside a while loop. Initialize a boolean array of 4 to FALSE outside
    >the while loop, and attach it to a shift register input on the left
    >side of loop. Inside the loop, convert your cluster to an array. XOR
    >the shift register with this array from the buttons, and wire this to
    >the output shift register and to an indicator array. Set the boolean
    >to the loop so it will run free and start it up; on the control panel,
    >click on the PB's and you will see the outputs toggle.
    >
    >Greg

  • How to use same actions for differ pop-up buttons

    Hi gurus,
    I am using 2 popup in a view.same popup's having same buttons 'Yes', 'No'.when i use 1st one i have to create an action for that Yes button where i put my code for that particular Action.
    But when i used 2nd one the action define for that is not acceptable with differ name.it takes only standard one.
    Now my Query is : How to use same actions for differ pop-up buttons with in a similar view?Where i put my code.
    Plz sugges me.
    <b>Points will be sured.</b>
    Sanket sethi

    Hi ,
    u can use the method SUBSCRIBE_TO_BUTTON_EVENT of the IF_WD_WINDOW interface ... to handle the event fired by the popup .....used this method after creating the popup window ...
    regards
    Yash

  • Adobe premiere elements 12 - how to create menu bar action for my inserted image button

    adobe premiere elements 12 - how to create menu bar action for my inserted image button without using their movie menu theme

    forbemag
    I do not think that I am completely focused into this completely, so let us see if the following is going to help.
    You are going to need a base for your button. When you mention "image" button, I am assuming that you are using that term to differentiate between a text button and a thumbnail type button.
    The menus (main and scene) take their origin in .psd files on the hard drive and strict nomenclature and structure for Layers Palatte. And, the buttons on the menus trace back to the menu markers on the Timeline, main menu marker and order of placement of scene markers. The scene thumbnail will only appear when there is a scene marker on the Timeline to which it.
    In view of all that
    Where have you already inserted this "image (button)"...into the Layers Palette of a Photoshop document or other? Is this "image (button)" in a structured Layer Group in the Layers Palette with sublayer groups, text layers, graphic/background layer"?
    Let me give you an example
    If you have a button (with thumbnail) on the main menu and you want that to open to a specific scene in your movie, then you use a main menu marker on the Timeline at the spot that you want that button to target.
    If I am getting closer to what you seek, then please further clarify the DVD navigational envisioned scheme.
    Thanks.
    ATR
    Add On...I did not see the exchanges between us and SG until after I had posted mine. I thought that your discussions were concluded. Please excuse the interruption.

  • Connecting kurzweil sp88 to GB I want to connect my Kurzweil sp88 to GB. The Kurzweil has a baffling array of buttons and possible MIDI channels. It also has the old style MIDI plug ins. I've got the MIDI out connected to my MacBook Pro via USB adapter.

    I have my Kurzweil connected to my MacBook Pro via an adapter (old style MIDI from Kurzweil to USB adapter connecting to Mac). The Kurzweil has a baffling array of buttons and possible MIDI channels. Can anyone please tell me which of the channels I should use? Also, should I leave the MIDI In cable out of the Kurzweil? Thanks for as detailed an answer as possible. If you like you can respond directly to me at [email protected]

    First of all I do not own this keyboard. But a quick look at the manual (found on the internet) says that when first powered up the SP88 is in Internal Sounds mode. Meaning you hear the sounds selected when playing the keyboard. It also stated that the default Midi Transmit Channel is Midi Channel #1.
    Looks like you would need to go to the SP88 Keyboard's MIDI SETUP and simply select LOCAL OFF. Now the keyboard should be ready to transmit Midi Out only and no internal sounds should be heard.
    Next you may or may not need to do this but you might need to go into the Utilities Folder found in your Applications Folder on your Mac and double click AUDIO MIDI SETUP the keyboard Icon. Figure #1 below.
    Next you need to make a physical connection from the Midi Out of the SP88 to the Midi In on your Midi Interface. Figure #3 below. You may need to Click on Add Device first. Figure #2 below.
    In the examples shown below I have a Yamaha Electronic Drum Kit that is Midi Out only... not USB out. I had to plug a Midi cable from the Midi Out Port on the back of my Drum Module to the Midi In Port on the back of my Motu 828 Audio Inferface which is connected to my Mac. Next I created the Audio Interface and Drum Module "Devices" as mentioned above.
    I then double clicked on the Drum Module and made my Midi Out Selections. Figure #4 Below. And once it was setup and the icons closed I used my mouse to draw a virtual cable connection from the Drum Module Out to the Audio Interface In. Figure #3 below. This is how I got my Midi Devices to talk to one another.
    Once you have made your setup as I have outlined create a MIDI Instrument Track in Garageband, select an instrument for example a Piano then Record Enable that track in Garageband and see if you now hear what you are playing. In this example the SP88 is a Midi Controller only and you will only hear sounds selected in Garageband which is what you want.
    Keep in midi your setup may be different than mine. You can keep your SP88 Keyboard on the default Midi Channel #1. That would be a good place to start. Garageband receives Midi on all channels.

  • Array of buttons return indexof?

    Hi,
    I have an array of buttons. How do I get the value of the array so i know which button the user clicks on. If they click on button 10 i need to get the value 10. I cant copy and past the code here because its too long.If someones interested i can email the code and what i'm trying to do. If someone could give me a generic example of a situation that would be great.
    Thanks in advance

    public class KeyPadDemo extends Frame {
         KeyPad keyPad;
         TextArea textArea;
         public static void main(String[] args){
              new KeyPadDemo();
         public KeyPadDemo(){
              this.setSize(300,300);
              this.setLayout(new BorderLayout());
              this.add(textArea = new TextArea(), "North");
              this.add(keyPad = new KeyPad(textArea),"Center");
              this.setVisible(true);
    class KeyPad extends Panel{
         MyButton buttonZero, buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive, buttonSix;
         MyButton buttonSeven, buttonEight, buttonNine, buttonDecimal, buttonZeroZero;
         TextArea outputArea;
         KeyPad(TextArea outputArea){
              this.outputArea=outputArea;
              setLayout(new GridLayout(4,3));                    //GUI number button pad
              add(buttonSeven = new MyButton("1"));
              add(buttonEight  = new MyButton("2"));
              add(buttonNine = new MyButton("3"));
              add(buttonFour = new MyButton("4"));
              add(buttonFive = new MyButton("5"));
              add(buttonSix = new MyButton("6"));
              add(buttonOne = new MyButton("7"));
              add(buttonTwo = new MyButton("8"));
              add(buttonThree = new MyButton("9"));
              add(buttonZero = new MyButton("0"));
              add(buttonDecimal  = new MyButton("."));
              add(buttonZeroZero = new MyButton("00"));
         class MyButton extends Button implements ActionListener{
              String string;
              public MyButton(String string){
                   super(string);
                   this.string=string;
                   addActionListener(this);
              public void actionPerformed(ActionEvent e){
                   outputArea.append(string);
    }

  • Array of buttons and Event Structure

    I have a the same problem... I was wondering if you were able to find a soln.
    I have literally thousands of buttons and each button as a unique key code and unique name.
    For Example :  
    Button Name    – Key code #
    PUSH              - 1
    POP                 - 2
    PULL               - 3
    The way I program this simple algorithm is deadly painful. 
    1)      I create 1000 buttons by going to control->buttons.
    2)      Then I have to go through one by one to change the Boolean text to “PUSH,POP,PULL…”  
    3)      Then used the Event Structure, adding in all the controls names “PUSH, POP, PULL..” In side each event, I placed the key codes “1,2,3..”
    I have search and tried for weeks to find a better way, and still nothing comes to mind.  
    Please help.

    Hi Ben,
    Thank you for your reply, I have attached my try_code and a picture of the code I currently have.  I  am new in Labview, but I am confident that, there must be a easier way to make an array of buttons with different button name and an associated number to that button.  
    The 1st file, is to show you want I am currently doing, (painful)
    The 2nd file attached is some of the things i tried, I had great hope with my try_code, array_jan25.  But I couldn't get it.  I think I am very close.
    Thanks again,
    Attachments:
    ThePainfulWay.doc ‏63 KB
    Array_jan25.vi ‏42 KB

  • Creating array of buttons

    I'm trying to create an array of buttons, but I keep getting a npe
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl<Container.java:1031>
    at java.awt.Container.add<container.Java:352>
    at ButtonArray.main<ButtonArray.java:26>
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class ButtonArray{
    static JLabel jl;
    public static void main(String[] args){
    JFrame jf = new JFrame();
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    jl = new JLabel();
    int count = 30;
    JButton[] jbh;
    jbh = new JButton[count];
    //JButton[] jbh = new JButton[]{new JButton("1"), new JButton("2")};
    for(int x=1; x<count; x++) {
         jbh[x] = new JButton(Integer.toString(x));
    Container c = jf.getContentPane();
    for(int i = 0; i < jbh.length; ++i){
    p1.add(jbh);
    for(int i = 0; i < jbh.length; ++i){
    jbh[i].addActionListener(new ButtonListener());
    c.add(p1, BorderLayout.NORTH);
    p2.add(jl);
    c.add(p2, BorderLayout.SOUTH);
    jf.setSize(400,300);
    jf.setVisible(true);
    jf.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent evt){
    System.exit(0);
    static class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent evt){
    jl.setText("Button" + evt.getActionCommand() + " was pressed");

    Just spotted the error.
    Your for loop adding buttons to the array begins at 1. Therefore there is a null still in position 0 of your array which you attempt to access in your next for loop.
    [shakes fist at Enc]
    Edited by: flounder on Aug 7, 2009 11:08 AM

  • Cannot add an action after I select my "Button" Symbol

    Ok, I have been following a whole bunch of tutorials, and a
    lot of the tutorials I've found are using Flash 8. I downloaded the
    trial for Flash CS3 and when I'm following the tutorial on adding
    an action, or link, to a button, they say...
    "Click the button"
    "Hit F9, to open the actions panel"
    Then for them, the action panel opens up and gives them the
    ability to add an action to their button, and in the upper left
    corner tab, they say to MAKE SURE IT SAYS "Actions - Button" and
    NOT "Actions - Frame"
    Well, in Adobe CS3 and actionscript, if I click on the button
    and hit F9, the action panel opens up, but I can't add an action to
    the button for some reason. I can only add it to "Action - Frame".
    Am I doing something wrong or what because I follow the
    tutorial exactly, but it seems like something changed in
    Actionscript 3.0 verses 2.0.
    If I Open a new file using Actionscript 2.0, everything works
    ok with the tutorial, but if I open a file withh 3.0 and try adding
    an action to a button, it doesn't work. Could anyone help me please
    or tell me what I'm doing wrong, thanks.

    AS3 no longer supports the attachment of code to Object
    instances, and requires code to be placed on the timeline, which is
    proper. Additionally, you will need to use the eventDispatcher
    model to 'connect' a responding function to the event and add an
    'event listener' to the Object. So basically the format for AS3
    code is something like:
    function stopTheThing(event:MouseEvent) {
    stop();
    my_btn.addEventListener(MouseEvent.CLICK,
    stopTheThing);

  • Workflwow query to know the status of the PO, Action, performed by

    can any one help me to get the workflow query to know the status of the PO, Action, performed by. or the table which can show the Action, Performed by.
    please help me with action history table
    Edited by: user11309801 on Jun 20, 2010 1:13 AM

    Not sure what workflow has to do with PO action history, but pl see if MOS Doc 394787.1 (How to Tell What had been Changed and Who had Made the Changes to a Purchase Order?) can help.
    HTH
    Srini

  • Getting the index of the button pressed in an array of buttons

    Hi,
    I have an array of buttons.
    JButton buttons[];
    buttons = new JButton[18];
    for(int idx = 0; idx<buttons.length; idx++)
    buttons[idx] = new JButton("changing button name");
    buttons[idx].addActionListener(this);
    locationButtonGrid.add(buttons[idx]);
    locationButtonGrid.validate();
    I need to know the index of the button pressed. I can't work from the string that is to be sent to the actionListener as this is going to be coming from a database and changing all the time. The idea behind it is that I am narrowing a list of entries down in a database by dividing it into 18 different sections, then dividing that section into 18 different sections and so on.
    I need to know which of the 18 buttons was pressed. Is this possible?
    Please let it be so!
    Cheers,
    elmicko

    Try this (my code is in bold):
    JButton buttons[];
    buttons = new JButton[18];
    for(int idx = 0; idx<buttons.length; idx++)
    buttons[idx] = new JButton("changing button name");
    buttons[idx].addActionListener(this);
    buttons[idx].setID( String.valueOf( idx ) );
    locationButtonGrid.add(buttons[idx]);
    locationButtonGrid.validate();
    In the listener (assuming event is evt):
    buttons[ Integer.parseInt(evt.getSource().getID()) ]
    or
    int idx = Integer.parseInt(evt.getSource().getID());
    buttons[idx]
    finally, if you want to modify the button when you get the event, you can just call the JButton methods by casting the source to a JButton and working from there.

Maybe you are looking for

  • Performance and data types: which to use?

    Hi All, I am wondering what data type to use and the effect of them on memory/speed. 1. What is the difference (if any) of using sgl, dbl, int etc. Looking at the LabVIEW help there seems to be a range of 8-256 bits of storage according to the data t

  • Adapter MetaData problem (PI installation).

    Hi All, I had installed 'PI' and as part of post installation, i imported the CR_Content.zip into the SLD. After doing that when i import the SAP BASIS 7.00 Software component into the repository, to my surprise it is imported but doesn't contain any

  • Syncing on 2 PCs

    Hi, as a surprise I bought a 6th gen nano for my girlfriend, filled it up with some songs from my iTunes library, put it back into the box and gave it to her. If she now links it to her USB-port, her iTunes on her PC does not recognize it. She has ve

  • Hide Menu Bar on BBPSTART trx

    Hi guys, I would like to know if one can hide for default the menu bar at the right hand side of the start screen (BBPSTART) in SRM 5.0. I do not need the menubar to disappear! Just hide it when oen opens SRM Thanks for any reply, Aart

  • GTX680-Mac_What are the Correct Drivers for 10.8.5?

    GTX680 on MacPro5, mid 2010.... and having issues. What are the correct drivers for Mac GeForce GTX680? And is there another driver for the OS as well? Or not in use when using GTX? I can not export without my system completely shutting down....many