Results 1 to 13 of 13
  1. #1
    SpicyHero is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Question Need some more GUI help...

    In my program, the user just enters his name and then can choose a color for his name to display in. But when I click on my button nothing happens.
    Java Code:
    import java.awt.Component;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Color;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
     
    public class thomas extends JFrame
    {     
        public static final Component Testing123 = null;
    	private JTextField EnterName;
        private JButton GreenButton;
        private Color CurrentColor;
        
        public thomas()
        {
            super("Name Colors");
            setLayout(new GridBagLayout());
            
    		Icon green = new ImageIcon(getClass().getResource("green.png"));
    
    		GreenButton = new JButton(null,green);
    		GreenButton.setLocation(500,300);
    		
    		
    		CurrentColor = Color.gray;
    		EnterName = new JTextField("Enter Name Here");
            JLabel CurrentName = new JLabel("No name yet.");
            CurrentName.setForeground(CurrentColor);
            
            thehandler handler = new thehandler(CurrentName);
            thehandler handler2 = new thehandler(GreenButton);
            
            EnterName.addActionListener(handler);
            GreenButton.addActionListener(handler2);
            
            add(EnterName);
            add(CurrentName);
            add(GreenButton);
        }
        private class thehandler implements ActionListener
        {
    
    		private JLabel CurrentName;
    		private JButton GreenButton;
             
            public thehandler(JLabel CurrentName)
            {
                this.CurrentName = CurrentName;
            }
    		public thehandler(JButton GreenButton) 
    		{
    			this.GreenButton = GreenButton;
    		}
    
    		public void actionPerformed(ActionEvent event)
            {
                String Name = "Name";
                if(event.getSource() == EnterName)
                {
                    Name = String.format(" %s", event.getActionCommand());
                    CurrentName.setText("Current Name is:"+Name);
                    JOptionPane.showMessageDialog(null,"You have set your name to"+Name);
                    add(Testing123);
                }
                else if(event.getSource() == GreenButton)
                {
                	CurrentColor = Color.green;
                	add(CurrentName);
                }
            }
        }
    }
    Also when I did "GreenButton.setLocation(500,300);" It doesn't go to that location. I'm appreciate it if anyone could help me. Thanks!
    Last edited by SpicyHero; 09-24-2013 at 05:40 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Need some more GUI help...

    First of all, you should obey the standard naming conventions. Classes and constructors start with an upper-case letter, methods and variables start with a lower-case letter.

    If you want help, you'll have to provide an SSCCE. Note that this isn't your whole program, but isn't a disconnected snippet either.

    But just glancing at your code, I can give you this hint: Changing the value that a variable points to does not go back in time and update every place that has used that value before. For example, if you have this:


    Java Code:
    String message = "hello";
    JLabel label = new JLabel("message");
    message = "goodbye";
    ...the JLabel will only display "hello", not "goodbye".
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some more GUI help...

    No it won't; it'll display 'message' ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Need some more GUI help...

    Quote Originally Posted by JosAH View Post
    No it won't; it'll display 'message' ;-)

    kind regards,

    Jos
    D'oh! Haven't had enough caffeine today. What I *meant* to say was:

    Java Code:
    String message = "hello";
    JLabel label = new JLabel(message);
    message = "goodbye";
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    55
    Rep Power
    0

    Default Re: Need some more GUI help...

    Quote Originally Posted by KevinWorkman View Post
    D'oh! Haven't had enough caffeine today. What I *meant* to say was:

    Java Code:
    String message = "hello";
    JLabel label = new JLabel(message);
    message = "goodbye";

    C'mon, we all know that you were just checking to see if we were paying attention, Weren't you

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some more GUI help...

    Quote Originally Posted by KevinWorkman View Post
    D'oh! Haven't had enough caffeine today.
    That'd be a crime against humanity; did you notify Amnesty International or the Animal Protection Services?

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    SpicyHero is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Need some more GUI help...

    So how can I update the my CurrentColor? Should I just re-add my CurrentName in the if statement?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Need some more GUI help...

    Quote Originally Posted by SpicyHero View Post
    So how can I update the my CurrentColor? Should I just re-add my CurrentName in the if statement?
    What happened when you tried that?

    You seem to already be using the JLabel API for changing the color. Can't you just do the same thing again?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    SpicyHero is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Need some more GUI help...

    Now I'm getting this console error when I click my button.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at thomas$thehandler.actionPerformed(thomas.java:77)
    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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(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)

    I have no idea how to fix this now. I'll probably start on a new program.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Need some more GUI help...

    It will be much easier to help you if you post an SSCCE with your updated code.

    But a NullPointerException is a pretty basic error. Look at the line it's talking about (line 77 of thomas, which again should really be Thomas). Something is null on that line. Step through it with a debugger or add some print statements to narrow down exactly what it is.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    SpicyHero is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Need some more GUI help...

    I've been trouble shooting for about an hour. I think it's my action listener. I really have no clue. I'll work on the SSCCE version.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Need some more GUI help...

    Look at line 77 of your Thomas class, which looks like it's inside an actionPerformed() method. You're trying to use a variable holding a null value, which happens when you forget to initialize an object.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    SpicyHero is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Need some more GUI help...

    Okay, now I replaced my
    Java Code:
    private JLabel CurrentName;
    to
    Java Code:
    "JLabel CurrentName = new JLabel("No name yet.");
    And the console message doesn't come up anymore! :D But the button still doesn't do anything.
    Last edited by SpicyHero; 09-24-2013 at 06:17 PM.

Posting Permissions

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