Results 1 to 17 of 17

Thread: User Interface

  1. #1
    swikar.java is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default User Interface

    Can anybody tell why use addActionlistner(this); This is like method.
    I cannot understand of use ''this'' word.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you look up the API for the addActionListener (I think this can be found in the AbstractButton API), you'll see that it requires that you pass an object into it that implements the ActionListener interface, and this can be any object that implements this interface. In your current situation, the current object, the one whose code you are in, happens to implement this interface (look up at the top of the class and you'll see this). So when you pass "this" to this method, you are passing the current object into the button's action listener list.

    Whenever this button is pressed, it iterates through its actionlistener list and calls the actionPerformed() method of each ActionListener object in this list. Since the current object is in that list (and in fact may be the only object in that list), whenever the button is pressed, the current object's actionPerformed() method will be called.

    Is that clear as Mudd? Please reply if this doesn't make sense to you and we can try to break this down.

    edit: a very simple example:
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class ActionListenerExample implements ActionListener
    {
      
      JButton myButton = new JButton("My Button");
      
      public ActionListenerExample()
      {
        myButton.addActionListener(this);
        // now when "myButton" is pressed, "this"'s actionPerformed method will be called
      }
      
      // all classes that implement ActionListener *MUST* have this method
      public void actionPerformed(ActionEvent e)
      {
        System.out.println("Hello from the actionlistener!");
      }
      
      public JButton getMyButton()
      {
        return myButton;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("example");
        frame.getContentPane().add(new ActionListenerExample().getMyButton());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    Last edited by Fubarable; 10-22-2008 at 02:11 PM.

  3. #3
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Incidentally, the old addActionListener(this) routine often indicates a poor design: the "telephone switchboard" all-purpose action listener. Doubtless there's a single object handling all UI events, and constantly querying those events to see which widget spewed them out and acting accordingly. Quickly becomes a maintenance nightmare. A far better approach is to add an individual action listener to each widget, typically as an anonymous inner class

  4. #4
    mohkhamis is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    "this"is to call another constructor in the same class for calling

  5. #5
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by mohkhamis View Post
    "this"is to call another constructor in the same class for calling
    That is only one use of the keyword 'this', and not the one the OP was asking about

  6. #6
    mohkhamis is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    sorry im new in java buti m trying

  7. #7
    mohkhamis is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    im now learining java how long it will take to devlop a useful program if im everyday studying

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by mohkhamis View Post
    im now learining java how long it will take to devlop a useful program if im everyday studying
    It's depends on you lol, depends on your skills/ability to learn new things in short period of time. To build a useful application you should have a useful design. So the answer is in your hand.

    Do you familiar with any programming language? It's really helpful to you. And object oriented programming too.

  9. #9
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by mohkhamis View Post
    im now learining java how long it will take to devlop a useful program if im everyday studying
    Studying every day is good, but you'll get there quicker if you have something specific in mind you wish to develop. Preferably something related to a hobby or interest you have.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I agreed with you georgemc. Better to start work on with a complete design, a real one. So you can, you have to learn a lot. It's really valid.

  11. #11
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    I needed to learn another language (not Java) quickly once, for a project I'd been dumped on in protest. I have a lot of CDs, and when friends came round at the weekend, we'd be constantly up and down changing CDs all the time. So I wrote a jukebox that held all my CDs as MP3s, and let us just scroll through all the CDs in my collection and choose random songs to put on a playlist, etc etc, via a numeric keypad.

    Point is, because it was something I was actually interested in, I learnt a lot quicker than if I just plodded along doing generic exercises somebody else had set me, plus, I had something useful at the end of it

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  13. #13
    mohkhamis is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default whts Book prefered for study?

    which book do u prefer to learn java on it u recomment im reading now th ebook for study guide is it useful or not

  14. #14
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by mohkhamis View Post
    which book do u prefer to learn java on it u recomment im reading now th ebook for study guide is it useful or not
    I bought "The Java Programming Language" by James Gosling, got a Java job and jumped right in the deep end, but I wouldn't recommend that to anybody!

    The "Head First" series of books are very good by all accounts

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by mohkhamis View Post
    which book do u prefer to learn java on it u recomment im reading now th ebook for study guide is it useful or not
    I suggest you the Suns' tutorial. It's really helpful to me at the time I start work on Swing/AWT in Java.

    Here is the the link. Start from the first page.

  16. #16
    swikar.java is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default For Loop Question

    1 for i:=1 to n do
    2 for j:=1 to n/3 do
    3 x:=x+1

    How many times third line execute?
    This is actually Psedocode.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by swikar.java View Post
    1 for i:=1 to n do
    2 for j:=1 to n/3 do
    3 x:=x+1

    How many times third line execute?
    This is actually Psedocode.
    What are you looking here? If you have a question on something related to Java, choose the correct forum and post it. Please don't post others threads if your question is not related to it.

Similar Threads

  1. J2ME User Interface Developer
    By mobileapps in forum Jobs Offered
    Replies: 0
    Last Post: 10-03-2008, 12:43 PM
  2. user interface development using JSP
    By pradeep1_mca@yahoo.com in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-02-2008, 01:48 PM
  3. Help user interface
    By carl in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:58 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
  •