Results 1 to 10 of 10
- 05-20-2011, 11:50 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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?
- 05-21-2011, 12:52 AM #2
What's the access modifier of the ActionListener instance? (public, private, etc.) If it's not visible to the other Swing components, they won't be able to add it as their ActionListener. But you could add it to them from within your main class.
If that doesn't answer your question, please post your code.Get in the habit of using standard Java naming conventions!
- 05-21-2011, 12:59 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
From the way you describe it your "third class" implements the ActionListener interface while the first one - with an "associated" ActionListener instance - does not.
An instance of ActionListener is just an object like any other. You can have an instance variable in a class declared with type ActionListener and manipulate it the same way you would any other: with set and get methods, or passing it around as an argument.
-----
There is an element of guesswork above. Things would be a lot clearer if you constructed a brief runnable example showing the difference between the behaviour of the two classes with respect to using some action listener in a third.
- 05-24-2011, 04:03 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
copy of offending code
Here is a stripped down version of the offending code. In essence, the goal is simply for the user to click a button and change the panel. The code compile, but gives an initialization error when it tries to create the Test object in FirstClass on line 74.
1 import java.awt.*; import java.awt.event.*;
2 import javax.swing.*; import javax.swing.event.*;
3
4 public class Test extends JFrame implements ActionListener {
5 final static String CARD1 = "first";
6 final static String CARD2 = "second";
7 final static String CARD3 = "third";
8
9 static JFrame frame;
10
11 static JPanel cards;
12
13 private FirstPanel first = new FirstPanel();
14 private SecondPanel second = new SecondPanel();
15 private ThirdPanel third = new ThirdPanel();
16
17 public void addComponentToPane(Container pane) {
18 JPanel Pane = new JPanel();
19
20 JPanel card1 = new JPanel(); card1.add(first);
21 JPanel card2 = new JPanel(); card2.add(second);
22 JPanel card3 = new JPanel(); card3.add(third);
23
24 cards = new JPanel(new CardLayout());
25
26 cards.add(card1,CARD1);
27 cards.add(card2,CARD2);
28 cards.add(card3,CARD3);
29
30 pane.add(Pane,BorderLayout.PAGE_END);
31 pane.add(cards,BorderLayout.CENTER);
32 }
33
34 public void actionPerformed(ActionEvent ae) {
35 if (ae.getActionCommand().equals("first")) {
36 CardLayout cl = (CardLayout)(cards.getLayout());
37 cl.show(cards,CARD2);
38 }
39 else if (ae.getActionCommand().equals("second")) {
40 CardLayout cl = (CardLayout)(cards.getLayout());
41 cl.show(cards,CARD3);
42 }
43 else if (ae.getActionCommand().equals("third")) {
44 CardLayout cl = (CardLayout)(cards.getLayout());
45 cl.show(cards,CARD1);
46 }
47 }
48
49 private static void createAndShowGUI() {
50 frame = new JFrame("Test");
51 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
52
53 Test test = new Test();
54 test.addComponentToPane(frame.getContentPane());
55
56 frame.setSize(300,300);
57 frame.setVisible(true);
58 }
59
60 public static void main(String[] args) {
61 javax.swing.SwingUtilities.invokeLater(new Runnable() {
62 public void run() {
63 createAndShowGUI();
64 }
65 });
66 }
67 }
68
69 class FirstPanel extends JPanel {
70 JButton firstBtn;
71
72 JLabel firstLbl;
73
74 Test tt = new Test();
75
76 FirstPanel() {
77 setOpaque(true);
78 setPreferredSize(new Dimension(300,300));
79
80 firstLbl = new JLabel("this is the first panel");
81
82 firstBtn = new JButton("first");
83 firstBtn.setActionCommand("first");
84 firstBtn.addActionListener(Test);
85
86 add(firstLbl);
86 add(firstLbl);
87 add(firstBtn);
88 }
89 }
90
91 class SecondPanel extends JPanel {
92 JButton secondBtn;
93
94 JLabel secondLbl;
95
96 //Test tt = new Test();
97
98 SecondPanel() {
99 setOpaque(true);
100 setPreferredSize(new Dimension(300,300));
101
102 secondLbl = new JLabel("this is the second panel");
103
104 secondBtn = new JButton("second");
105 secondBtn.setActionCommand("second");
106 //secondBtn.addActionListener(tt);
107
108 add(secondLbl);
109 add(secondBtn);
110 }
111 }
112
113 class ThirdPanel extends JPanel {
114 JButton thirdBtn;
115
116 JLabel thirdLbl;
117
118 //Test tt = new Test();
119
120 ThirdPanel() {
121 setOpaque(true);
122 setPreferredSize(new Dimension(300,300));
123
124 thirdLbl = new JLabel("this is the third panel");
125
126 thirdBtn = new JButton("third");
127 thirdBtn.setActionCommand("third");
128 //thirdBtn.addActionListener(tt);
129
130 add(thirdLbl);
131 add(thirdBtn);
132 }
133 }
- 05-25-2011, 01:25 AM #5
-
- 05-25-2011, 02:39 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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:
Java 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); } } }
- 05-25-2011, 10:43 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Two of the classes you posted begin like this:
andJava Code: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(); // etc
There is a circularity here: once you try and construct an instance of Test you must construct an instance of FirstPanel and vice versa.Java Code:class FirstPanel extends JPanel { JButton firstBtn; JLabel firstLbl; Test tt = new Test(); // etc
I would have expected a StackOverflowError in these circumstances.
----------------
It's the "new" that does the damage here. There's nothing wrong with each class having an instance variable of the type of the other: it's when you try to initialise such variables to new values that you have an infinite regress. Rather than do this you could pass arguments to the constructors that are used to initialise the variables.
- 05-26-2011, 12:20 AM #9
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Thanks a million!
Thanks for the explanation, pbrockway2! It makes perfect sense now, and I have fixed the error.
Once again, the members of Java Forum have come through. It is a great resource!:D
- 05-26-2011, 05:12 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
event handling between classes
By newbie123 in forum New To JavaReplies: 3Last Post: 03-25-2010, 12:38 PM -
Event handling in JSF
By java08 in forum JavaServer Faces (JSF)Replies: 0Last Post: 03-24-2009, 06:42 AM -
rmi and event handling
By darkhorse in forum Advanced JavaReplies: 0Last Post: 03-15-2009, 08:20 AM -
How to get objects to interact without inheritence in separate classes?
By Fliz in forum New To JavaReplies: 1Last Post: 11-18-2008, 04:48 PM -
SWT Event Handling
By Java Tip in forum Java TipReplies: 0Last Post: 12-30-2007, 12:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks