Page 2 of 2 FirstFirst 12
Results 21 to 40 of 40
  1. #21
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Hi, thanks for pointing that out. I know that but I was changing the names before I posted it on here as a result of my earlier concerns as stated in my first message.

  2. #22
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    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);
    	}
    
    }
    I hope this helps.

  3. #23
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    So what are the exceptions you are seeing?

  4. #24
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    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)

  5. #25
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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...
    }

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  7. #27
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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.

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Oh...of course...I knew that...oh yes.

    I was just testing you all...

    *ahem*

  9. #29
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Oh wait I screwed up again lol, it does, my bad.

    So yeah just use Login.this for the constructor call.

  10. #30
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I'm quite capable of making myself look foolish, you know...
    ;)

  11. #31
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I believe we all are lol

  12. #32
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    I'm sorry I couldn't explain it better.
    So where does Login.this go please?

  13. #33
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    I've never used anonymous classes but I'll read about it.

  14. #34
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Just switch the constructor call from
    Java Code:
    new AnotherGUI(this)
    to
    Java Code:
    new AnotherGUI(Login.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.

  15. #35
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  16. #36
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Or, to put it another way...what he said...^
    :)

  17. #37
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    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?

  18. #38
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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:
    Java Code:
            JButton jb = new JButton();
            jb.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    handleJBButtonAction();
                }
            });
    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.

    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.

  19. #39
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by StormyWaters View Post
    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.
    This looks remarkably like my reply #14 way back on the first page. ;)

  20. #40
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Calling on a class
    By gandalf5166 in forum New To Java
    Replies: 12
    Last Post: 03-24-2010, 11:58 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 03:48 AM
  3. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 02:12 AM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 08:35 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •