Phoenix
Phoenix

Reputation: 23

Can you take a value of an array and use it in a variable

I have 26 variables (case1, case2, ...) I have 26 buttons. (btnCase1, btnCase2, ...) Is there a way to do this;

final JButton btn18 = new JButton("18");
    btn18.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            btn18.setText(Integer.toString(cases[18]));
            picked = cases[18];
            btn18.setEnabled(false);
            btnNext.doClick();
            casesPicked++;
        }
    });
    btn18.setBounds(785, 281, 100, 100);
    frame.getContentPane().add(btn18);

    final JButton btn19 = new JButton("19");
    btn19.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            btn19.setText(Integer.toString(cases[19]));
            picked = cases[19];
            btn19.setEnabled(false);
            btnNext.doClick();
            casesPicked++;
        }
    });

Is there a way to say something like this:

 final JButton btn18 = new JButton("18");
    btn18.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             int i = 18
            btn[i].setText(Integer.toString(cases[i]));
            picked = cases[i];
            btn[i].setEnabled(false);
            btnNext.doClick();
            casesPicked++;
        }
    });

I need to be able to do this later on in the code to recall what cases have already been chosen.

This is my current full code. http://pastebin.com/9Am3jMQ8 Probably the worst code you guys will ever read.

Upvotes: 0

Views: 86

Answers (3)

dsh
dsh

Reputation: 12214

Can you take a value of an array and use it in a variable?

Yes.



I would create a class for your ActionListener. In it you can define exactly what you want it to do and provide it the data it needs to do it.

class MyActionListener implements ActionListener
    {
    private final JButton button;
    private final JButton btnNext;
    private final int index;

    public MyActionListener(final int index,
                            final JButton button,
                            final JButton btnNext)
        {
        this.index = index;
        this.btnNext = btnNext;
        this.button = button;
        }

    @Override
    public void actionPerformed(final ActionEvent event)
        {
        picked = cases[i];
        this.button.setText( Integer.toString(cases[i]) );
        this.button.setEnabled(false);
        this.btnNext.doClick();
        casesPicked++;
        }

    }

Then you can set this up, without repeating yourself:

final JButton btn18 = new JButton("18");
btn18.addActionListener( new MyActionListener(18, btn18, btnNext) );
frame.getContentPane().add(btn18);

final JButton btn19 = new JButton("19");
btn19.addActionListener( new MyActionListener(19, btn19, btnNext) );
frame.getContentPane().add(btn19);

You could then easily change your button creation to use a loop:

for (int btnNum = 0 ; btnNum < 20 ; i += 1)
    {
    final JButton btn = new JButton( ""+btnNum );
    btn.addActionListener( new MyActionListener(btnNum, btn, btnNext) );
    frame.getContentPane().add(btn);
    }

Upvotes: 1

Madushan Perera
Madushan Perera

Reputation: 2598

You can implement Jbutton array like below :

    JButton button[] = new JButton[5];

    for (int i = 0; i < button.length; i++) {
        JButton btn = button[i];
        btn = new JButton("Button " + i);
        btn.addActionListener(this);
        jPanel2.add(btn);//adding button to a panel
        // set Layout to your panel as u can display all ur buttons in kind of order 
//Ex. jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.PAGE_AXIS));
    }

Implement actionListener interface to your class

@Override
public void actionPerformed(ActionEvent e) {
    switch (e.getActionCommand()) {
        case "Button " + 0:
            System.out.println("pressed button 0");
            break;
        case "Button " + 1:
            System.out.println("pressed button 1");
            break;
        case "Button " + 2:
            System.out.println("pressed button 2");
            break;
        case "Button " + 3:
            System.out.println("pressed button 3");
            break;
        case "Button " + 4:
            System.out.println("pressed button 4");
            break;
        default:
            break;

    }

}

Upvotes: 0

Shadow
Shadow

Reputation: 4006

If I'm not mistaken, you want to make all the buttons work dynamically. This can be done by making a class that implements ActionListener, and writing dynamic code inside that, that will work with all your buttons. Something like this:

public class ButtonClick implements ActionListener {

    btn1.addActionListener(this);
    //More buttons
    btn18.addActionListener(this);
    btn19.addActionListener(this);

    public void actionPerformed (ActionEvent e) {
        //Use a global variable as a count for the number
        //of buttons clicked

        Object source = e.getSource();
        if (source instanceof JButton) {
            JButton thisButton = (JButton)source;
            Integer caseNumber = Integer.parseInt(thisButton.getText());

            thisButton.setText(cases[caseNumber].toString());
            //Any other code you need
        }
    }
}

Upvotes: 0

Related Questions