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 5 of 5

Thread: ActionListener: use two bottuns with getSource() doesn't work

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default ActionListener: use two bottuns with getSource() doesn't work

    This is a simple code that create a litener to a button, actually I have two buttons
    click_1 and click_2



    private class ButtonWatcher implements ActionListener
       {
          public void actionPerformed(ActionEvent a)
          {
     
             [COLOR="Blue"]Object [COLOR="DarkOrange"]buttonPressed = a.getSource();[/COLOR]
     
             if ([COLOR="darkorange"]buttonPressed[/COLOR].equals([COLOR="Green"]click_1[/COLOR]))
             {
                txtArea.setText(" [COLOR="Green"]Click_1 is pressed [/COLOR]");
             }
     
             if ([COLOR="DarkOrange"]buttonPressed[/COLOR].equals([COLOR="green"]click_2[/COLOR]))
             {
                txtArea.setText(" [COLOR="green"]click_2 is pressed [/COLOR]");
             }
          }
       }
    [/COLOR]

    and I add the liteners to the buttons:
          [COLOR="Green"]click_1[/COLOR].addActionListener(new ButtonWatcher());
          [COLOR="green"]click_2[/COLOR].addActionListener(new ButtonWatcher());

    As you know the use of:
    Object buttonPressed = a.getSource();

    will get the source of the pressed button, and hence the related code will be implemented using the if-statements

    But, non of the buttons produce any output to the JTextArea object

    but each button can do its job normally when it is the only one to be used.. I mean such as this


    [COLOR="black"]private class ButtonWatcher implements ActionListener
       {
          public void actionPerformed(ActionEvent a)
          {
     
                txtArea.setText(" Click_1 is pressed ");
     
          }[/COLOR]
       }

    click_1.addActionListener(new ButtonWatcher());



    So why the use of getScource() and if statements to use two buttons doesn't work .. what I miss


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: ActionListener: use two bottuns with getSource() doesn't work

    Can you post the full code that reproduces the problem? The code you posted doesn't seem like it should behave as you describe, making me think the problem lies outside of the code you posted.

  3. The Following User Says Thank You to copeg For This Useful Post:

    voltaire (May 14th, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener: use two bottuns with getSource() doesn't work

    Quote Originally Posted by copeg View Post
    Can you post the full code that reproduces the problem? The code you posted doesn't seem like it should behave as you describe, making me think the problem lies outside of the code you posted.
    I didn't provide the full code because I wnat to focus on the area of the problem (as I think it is).

    Here is the full simple code:


    [COLOR="Black"]
     
    [COLOR="Blue"]package [/COLOR]numbergeneratorchecker;
     
    [COLOR="blue"]import [/COLOR]java.awt.*;
    [COLOR="blue"]import [/COLOR]javax.swing.*;
    [COLOR="blue"]import [/COLOR]java.awt.event.*;
    [COLOR="blue"]import [/COLOR]java.io.*;
     
    [COLOR="blue"]public [/COLOR][COLOR="blue"]class [/COLOR]NewClass [COLOR="blue"]extends [/COLOR]JFrame
    {
     
       [COLOR="blue"]private [/COLOR]JButton click_1;
       [COLOR="Blue"]private [/COLOR]JButton click_2;
     
       [COLOR="blue"]private [/COLOR]Panel pn1;
       [COLOR="blue"]private [/COLOR]Panel pn2;
       [COLOR="blue"]private [/COLOR]JTextArea txtArea;
       [COLOR="blue"]private [/COLOR]JScrollPane scrollTxtArea;
     
     
       [COLOR="blue"]public [/COLOR]NewClass()
       {
          setSize(300, 200);
          setTitle("Number Gererator");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
     
          /********** 2 panels ******************/
          pn1 = [COLOR="blue"]new [/COLOR]Panel();
          pn2 = [COLOR="blue"]new [/COLOR]Panel();
     
          /********** 2 button ******************/
          JButton click_1 = [COLOR="blue"]new [/COLOR]JButton("click_1");
          JButton click_2 = [COLOR="blue"]new [/COLOR]JButton("click_2");
     
          /********** text area ******************/
          txtArea = [COLOR="blue"]new [/COLOR]JTextArea(2, 22);
          scrollTxtArea = [COLOR="blue"]new [/COLOR]JScrollPane(txtArea);
     
     
     
     
     
          /***********  Interface  *******/
          Container cp = getContentPane();
          cp.setLayout([COLOR="blue"]new [/COLOR]GridLayout(3,1));
     
          /***** 2 panels ****/
          cp.add(pn1);
          cp.add(pn2);
     
     
          click_1.addActionListener([COLOR="blue"]new [/COLOR]ButtonWatcher());
          click_2.addActionListener([COLOR="blue"]new [/COLOR]ButtonWatcher());
     
          /********** button ******************/
          pn1.add(click_1);
          pn1.add(click_2);
     
          /******* text area ******/
          pn2.add(scrollTxtArea);
     
       }
       [COLOR="blue"]private class [/COLOR]ButtonWatcher [COLOR="blue"]implements [/COLOR]ActionListener
       {
          [COLOR="blue"]public void [/COLOR]actionPerformed(ActionEvent a)
          {
     
             Object buttonPressed = a.getSource();
     
             [COLOR="blue"]if[/COLOR] (buttonPressed.equals(click_1))
             {
                txtArea.setText(" Click_1 is pressed ");
             }
             [COLOR="blue"]if[/COLOR] (buttonPressed.equals(click_2))
             {
                txtArea.setText(" click_2 is pressed ");
             }
          }
       }
    }[/COLOR]


    [COLOR="Black"][COLOR="Blue"]package [/COLOR]numbergeneratorchecker;
     
     
    [COLOR="blue"]public [/COLOR]class Main
    {
     
       [COLOR="Blue"]public static void [/COLOR]main(String[] args)
       {
      NewClass ss = [COLOR="blue"]new [/COLOR]NewClass();
      ss.setVisible([COLOR="Blue"]true[/COLOR]);
       }
     
    }[/COLOR]

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: ActionListener: use two bottuns with getSource() doesn't work

    Posting the rest of that code sheds much light on the problem. The issue lies in your creation of the two buttons:

          /********** 2 button ******************/
          JButton click_1 = new JButton("click_1");
          JButton click_2 = new JButton("click_2");

    The scope of click_1 and click_2 is within the constructor, not outside. As a result, both instance variables click_1 and click_2 were never instantiated. The fix is to instantiate them in your constructor rather than assigning local variables with the same names:
          /********** 2 button ******************/
          click_1 = new JButton("click_1");
          click_2 = new JButton("click_2");

  6. The Following User Says Thank You to copeg For This Useful Post:

    voltaire (May 14th, 2010)

  7. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener: use two bottuns with getSource() doesn't work

    Quote Originally Posted by copeg View Post
    Posting the rest of that code sheds much light on the problem. The issue lies in your creation of the two buttons:

          /********** 2 button ******************/
          JButton click_1 = new JButton("click_1");
          JButton click_2 = new JButton("click_2");

    The scope of click_1 and click_2 is within the constructor, not outside. As a result, both instance variables click_1 and click_2 were never instantiated. The fix is to instantiate them in your constructor rather than assigning local variables with the same names:
          /********** 2 button ******************/
          click_1 = new JButton("click_1");
          click_2 = new JButton("click_2");

    Thank you copeg , that was a tricky problem

Similar Threads

  1. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 11:25 AM
  2. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM
  3. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 07:21 PM
  4. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 10:00 AM
  5. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM