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);
}
}