2

Is there more efficient way to add button R, 0 and E? Is it possible to somehow add them to the array?

public Keypad() {
        setTitle("Keypad");
        setSize(220, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(4, 3, 2, 2));

    JButton[] buttonArray = new JButton[10];
    for (int i = 1; i < buttonArray.length; i++) {
        buttonArray[i] = new JButton(String.valueOf(i));
        add(buttonArray[i]);
        buttonArray[i].addActionListener(this);
    }

    add(buttonR);
    add(button0);
    add(buttonE);

    setVisible(true);
    setResizable(false);
}
bsiamionau
  • 8,099
  • 4
  • 46
  • 73
knowbody
  • 8,106
  • 6
  • 45
  • 70

2 Answers2

3

You can't get much more efficient than you are already now. You could maybe slightly reorganize the code to move the addition of the ActionListener and the addition of the button to the hierarchy in a single method. Also, the JButton[] is useless so drop it, you will save a few bytes in memory (but really nothing major):

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Keypad extends JFrame implements ActionListener {

    private JButton buttonR = new JButton("R");
    private JButton button0 = new JButton("0");
    private JButton buttonE = new JButton("E");

    public Keypad() {
        setTitle("Keypad");
        setSize(220, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(4, 3, 2, 2));
        for (int i = 1; i < 10; i++) {
            addButton(new JButton(String.valueOf(i)));
        }

        addButton(buttonR);
        addButton(button0);
        addButton(buttonE);

        setResizable(false);
        setVisible(true);
    }

    private void addButton(JButton button) {
        button.addActionListener(this);
        add(button);
    }

    @Override
    public void actionPerformed(ActionEvent e) {


    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • +1 for helpful review. Might a later `pack()` be an alternative to `setSize()` in a [non-resizable container](http://stackoverflow.com/a/12532237/230513)? – trashgod Feb 21 '13 at 12:40
  • @trashgod indeed pack() is usually preferred. – Guillaume Polet Feb 21 '13 at 12:48
  • @knowbody You have different ways to handle those. You could set an "ActionCommand" on each button (for example one which is equivalent to the label) and use a `swicth` (or `if/elseif/elseif/.../else`) on the action command of the ActionEvent. – Guillaume Polet Feb 21 '13 at 12:51
  • @Guillaume Polet thanks. what is the difference between `setSize()` and `pack()`? – knowbody Feb 21 '13 at 12:52
  • 2
    @knowbody when you use pack(), the frame will take the size of the preferred size of the content pane (plus the necessary spaces for borders, if any). Usually, pack() is preferred over setSize() because it will work better across different platforms and Look & Feels. I you want the buttons of your Keypad to be somehow bigger than the default preferred size, you can add an empty border to each button: `button.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));` (`pack()` should be called before `setLocationRelativeTo(null);` which itself should be called before `setVisible(true)`) – Guillaume Polet Feb 21 '13 at 12:59
1

"Is it possible to somehow add them to the array?"

Yes you can do this if that is what you want

JButton[] buttonArray = new JButton[10];
for (int i = 1; i < buttonArray.length+3; i++) {

   if(i<buttonArray.length){
    buttonArray[i] = new JButton(String.valueOf(i));
    add(buttonArray[i]);
    buttonArray[i].addActionListener(this);
   }
  if(i==buttonArray.length+1){add(buttonR);}
  if(i==buttonArray.length+2){add(button0);}
  if(i==buttonArray.length+3){add(buttonE);}

}

This does what you want but AFAIK there is no quicker way to do this.

phcoding
  • 608
  • 4
  • 16