10 Replies Latest reply: Jan 14, 2013 1:15 AM by Gowtham Gutha RSS

    Write custom ActionListener

    978140
      Hi. I have a class with class looking like this:
      public MyClass extends JPanel{
         JComboBox<String> box1 = new JComboBox<String>();
         JComboBox<String> box2 = new JComboBox<String>();
      }
      Then I have a class containing this class:
      public GUI extends JPanel implements ActionListemer {
        
        public GUI(){
          MyClass myObject = new MyClass();
          // What I would like to do now:
          myObject.addActionListener();
        }
      
        public void actionPerformed(ActionEvent e) {
          // ....
        }
      }
      Is it possible for me to somehow implement the method addActionListener() in "MyClass"? How would this look?
        • 1. Re: Write custom ActionListener
          gimbal2
          EDIT: I'm too quick. Perhaps you were looking for this:
           myObject.addActionListener(this);
          • 2. Re: Write custom ActionListener
            linkin
            Hunter_wow wrote:
            Is it possible for me to somehow implement the method addActionListener() in "MyClass"? How would this look?
            Why you try to implement the addActionListener()?
            • 3. Re: Write custom ActionListener
              978140
              Yes, you are correct. But my main problem is that "MyObject" do not contain the method addActionListener(Object o). Do I need to write addActionListener(Object o) in "MyClass" somehow?

              Edit: box1 and box2 are start and end values. That mean they need to check each other when one of them are changed. Thats why I would like to have an actionListener(this) that runs for both my boxes in MyClass

              Edited by: Hunter_wow on 2013-jan-09 01:06
              • 4. Re: Write custom ActionListener
                EJP
                But my main problem is that "MyObject" do not contain the method addActionListener(Object o).
                No it isn't. Why do you think that?
                • 5. Re: Write custom ActionListener
                  gimbal2
                  You're doing something wrong then, since your MyClass is apparently implementing ActionListener and that is what the addActionListener() method wants.

                  addActionListener() is only going to work when you pass objects that actually implement the ActionListener interface.
                  • 6. Re: Write custom ActionListener
                    978140
                    MyClass only extends JPanel. So it would be the same as typing:
                    JPanel panel = new JPanel();
                    panel.addActionListener(this); // ERROR. Do not exists
                    JPanel: http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JPanel.html
                    • 7. Re: Write custom ActionListener
                      978140
                      Guess the correct answer is to do something like this:

                      myObject.box1.addActionListener(this);
                      myObject.box2.addActionListener(this);

                      Thanks for making me think =)
                      • 8. Re: Write custom ActionListener
                        gimbal2
                        Hunter_wow wrote:
                        Guess the correct answer is to do something like this:

                        myObject.box1.addActionListener(this);
                        myObject.box2.addActionListener(this);

                        Thanks for making me think =)
                        EDIT: indeed, input components have an addActionListener(), not JPanel. I'm so rusty that I just completely missed that. You can still make a JPanel clickable by using mouse listeners in stead of an action listener, should you in the future be looking for something like that.

                        Best source of guidance are the Javadocs by the way.

                        http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html

                        Those are a whole bunch of inherited methods, but you can simply do a search in the browser for the method you're looking for to know the truth (it ain't there).
                        • 9. Re: Write custom ActionListener
                          841361
                          Events listeners are added on the source component and they eventually generates events on some user action.
                          In your case the combo boxes works as source ..so you can add the same action listener on both the combo boxes ,and in the action performed method
                          you can check that from which source (combo box 1 or combo box2 ) the event is generated and take the action accordingly.
                          • 10. Re: Write custom ActionListener
                            Gowtham Gutha
                            myObject.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent ae)
                            {
                            }
                            });

                            You can use anonymous inner class instead of writing a new class. For this, you need not implement the ActionListener interface.

                            Here MyClass need to implement the ActionListener interface and then write addActionListener(ActionListener) in the MyClass (according to you)