I have two classes: one which draws the applet and one which adds the actionListeners. It seems that the applet is not correctly adding the actionListeners because none of the functions in my applet work. Following are snippets of my code:
This belongs to the applet class (StackApplet):
actListen is a new instantiation of the Listener class.
public void init() {
try {
SwingUtilities.invokeAndWait(
new Runnable() {
@Override
public void run() {
actListen.invokePush();
actListen.invokePop();
}
});
} catch (Exception e) {
}
This belongs to the listener class:
public void invokePush() {
pushListener = new ActionListener() {
public void actionPerformed(ActionEvent act) {
int currentSize = (int)myStack.size();
try {
if (currentSize == ceiling) {
StackApplet.pushField.setEnabled(false);
StackApplet.pushField.setForeground(Color.RED);
StackApplet.pushField.setText("Error: The stack is already full");
} else if (currentSize == ceiling - 1) {
StackApplet.pushField.setForeground(Color.YELLOW);
StackApplet.pushField.setText("Warning: The stack is almost full");
} else if (currentSize == 0) {
StackApplet.pushField.setText("weenie");
}
} catch (Exception e) {
}
}
};
StackApplet.pushBtn.addActionListener(pushListener);
}
It seems that the Applet is not correctly calling the ActionListeners
I suggest that you pass references and call public methods on these references, something like:
Then accept the StackApplet reference in your ActListen’s constructor, and then use that instance to call non-static methods of StackApplet.
Something like,
You will want to strive to avoid use of static anything except in certain situations.