The ActionListener
s are set up properly but for some reason are not working when I press one of the buttons. Any ideas? I have searched for ways to do it but nothing seems to work. Here is my code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Master extends JPanel implements ActionListener {
final static String SIGNIN = "Sign In";
final static String SIGNOUT = "Sign Out";
final static String OTHER = "Other Stuff";
final static int extraWindowWidth = 100;
public String[] Names, SecurityNumber;
public int FirstService = 0;
public int SecondService = 0;
public int counter = 0;
protected JTextField ChildName, ChildSecurityNum, SecurityNum;
protected JButton SignOut, FirstServiceButton, SecondServiceButton,
FirstServiceTotal, SecondServiceTotal;
public Master(Container pane) {
// Set up Arrays for storage
SecurityNumber = new String[300];
Names = new String[300];
// Text Fields
ChildName = new JTextField("");
ChildName.setEditable(false);
ChildName.setColumns(10);
SecurityNum = new JTextField("");
SecurityNum.setEditable(true);
SecurityNum.setColumns(2);
ChildSecurityNum = new JTextField("");
ChildSecurityNum.setEditable(true);
ChildSecurityNum.setColumns(5);
// Buttons
SignOut = new JButton("Sign Out");
SignOut.setActionCommand("signOut");
SignOut.setEnabled(true);
FirstServiceButton = new JButton("Sign In First Service");
FirstServiceButton.setActionCommand("signinfirst");
FirstServiceButton.setEnabled(true);
SecondServiceButton = new JButton("Sign In Second Service");
SecondServiceButton.setActionCommand("signinsecond");
SecondServiceButton.setEnabled(true);
FirstServiceTotal = new JButton("First Service Total Count");
FirstServiceTotal.setActionCommand("firstservicetotal");
FirstServiceTotal.setEnabled(true);
SecondServiceTotal = new JButton("Second Service Total Count");
SecondServiceTotal.setActionCommand("secondservicetotal");
SecondServiceTotal.setEnabled(true);
SecondServiceButton.addActionListener(this);
FirstServiceButton.addActionListener(this);
FirstServiceTotal.addActionListener(this);
SecondServiceTotal.addActionListener(this);
SignOut.addActionListener(this);
JTabbedPane tabbedPane = new JTabbedPane();
// Create the "cards".
JPanel card1 = new JPanel() {
// Make the panel wider than it really needs, so
// the window's wide enough for the tabs to stay
// in one row.
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += extraWindowWidth;
return size;
}
};
card1.add(new Label("Child's Name :"));
card1.add(new JTextField("", 15));
card1.add(new Label("Child's Security Number :"));
card1.add(ChildSecurityNum);
card1.add(new Label("Child's Grade :"));
card1.add(new JTextField("", 2));
card1.add(FirstServiceButton);
card1.add(SecondServiceButton);
JPanel card2 = new JPanel();
card2.add(new Label("Enter Security tag # :"));
card2.add(new JTextField("", 5));
card2.add(SignOut);
card2.add(new Label("Child's Name :"));
card2.add(ChildName);
JPanel card3 = new JPanel();
card3.add(new JButton("Print Attendence Sheet"));
card3.add(FirstServiceTotal);
card3.add(SecondServiceTotal);
tabbedPane.addTab(SIGNIN, card1);
tabbedPane.addTab(SIGNOUT, card2);
tabbedPane.addTab(OTHER, card3);
pane.add(tabbedPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if ("signOut".equals(e.getActionCommand())) {
ChildName.setText("");
SecurityNum.setText("");
int counter = 0;
for (String a : SecurityNumber) {
if (a.equals(SecurityNum.getText())) {
ChildName.setText(Names[counter]);
} else
counter++;
}
}
if ("signinfirst".equals(e.getActionCommand())) {
FirstService++;
counter++;
Names[counter] = ChildName.getText();
SecurityNumber[counter] = SecurityNum.getText();
ChildName.setText("");
SecurityNum.setText("");
}
if ("signinsecond".equals(e.getActionCommand())) {
SecondService++;
counter++;
Names[counter] = ChildName.getText();
SecurityNumber[counter] = SecurityNum.getText();
ChildName.setText("");
SecurityNum.setText("");
}
if ("FirstServiceTotal".equals(e.getActionCommand())) {
JOptionPane.showMessageDialog(null, "There are " + FirstService
+ "Kids.");
}
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Sign in and Sign out");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
Master demo = new Master(frame.getContentPane());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
There isn't any problem with your ActionListeners (determined through printing "hello world" upon the button being pressed). The problem is that you never added SecurityNum to card1, which is why its text isn't getting cleared.