1

I have a Java GUI project where I am creating an ATM. I have everything setup, but for some reason my number pad on the left is not displaying properly. It should appear as a 4x3 grid of numbers, but it is just displaying a 9. I have checked to make sure it is in a GridLayout and I have checked my loop, but I possibly may have looked over something. Any help is appreciated, thanks!

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;

public class ATMProject extends JPanel implements ActionListener {
    private JPanel mainPanel = null;
    private JPanel btnPanel = null;
    private JPanel userBtns = null;
    private JTextArea textArea = null;
    private JPanel keyPanel = null;
    private JTextField numField = null;
    private JPanel numpadPanel = null;
    private JButton[] userButtons = null;
    private JButton[] keypadButtons = null;
    private String[] btnPanelbtns = { "A", "B", "C" };
    private String[] numpadPanelbtns = { "7", "4", "1", "8", "5", "2", "9", "6", "3", "0", ".", "CE" };

    public ATMProject() {
        super();
        mainPanel = new JPanel();
        this.setLayout(new BorderLayout());
        this.add(mainPanel);

        btnPanel = new JPanel();
        btnPanel.setLayout(new GridLayout(3, 1));
        this.add(btnPanel, BorderLayout.EAST);

        textArea = new JTextArea();
        this.add(textArea, BorderLayout.CENTER);

        keyPanel = new JPanel();
        keyPanel.setLayout(new BorderLayout());
        this.add(keyPanel, BorderLayout.WEST);

        numpadPanel = new JPanel();
        numpadPanel.setLayout(new GridLayout(0, 3));
        keyPanel.add(numpadPanel, BorderLayout.CENTER);

        numField = new JTextField();
        keyPanel.add(numField, BorderLayout.NORTH);

        userButtons = new JButton[btnPanelbtns.length];
        for (int i = 0; i < userButtons.length; i++) {
            userButtons[i] = new JButton(btnPanelbtns[i]);
            userButtons[i].addActionListener(this);
            btnPanel.add(userButtons[i]);
        }

        keypadButtons = new JButton[numpadPanelbtns.length];
        for (int i = 0; i < userButtons.length; i++) {
            keypadButtons[i] = new JButton(numpadPanelbtns[i]);
            keypadButtons[i].addActionListener(this);
            numpadPanel.add(keypadButtons[i]);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        MyFrame mf = new MyFrame();
    }

}


import javax.swing.JFrame;

public class MyFrame extends JFrame {
    private ATMProject atm = null;
    public MyFrame(){
        super();
        atm = new ATMProject();
        this.add(atm);
        this.setTitle("ATM");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setSize(800,300);
        this.setVisible(true);
    }
}

This is how it is supposed to appear:

enter image description here

Blastoize34
  • 65
  • 1
  • 8

2 Answers2

2

You're adding you buttons to keyPanel...

keyPanel.add(keypadButtons[i]);

which is using a BorderLayout...

keyPanel = new JPanel();
keyPanel.setLayout(new BorderLayout());

so only the last component added to it will be laid out by the panel.

One imagines you should be adding them to numpadPanel instead...

numpadPanel.add(keypadButtons[i]);

Yeah, I have that numpadPanel.add(keypadButtons[i]); and I am getting just 7 8 9 vertically.

That's kind of how GridLayout works, you could force into a horizontal priority mode by using something more like

numpadPanel.setLayout(new GridLayout(0, 3));

KeyPad

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • `keyPanel.setLayout(new BorderLayout());` Oops! missed that bit.. :-P – Andrew Thompson Apr 06 '16 at 00:32
  • Welcome to the forest, watch out for the trees ;) – MadProgrammer Apr 06 '16 at 00:34
  • Yeah, I have that `numpadPanel.add(keypadButtons[i]);` and I am getting just 7 8 9 vertically. – Blastoize34 Apr 06 '16 at 00:35
  • It's kind of the way `GridLayout` works, try making your `numpadPanelbtns` in the order of `7, 4, 1, 8, 5, 2, 9, 6, 3` instead and see what happens – MadProgrammer Apr 06 '16 at 00:38
  • @Blastoize34 or you could just use `new GridLayout(0, 3)` and force into horizontal priority mode instead – MadProgrammer Apr 06 '16 at 00:42
  • 1
    BTW OP - try adding a `TitledBorder` to each panel showing the layout, as seen in [this answer](http://stackoverflow.com/a/5630271/418556). That, or setting different colors to the background of each panel, is a great way to debug a GUI that is laid out using multiple different layout managers. – Andrew Thompson Apr 06 '16 at 00:43
  • I used `new GridLayout(0,3)` and I get 7 4 1 horizontally, I just updated my code above. – Blastoize34 Apr 06 '16 at 01:01
  • @Blastoize34 As I said you could change your `numpadPanelbtns` order **OR** you could use `GridLayout(0,3)`, I suggest you put your `numpadPanelbtns` back to it's original order – MadProgrammer Apr 06 '16 at 01:05
  • Okay, both ways and nothing changes. Ill look more into it later – Blastoize34 Apr 06 '16 at 01:07
  • 1
    Got it, posted the solution below, thanks for the input about the priority mode and TitleBorder and such!! – Blastoize34 Apr 06 '16 at 01:21
2

My loop was incorrectly written. for (int i = 0; i < userButtons.length; i++) my program was only outputting 3 buttons because userButtons.length only had 3 elements. I changed userButtons.length to numpadPanelbtns.length and it fixed it because there are 12 elements in the numpadPanelbtns array.

Blastoize34
  • 65
  • 1
  • 8