-1

I am trying to have buttons respond to being pressed and output to a textfield in Java Swing. The GUI will show once I execute the code, however it is pretty small and I must extend the window. The main problem is that the listener is not working. I do not understand why. It is a nested ActionListener. Any help would be greatly appreciated.

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import java.awt.event.*;

public class MyAtm extends JPanel {
private final ButtonGroup buttonGroup = new ButtonGroup();
JTextField inputField;
JText text = null;
JButton withdrawalButton;
JButton depositButton;
JButton transferButton;
JButton balanceButton;
JRadioButton checkingAccountRadioButton;
JRadioButton savingAccountRadioButton;

/**
 * Create the panel.
 */
public MyAtm() {
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 173, 0, 0, 172, 0, 0, 0, 27, -21, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 36, 0, 0, 31, 30, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JButton withdrawalButton = new JButton("Withdraw");
    withdrawalButton.setName("Withdraw");
    //buttonGroup.add(withdrawalButton);
    GridBagConstraints gbc_withdrawalButton = new GridBagConstraints();
    gbc_withdrawalButton.fill = GridBagConstraints.BOTH;
    gbc_withdrawalButton.insets = new Insets(0, 0, 5, 5);
    gbc_withdrawalButton.gridx = 5;
    gbc_withdrawalButton.gridy = 2;
    add(withdrawalButton, gbc_withdrawalButton);

    JButton depositButton = new JButton("Deposit");
    GridBagConstraints gbc_depositButton = new GridBagConstraints();
    gbc_depositButton.fill = GridBagConstraints.BOTH;
    gbc_depositButton.insets = new Insets(0, 0, 5, 5);
    gbc_depositButton.gridx = 8;
    gbc_depositButton.gridy = 2;
    add(depositButton, gbc_depositButton);

    JButton transferButton = new JButton("Transfer");
    GridBagConstraints gbc_transferButton = new GridBagConstraints();
    gbc_transferButton.fill = GridBagConstraints.BOTH;
    gbc_transferButton.insets = new Insets(0, 0, 5, 5);
    gbc_transferButton.gridx = 5;
    gbc_transferButton.gridy = 5;
    add(transferButton, gbc_transferButton);

    JButton balanceButton = new JButton("Balance");
    GridBagConstraints gbc_balanceButton = new GridBagConstraints();
    gbc_balanceButton.fill = GridBagConstraints.BOTH;
    gbc_balanceButton.insets = new Insets(0, 0, 5, 5);
    gbc_balanceButton.gridx = 8;
    gbc_balanceButton.gridy = 5;
    add(balanceButton, gbc_balanceButton);

    JRadioButton checkingAccountRadioButton = new JRadioButton("Checking");
    buttonGroup.add(checkingAccountRadioButton);
    GridBagConstraints gbc_checkingAccountRadioButton = new GridBagConstraints();
    gbc_checkingAccountRadioButton.anchor = GridBagConstraints.WEST;
    gbc_checkingAccountRadioButton.fill = GridBagConstraints.VERTICAL;
    gbc_checkingAccountRadioButton.insets = new Insets(0, 0, 5, 5);
    gbc_checkingAccountRadioButton.gridx = 5;
    gbc_checkingAccountRadioButton.gridy = 7;
    add(checkingAccountRadioButton, gbc_checkingAccountRadioButton);

    JRadioButton savingAccountRadioButton = new JRadioButton("Saving");
    buttonGroup.add(savingAccountRadioButton);
    GridBagConstraints gbc_savingAccountRadioButton = new GridBagConstraints();
    gbc_savingAccountRadioButton.fill = GridBagConstraints.HORIZONTAL;
    gbc_savingAccountRadioButton.insets = new Insets(0, 0, 5, 5);
    gbc_savingAccountRadioButton.gridx = 8;
    gbc_savingAccountRadioButton.gridy = 7;
    add(savingAccountRadioButton, gbc_savingAccountRadioButton);

    JTextField inputField = new JTextField();
    inputField.setToolTipText("Enter Your amount");
    GridBagConstraints gbc_inputField = new GridBagConstraints();
    gbc_inputField.gridwidth = 6;
    gbc_inputField.insets = new Insets(0, 0, 5, 5);
    gbc_inputField.fill = GridBagConstraints.BOTH;
    gbc_inputField.gridx = 4;
    gbc_inputField.gridy = 8;
    add(inputField, gbc_inputField);
    //pack();
    //text = new JText();
    withdrawalButton.addActionListener(new JText());



}

private class JText implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            if(e.getSource() == (withdrawalButton)) {
                System.out.println(withdrawalButton.getName());
            }
        }catch(Exception ex) {
            System.out.println(ex);
        }
    }
}
public static void main(String[] args) {
    JFrame frame = new JFrame();
    MyAtm atm = new MyAtm();
    frame.getContentPane().add(atm);
    frame.setVisible(true);

}

}

  • *"The GUI will show once I execute the code, however it is pretty small"* Change `frame.getContentPane().add(atm); frame.setVisible(true);` to `frame.getContentPane().add(atm); frame.pack(); frame.setVisible(true);`. The call to pack will cause a variety of things to happen, including that the frame will be made to be exactly the right size it needs to display the `atm`. Immediately after `pack()` I would typically call `frame.setMinimumSize(frame.getSize());` to ensure that while it can be dragged larger, it can never be dragged to being too small. – Andrew Thompson Jun 17 '18 at 14:05

2 Answers2

2
JButton withdrawalButton = new JButton("Withdraw");

Remove "JButton", you want to assign to an instance variable, not to a local variable.

Look at your action listener implementation, it is as follows:

private class JText implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (e.getSource() == (withdrawalButton)) {
                    System.out.println(withdrawalButton.getName());
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }

and the e.getSource() == (withdrawalButton) check compares the source to an instance variable.

Coder-Man
  • 2,391
  • 3
  • 11
  • 19
1

Your problem is that you're shadowing the withdrawalButton variable by declaring it twice, once in the class, where it is null, and again in the MyAtm constructor. The first one is never assigned an object and so is null, while second one is the one that is being added to the GUI and being given the ActionListener. In your listener, you test if the source is equal to the first null variable.

Solution: don't do this, don't declare the variable twice but rather once, in the class only. e.g., change this:

JButton withdrawalButton = new JButton("Withdraw");

to this:

withdrawalButton = new JButton("Withdraw");

In greater detail, here's what you're doing:

public class MyAtm extends JPanel {

    // ....

    // alternatively, you can assign the object here
    JButton withdrawalButton;

    // ....

    public MyAtm() {

        // .....

        // creating a **local** variable, one that shadows the class field
        JButton withdrawalButton = new JButton("Withdraw");

        // ....

        withdrawalButton.addActionListener(new JText());

    }

    private class JText implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (e.getSource() == (withdrawalButton)) {
                    System.out.println(withdrawalButton.getName());
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }

    // ...
}

Here's what you want to do:

public class MyAtm extends JPanel {

    // ....

    // class field is never initialized
    JButton withdrawalButton;

    // ....

    public MyAtm() {

        // .....

        // initialize the field
        withdrawalButton = new JButton("Withdraw");

        // ....

        withdrawalButton.addActionListener(new JText());

    }

    private class JText implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                // here withdrawalButton is the null field
                if (e.getSource() == (withdrawalButton)) {
                    System.out.println(withdrawalButton.getName());
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }

    // ...
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373