Create JButtons from array

B

Brian

Hi all

I'm writing a small program with a lot of buttons. To make it a little
easier to write the code I've tried to create the JButtons from an
array.

I don't work.

I don't get response from the ActionListener

Any ideas?

/Brian

Simple example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

public class ButtonPanel extends JPanel {
private Controller controller = new Controller();
private JButton btnOne, btnTwo, btnThree, btnABC, btnFour, btnFive,
btnSix, btnExtra1, btnSeven, btnEight, btnNine, btnExtra2, btnAsterix,
btnZero, btnHash, btnSearch;
private JButton[] buttons = { btnOne, btnTwo, btnThree, btnABC,
btnFour, btnFive, btnSix, btnExtra1, btnSeven, btnEight,
btnNine, btnExtra2, btnAsterix, btnZero, btnHash, btnSearch };
private String[] buttonTxt = { "1", "2", "3", "ABC", "4", "5", "6",
"Extra1", "7", "8", "9", "Extra2", "*", "0", "#", "Søg" };

public ButtonPanel() {
for (int i = 0; i < buttons.length; i++) {
buttons = new JButton(buttonTxt);
buttons.setSize(80, 80);
buttons.addActionListener(controller);
this.add(buttons);
}
}

private class Controller implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOne) {
System.out.println("test OK");
}
}
}
 
D

Daniele Futtorovic

Hi all

I'm writing a small program with a lot of buttons. To make it a
little easier to write the code I've tried to create the JButtons
from an array.

I don't work.

I don't get response from the ActionListener

Any ideas?

/Brian

Simple example: import java.awt.event.ActionEvent; import
java.awt.event.ActionListener; import java.util.ArrayList;

import javax.swing.JButton; import javax.swing.JPanel; import
javax.swing.border.EtchedBorder;

public class ButtonPanel extends JPanel { private Controller
controller = new Controller(); private JButton btnOne, btnTwo,
btnThree, btnABC, btnFour, btnFive, btnSix, btnExtra1, btnSeven,
btnEight, btnNine, btnExtra2, btnAsterix, btnZero, btnHash,
btnSearch; private JButton[] buttons = { btnOne, btnTwo, btnThree,
btnABC, btnFour, btnFive, btnSix, btnExtra1, btnSeven, btnEight,
btnNine, btnExtra2, btnAsterix, btnZero, btnHash, btnSearch };
private String[] buttonTxt = { "1", "2", "3", "ABC", "4", "5", "6",
"Extra1", "7", "8", "9", "Extra2", "*", "0", "#", "Søg" };

public ButtonPanel() { for (int i = 0; i < buttons.length; i++) {
buttons = new JButton(buttonTxt); buttons.setSize(80, 80);
buttons.addActionListener(controller); this.add(buttons); } }

private class Controller implements ActionListener { public void
actionPerformed(ActionEvent e) { if (e.getSource() == btnOne) {
System.out.println("test OK"); } } }


The moment your "buttons" array is created (at instantiation time),
btnOne et al. haven't been set to a specific object. Consequently,
they're holding the value null. What's put in your array isn't the
variable btnOne*, but the value they're holding -- in this case: null.
Later on, when you fill the array, the values they were holding get
replaced. btnOne etc. are not affected by this and are null throughout
your program. Consequently your equality comparision in
actionPerformed(ActionEvent) fails.

* You'd need variable pointers for that. There are no valiable pointers
in Java.


Learn more about variables and Objects:
<> err... Could somebody provide me with a good link on this topic? I
remember a nice one with a remote in a glass, but can't seem to find it.


You could simply replace:
e.getSource() == btnOne
with:
e.getSource() == buttons[0]
or use a java.util.List instead of an array and use (not with an "if"):
buttons.indexOf(e.getSource())


DF.
 
K

Knute Johnson

Brian said:
Hi all

I'm writing a small program with a lot of buttons. To make it a little
easier to write the code I've tried to create the JButtons from an
array.

I don't work.

I don't get response from the ActionListener

Any ideas?

/Brian

Simple example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

public class ButtonPanel extends JPanel {
private Controller controller = new Controller();
private JButton btnOne, btnTwo, btnThree, btnABC, btnFour, btnFive,
btnSix, btnExtra1, btnSeven, btnEight, btnNine, btnExtra2, btnAsterix,
btnZero, btnHash, btnSearch;
private JButton[] buttons = { btnOne, btnTwo, btnThree, btnABC,
btnFour, btnFive, btnSix, btnExtra1, btnSeven, btnEight,
btnNine, btnExtra2, btnAsterix, btnZero, btnHash, btnSearch };
private String[] buttonTxt = { "1", "2", "3", "ABC", "4", "5", "6",
"Extra1", "7", "8", "9", "Extra2", "*", "0", "#", "Søg" };

public ButtonPanel() {
for (int i = 0; i < buttons.length; i++) {
buttons = new JButton(buttonTxt);
buttons.setSize(80, 80);
buttons.addActionListener(controller);
this.add(buttons);
}
}

private class Controller implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOne) {
System.out.println("test OK");
}
}
}


See the notes in you code (modified) below. If you are going to post
code, please post an SSCCE (see http://www.physci.org/codes/sscce.html).
It would be really nice if you could develop a neat indentation style.

This should get you going, post back if you have further questions.

//
// it is just easier to include the whole package rather than having all
// those statements
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonPanel extends JPanel {
private Controller controller = new Controller();
//
// these are redundant because you reassign them in the loop
//
// private JButton btnOne, btnTwo, btnThree, btnABC, btnFour, btnFive,
// btnSix, btnExtra1, btnSeven, btnEight, btnNine, btnExtra2, btnAsterix,
// btnZero, btnHash, btnSearch;
//
// these are also a problem
// private JButton[] buttons = { btnOne, btnTwo, btnThree, btnABC,
//btnFour, btnFive, btnSix, btnExtra1, btnSeven, btnEight,
//btnNine, btnExtra2, btnAsterix, btnZero, btnHash, btnSearch };
//
private String[] buttonTxt = { "1", "2", "3", "ABC", "4", "5", "6",
"Extra1", "7", "8", "9", "Extra2", "*", "0", "#", "Søg" };
//
// just create an array of JButtons
//
JButton[] buttons = new JButton[buttonTxt.length];

public ButtonPanel() {
for (int i = 0; i < buttons.length; i++) {
buttons = new JButton(buttonTxt);
buttons.setSize(80, 80);
buttons.addActionListener(controller);
this.add(buttons);
}
}

//
// rather than create another class that implements ActionListener you can
// have your ButtonPanel class implement ActionListener
// to do that just change the first line of the class to;
//
// public class ButtonPanel extends JPanel implements ActionListener {
//
// and add an actionPerformed() method to the class
//
private class Controller implements ActionListener {
public void actionPerformed(ActionEvent e) {
/*
if (e.getSource() == btnOne) {
System.out.println("test OK");
}
*/
// get the action command which is the text of the button unless
// otherwise set and test that for the one you want, that way
you don't
// have to have references to all the buttons here
String actionCommand = e.getActionCommand();
if (actionCommand.equals("1"))
System.out.println("test OK");
else
System.out.println(actionCommand);
}
}


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ButtonPanel());
f.pack();
f.setVisible(true);
}
});
}

}
 
N

none

You are instantiating one set of buttons when you create the array, and
overwriting them with another set in the loop. The, you test for the
wrong button in the callback.

Arthur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top