Results 21 to 40 of 40
- 06-10-2010, 03:45 PM #21
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
- 06-10-2010, 03:46 PM #22
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
I hope this helps.Java Code:package one; import java.awt.*; import javax.swing.*; public class AnotherGUI extends JFrame { private JLabel nameLabel; private JLabel genderLabel; private JTextField nametextField; private JTextField gendertextField; public AnotherGUI(Login login){ super(login.getUserName()); Container pane = getContentPane(); pane.setLayout(new FlowLayout()); Font monospacedFont = new Font("Monospaced", Font.PLAIN, 14); //Instantiates Labels, TextFields and Buttons nameLabel = new JLabel("Name: "); nameLabel.setFont(monospacedFont); nametextField = new JTextField(2); genderLabel = new JLabel ("Gender: "); genderLabel.setFont(monospacedFont); gendertextField = new JTextField(6); //Adds the labels, textFields, and buttons to the pane pane.add(nameLabel); pane.add(nametextField); pane.add(genderLabel); pane.add(gendertextField); pack(); setVisible(true); } }
- 06-10-2010, 03:55 PM #23
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
So what are the exceptions you are seeing?
- 06-10-2010, 03:59 PM #24
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The constructor AnotherGUI(Login.ButtonHandler) is undefined
at one.Login$ButtonHandler.actionPerformed(Login.java :101)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 06-10-2010, 04:05 PM #25
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Doh!
Silly me. I so rarely use anything other than anonymous classes for listeners that I completely missed that "this" would refer to the ButtonHandler and not the Login.
Either go for an anonymous inner class for the listener (if you know what they are), or pass the Login into the ButtonHandler constructor (which will involve adding a constructor to the ButtonHandler that takes a Login). That'll be stored (as in AnotherGui), which will be passed to AnotherGui instead of "this".
ETA: In case that's a bit too much writing, this is what I mean:
Java Code:class ButtonHandler blah blah { Login login; // pass this to the AnotherGUi constructor instead of "this". ButtonHandler(Login login) { this.login = login; } ...other stuff... }
- 06-10-2010, 04:09 PM #26
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Double doh!
It's still an inner class isn't it?
Ignore most of my stuff then...:)
Apart from the anonymous listener if you understand them...they make far more sense, to me anyway.
- 06-10-2010, 04:12 PM #27
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
No the doh was all mine lol,
I didn't look to see if it was anonymous or not, lol. I was intrigued by you're reply and I was unsure if just an inner class would work with Login.this, so I tested it and it doesn't.
- 06-10-2010, 04:15 PM #28
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Oh...of course...I knew that...oh yes.
I was just testing you all...
*ahem*
- 06-10-2010, 04:15 PM #29
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Oh wait I screwed up again lol, it does, my bad.
So yeah just use Login.this for the constructor call.
- 06-10-2010, 04:16 PM #30
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I'm quite capable of making myself look foolish, you know...
;)
- 06-10-2010, 04:20 PM #31
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
I believe we all are lol
- 06-10-2010, 04:27 PM #32
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
I'm sorry I couldn't explain it better.
So where does Login.this go please?
- 06-10-2010, 04:27 PM #33
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
I've never used anonymous classes but I'll read about it.
- 06-10-2010, 04:31 PM #34
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Just switch the constructor call from
toJava Code:new AnotherGUI(this)
The reasoning behind this is that the "this" is referring to your ButtonHandler.class. However since the ButtonHandler is inside the Login class, you can specify it to use the Login class by doing Login.this.Java Code:new AnotherGUI(Login.this)
- 06-10-2010, 04:32 PM #35
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Instead of the "this" passed into the constructor:
new AnotherGUI(this);
pass Login.this.
Because ButtonHandler is an inner class to Login, "this" refers to the ButtonHandler object we're in at the time. To get at the Login object we are a part of, we need to use Login.this.
- 06-10-2010, 04:33 PM #36
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Or, to put it another way...what he said...^
:)
- 06-10-2010, 04:37 PM #37
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
You guys are the best. Sorry for the trouble and thanks for your help. I will look up anonymous classes. Is that the best thing to use for listeners?
- 06-10-2010, 04:52 PM #38
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
If your listener is essentially doing one thing on one object (button in this case) then I find it better to assign an anonymous listener to the button which then calls a method in the class itself...unless the action is only a couple of lines.
So:
Where handleJBButtonAction() will do the work. As I say, if the work is only a couple of lines that bit is unecessary. What you don't want to do is stick loads of code in there as it becomes messy really quickly.Java Code:JButton jb = new JButton(); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { handleJBButtonAction(); } });
This ties the listener code to the button in a (fairly) clear way. As with everything, though, YMMV. If you had a load of listeners on a single object then you may end up with a mess as well.
-
- 06-11-2010, 08:54 AM #40
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Calling on a class
By gandalf5166 in forum New To JavaReplies: 12Last Post: 03-24-2010, 10:58 PM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a class method from another class
By caro in forum New To JavaReplies: 4Last Post: 06-10-2009, 01:12 AM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks