Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: ACTION LISTENER HANDLING

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ACTION LISTENER HANDLING

    caN ANYONE HELP ME IN RUNNING MY CODE SO THAT WHENVER I PRESS ANY BUTTON IT DISPLAYS MESSSAGE IN TEXT FIELD...

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     
     
     
    class NewClass1{
    public static void main(String args[]){
      JFrame f =new JFrame("faria");
           JTextField t=new JTextField(10);
                JLabel l=new JLabel("");
                JButton j= new JButton("a");
                JButton j1= new JButton("b");
                JButton j2= new JButton("c");
                l.setLayout(new FlowLayout());
                f.add(l);
    l.add(t);
                l.add(j);
                 l.add(j1);
                  l.add(j2);
    j.addActionListener(new A());
     
    j1.addActionListener(new B());
    j2.addActionListener(new C());
    f.setVisible(true);
    f.setSize(400,400);
     
     
     
     
    }
    }
    class A implements ActionListener{
       public void actionPerformed(ActionEvent e) {
          //  counter++;
     
     
             System.out.println("hiii" );
     
       }
            }
    class B implements ActionListener{
     
       public void actionPerformed(ActionEvent e) {
     
                System.out.println("hello" );
     
       }
            }
     
       class C implements ActionListener{
       public void actionPerformed(ActionEvent e) {
     
                System.out.println("how r u" );
       }
     
            }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: ACTION LISTENER HANDLING

    Hello fari,

    Welcome to the Java Programming Forums

    Please take a look at this link, im sure it will help.

    http://www.javaprogrammingforums.com...ava-swing.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ACTION LISTENER HANDLING

    Or instead of using anonymous classes you could define the classes A, B, etc to implement the ActionListener interface and put the actionPerformed() method in them.

  4. #4
    Member
    Join Date
    May 2010
    Posts
    38
    Thanks
    1
    Thanked 8 Times in 7 Posts

    Default Re: ACTION LISTENER HANDLING

    And mind not using all caps? I know you need help but screaming isn't going to make it come any faster, as a matter of fact it might put people off

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     import java.awt.Component;
     import java.awt.Container;
        import javax.swing.JPanel;
    	   import java.awt.*;
       import java.awt.event.*;
    	import java.io.*;
    	import javax.swing.AbstractButton;
    	import java.util.*;
     
     
     
     
    class NewClass1 extends JFrame implements ActionListener{
     
     JTextField t;
     
     
     JLabel l;
     
    private JButton j, j1, j2;
     
    JFrame f;
     
     
    public NewClass1()
    {
     // JFrame f =new JFrame("faria");
           JTextField t= new JTextField(15);
                JLabel l=new JLabel("Label");
                JButton j= new JButton("a");
    				j.addActionListener(this);
                JButton j1= new JButton("b");
    				j1.addActionListener(this);
                JButton j2= new JButton("c");
    				j2.addActionListener(this);
    				t.setText("5");
             //    l.setLayout(new FlowLayout());
                // f.add(l);
    // l.add(t);
             //   l.add(j);
              //   l.add(j1);
             //     l.add(j2);
     
    				   setTitle("farris");
    				   Container pane = getContentPane();
     
    				   pane.add(l);
    				 pane.add(t);
    				  pane.add(j);
    				  pane.add(j1);
    		      pane.add(j2);
     
     
     
    // j.addActionListener(new A());
     
    // j1.addActionListener(new B());
    // j2.addActionListener(new C());
     pane.setLayout(new GridLayout(4,4));
    setVisible(true);
    setSize(400,400);
     setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
     
    }
     
    public static void main(String args[]){
     NewClass1 refVar = new NewClass1();
     
    }
     
    public void actionPerformed(ActionEvent e)
    {
     
          if (e.getActionCommand().equals("a"))
          {
             System.out.println("hiii" );
    			t.setText("hi");
    			}
          else if (e.getActionCommand().equals("b"))
    		{
                 System.out.println("hello" );
           t.setText("hello");
       }
     
     
       else if (e.getActionCommand().equals("c"))
    	{
                System.out.println("how r u" );
    				t.setText("how r u");
       }
     
            }
     
    		  }

    Code should work but for mysterious NullPointerExceptions.

    at NewClass1.actionPerformed(NewClass1.java:98)
    which is
    t.setText("how r u");

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ACTION LISTENER HANDLING

    What is the scope of the t variable assigned a value in the Constructor?

  7. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 12th, 2010)

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    What was scope again? I forgot.

    Do I have to assign it coordinates or something?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ACTION LISTENER HANDLING

    What text book are you using?
    I bet you could get a definition using Google.

    Nothing to do with coordinates.

  10. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 12th, 2010)

  11. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    I think scope is whether or not Java has access to something, or can find it at least.

    Why isn't it finding it?

    I'm baffled.

  12. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    Ok, it's not carrying over. Should I change it to 3 handlers?

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ACTION LISTENER HANDLING

    Why isn't it finding it
    The compiler is finding it, otherwise there would be an error message about missing definition.
    The one its using at that point in the code has a null value when the code is executed.
    Where does your code give it a value? It Never does!!!

  14. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 12th, 2010)

  15. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    Quote Originally Posted by Norm View Post
    The compiler is finding it, otherwise there would be an error message about missing definition.
    The one its using at that point in the code has a null value when the code is executed.
    Where does your code give it a value? It Never does!!!

    What do you mean by "give it a value"?

    Give what a value?

    The button has a text.

  16. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ACTION LISTENER HANDLING

    What do you mean by "give it a value"?
    x = 4; // Give the value 4 to the variable x

    In your program we were talking about the variable t.

    Remember:
    mysterious NullPointerExceptions.
    at NewClass1.actionPerformed(NewClass1.java:98)
    which is
    t.setText("how r u");

  17. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    Quote Originally Posted by Norm View Post
    x = 4; // Give the value 4 to the variable x

    In your program we were talking about the variable t.

    Remember:
    t is a text field, not a variable.

  18. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ACTION LISTENER HANDLING

    t is a text field, not a variable
    Well not really. t is a variable that refers/points to a text field. It really is a pointer because you get the NullPointerException when you try to use it when its null. It is possible to have many variables point/refer to the same object. Many variables, one object.

  19. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    Quote Originally Posted by Norm View Post
    Well not really. t is a variable that refers/points to a text field. It really is a pointer because you get the NullPointerException when you try to use it when its null. It is possible to have many variables point/refer to the same object. Many variables, one object.
    So I set it to four?

    So really, what do I do?

    Last edited by javapenguin; June 12th, 2010 at 08:21 PM.

  20. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ACTION LISTENER HANDLING

    The variable t must be given the address of a textfield. See how you did it in the Constructor.

  21. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    It is a text field.

    private JTextField t;

    Constructor
    {

    t = new JTextField(15);
    t.setText("5"); // in case that was the cause, not having it set to anything, but it doesn't appear to be.
    Container pane = getContentPane();

    pane.add(t);

    }

  22. #19
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    Well, I made a new textfield called t in the actionPerformed, and the stupdi exception problem went away, though it won't go change text in text field. Now I know that somehow it isn't recognizing the text field, but somehow isn't throwing a compiler error.

  23. #20
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ACTION LISTENER HANDLING

    Wait never mind, I fixed it, the program code that'll work is:

     import java.awt.FlowLayout;
       import java.awt.event.ActionEvent;
       import java.awt.event.ActionListener;
       import javax.swing.JButton;
       import javax.swing.JFrame;
       import javax.swing.JLabel;
       import javax.swing.JTextField;
       import java.awt.Component;
       import java.awt.Container;
       import javax.swing.JPanel;
       import java.awt.*;
       import java.awt.event.*;
       import java.io.*;
       import javax.swing.AbstractButton;
       import java.util.*;
    	import javax.swing.*;
     
     
     
     
        class NewClass1 extends JFrame implements ActionListener{
     
          private JTextField t;
     
     
          JLabel l;
     
          private JButton j, j1, j2;
     
       // JFrame f;
     
     
           public NewClass1()
          {
          // JFrame f =new JFrame("faria");
              t= new JTextField(15);
             l = new JLabel("Label: ",
                                      SwingConstants.RIGHT);	
             JButton j= new JButton("a");
             j.addActionListener(this);
             JButton j1= new JButton("b");
             j1.addActionListener(this);
             JButton j2= new JButton("c");
             j2.addActionListener(this);
             //    l.setLayout(new FlowLayout());
                // f.add(l);
          // l.add(t);
             //   l.add(j);
              //   l.add(j1);
             //     l.add(j2);
     
             setTitle("farria");
             Container pane = getContentPane();
     
             pane.add(l);
             pane.add(t);
             pane.add(j);
             pane.add(j1);
             pane.add(j2);
     
     
     
          // j.addActionListener(new A());
     
          // j1.addActionListener(new B());
          // j2.addActionListener(new C());
             pane.setLayout(new GridLayout(4,4));
             setVisible(true);
             setSize(400,400);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
     
          }
     
           public static void main(String args[]){
             NewClass1 refVar = new NewClass1();
     
          }
     
           public void actionPerformed(ActionEvent e)
          {
         // JTextField t = new JTextField();
             if (e.getActionCommand().equals("a"))
             {
                System.out.println("hiii" );
                t.setText("hi");
             }
             else if (e.getActionCommand().equals("b"))
             {
                System.out.println("hello" );
                t.setText("hello");
             }
     
     
             else if (e.getActionCommand().equals("c"))
             {
                System.out.println("how r u" );
                t.setText("how r u");
             }
     
          }
     
       }
    Last edited by javapenguin; June 12th, 2010 at 08:35 PM.

Similar Threads

  1. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  2. Beginner Help (Action Listener)
    By gradstudent in forum AWT / Java Swing
    Replies: 2
    Last Post: April 30th, 2010, 10:26 AM
  3. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  4. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM