Event Handling with Separate Classes
It seems I have yet another event handling question.
I have a program with three classes. The first class contains the main method as well as some Swing components and an associated ActionListener.
In the second class, there are some other Swing components. I'd like to use the ActionListener in the first class with these components. I can't seem to get this to work.
However, I can access a different ActionListener in the third class by creating an object of the third class and passing it as the argument to addActionListener in the second class.
What is unique about the class with the main method?
repost of code with error message
Sorry for the last post.
Here is the runtime error message:
Exception in thread "AWT-EventQueue-0" JavaAWT: Assertion failure: Java exception thrown
JavaAWT: File src/macosx/native/apple/awt/util/AWTException.m; Line 40
JavaAWT: Assertion failure: _javaException
JavaAWT: File src/macosx/native/apple/awt/util/AWTException.m; Line 48
JavaAWT: Assertion failure: _javaException != ((void *)0)
JavaAWT: File src/macosx/native/apple/awt/util/AWTException.m; Line 148
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Window.init(Window.java:287)
at java.awt.Window.<init>(Window.java:319)
at java.awt.Frame.<init>(Frame.java:419)
at java.awt.Frame.<init>(Frame.java:384)
at javax.swing.JFrame.<init>(JFrame.java:150)
at Test.<init>(Test.java:4)
at FirstPanel.<init>(Test.java:75)
(repeats ad infinitum)
And here is the code sans line numbers:
Code:
import java.awt.*; import java.awt.event.*;
import javax.swing.*; import javax.swing.event.*;
public class Test extends JFrame implements ActionListener {
final static String CARD1 = "first";
final static String CARD2 = "second";
final static String CARD3 = "third";
static JFrame frame;
static JPanel cards;
private FirstPanel first = new FirstPanel();
private SecondPanel second = new SecondPanel();
private ThirdPanel third = new ThirdPanel();
public void addComponentToPane(Container pane) {
JPanel Pane = new JPanel();
JPanel card1 = new JPanel(); card1.add(first);
JPanel card2 = new JPanel(); card2.add(second);
JPanel card3 = new JPanel(); card3.add(third);
cards = new JPanel(new CardLayout());
cards.add(card1,CARD1);
cards.add(card2,CARD2);
cards.add(card3,CARD3);
pane.add(Pane,BorderLayout.PAGE_END);
pane.add(cards,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("first")) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards,CARD2);
}
else if (ae.getActionCommand().equals("second")) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards,CARD3);
}
else if (ae.getActionCommand().equals("third")) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards,CARD1);
}
}
private static void createAndShowGUI() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Test test = new Test();
test.addComponentToPane(frame.getContentPane());
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class FirstPanel extends JPanel {
JButton firstBtn;
JLabel firstLbl;
Test tt = new Test();
//ThirdPanel tp = new ThirdPanel();
FirstPanel() {
setOpaque(true);
setPreferredSize(new Dimension(300,300));
firstLbl = new JLabel("this is the first panel");
firstBtn = new JButton("first");
firstBtn.setActionCommand("first");
firstBtn.addActionListener(tt);
//firstBtn.addActionListener(tp);
add(firstLbl);
add(firstBtn);
}
}
class SecondPanel extends JPanel {
JButton secondBtn;
JLabel secondLbl;
//Test tt = new Test();
SecondPanel() {
setOpaque(true);
setPreferredSize(new Dimension(300,300));
secondLbl = new JLabel("this is the second panel");
secondBtn = new JButton("second");
secondBtn.setActionCommand("second");
//secondBtn.addActionListener(tt);
add(secondLbl);
add(secondBtn);
}
}
class ThirdPanel extends JPanel implements ActionListener {
JButton thirdBtn;
JLabel thirdLbl;
//Test tt = new Test();
ThirdPanel() {
setOpaque(true);
setPreferredSize(new Dimension(300,300));
thirdLbl = new JLabel("this is the third panel");
thirdBtn = new JButton("third");
thirdBtn.setActionCommand("third");
//thirdBtn.addActionListener(tt);
add(thirdLbl);
add(thirdBtn);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("first")) {
CardLayout cl = (CardLayout)(Test.cards.getLayout());
cl.show(Test.cards,Test.CARD2);
}
else if (ae.getActionCommand().equals("second")) {
//System.out.println("testIt = " + testIt);
}
else if (ae.getActionCommand().equals("third")) {
//System.out.println("testIt = " + testIt);
}
}
}