Hi, basically I have an array of JButtons 0-25 labeled A through to Z. When each of the buttons are clicked I need to call a function passing the letter value. I thought that I could do this like this:


if (buttonObj == letterButton[0]) {
functionName();
}

But I have no way to pass the letter to the function. Has anyone got any ideas for this? Thanks!!!

Shaun

I believe you can get the lable of the button:

JButton.getLabel();

that could be deprecated so you can also simply try:

JButton.getText();

Hi, thanks for your reply but it doesn't seem to work. I have used the following:

messageBox( acceptButton.getText() );

This words because acceptButton is not part of an array.
But when I do the same with a button which is part of an array such as:

messageBox( acceptButton[].getText() );
or
messageBox( acceptButton[0].getText() );

then it doesn't work. It says that it can not find the varible acceptButtons when I have previously definded this as an array of buttons.

Any ideas??

Make sure the acceptButton[] array is global. If you declared and defined it in your constructor, then it won't be seen outside of that method.

I would recommend making the button a listener of itself.

public ExtendedJButton extends JButton implements Actionlistener
{
  public ExtendedJButton(String name)
  {
    super(name);
    init();
  }

  private void init()
  {
    addActionListener(this);
  }

  public void actionPerformed(ActionEvent e)
  {
    System.out.println("OVERRIDE actionPerformed(ActionEvent e)");
  }
}

Then create the button like so...

JButton jButton = new ExtendedJButton("A")
{
    public void actionPerformed(ActionEvent e)
    {
       messageBox(getText());
    }
};

I think that should work.

You can just add a listener to each button.

JButton[] buttonArray = new JButton[25];

for (int i = 0; i < buttonArray.length; i++) {
    buttonArray[i] = new JButton(String.valueOf(i + 1));
    buttonArray[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("actionPerformed(ActionEvent)");
        }
    });
    // maybe set location of buttonArray[i]
    // maybe set size of buttonArray[i]
}

This can get messy though; you will now have 25 classes in your classes folder. What I would do is create an inner private class that is an ActionListener, then add that to each of the buttons.

JButton[] buttonArray = new JButton[25];

for (int i = 0; i < buttonArray.length; i++) {
    buttonArray[i] = new JButton(String.valueOf(i + 1));
    buttonArray[i].addActionListener(new MyActionListener());
    // maybe set location of buttonArray[i]
    // maybe set size of buttonArray[i]
}

// ..

private class MyActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.out.println("actionPerformed(ActionEvent)");
    }
}

Hi, Thanks for your reply :)

Do you know of any other way to do this without using action Listeners as I am not suppose to use them for this part of my assignment, if not then I will just have too. Thanks again, Shaun :)

Hi, Thanks for your reply :)

Do you know of any other way to do this without using action Listeners as I am not suppose to use them for this part of my assignment, if not then I will just have too. Thanks again, Shaun :)

Dude maybe I'm missing something here, but how else are you supposed to handle events without having event listeners?

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

I was under the impression that you could use the getText function or something similar. Obviously not then. So for an array of buttons, you would rec. an active listener as previously stated? Cheers mate

This article has been dead for over six months. Start a new discussion instead.