Results 1 to 8 of 8

Thread: Where to use actionlistener ?

  1. #1
    Join Date
    Mar 2010
    Posts
    383

    Where to use actionlistener ?

    Hello,
    I would like to know about General Action listener will learn through a book I've read four different examples of the tethering an Event:
    1) Frame is Listener
    2) Member-class listener
    3) Anonymous subclass of an adapter
    4) Anonymous class of listener interface
    You are an experienced Java people say these things a little more determined than me, because I am in the training. My questions:
    1) Is it a matter of taste, I like what tethering listener? (Certainly not, but ....)
    2) How can I solder in the simplest way a listener to a button.
    Any help regarding this is appreciated.

  2. #2
    Join Date
    May 2008
    Posts
    255

    Re: Where to use actionlistener ?

    Hello,
    Here is the code which can explain you properly

    Code:
    Button class Hello extends JFrame implements ActionListener (
    JButton b1;
    JButton b2;
    Hello public Button () (
    this.setDefaultCloseOperation (DISPOSE_ON_CLOSE);
    mkBut ();
    arele ();
    Buttons register ();
    )
    private void mkBut () (
    b1 = new JButton ("button 1");
    b2 = new JButton ("button 2");
    )
    private void arele () (
    this.setLayout (new Flow layout ());
    this.getContentPane (). add (b1);
    this.getContentPane (). add (b2);
    )
    Buttons private void register () (
    b1.addActionListener (this);
    b2.addActionListener (this);
    )
    public static void main (String [] args) (
    Hello h = new Button app Button ();
    app.setSize (100,100);
    app.setVisible (true);
    )
    public void actionPerformed (ActionEvent exc) (
    if (exc.getSource () == b1)
    System.out.println ("Button1 pressured");
    if (exc.getSource () == b2)
    System.out.println ("Button2 pressured");
    System.out.println ("An event occurred");
    )
    )

  3. #3
    Join Date
    Mar 2010
    Posts
    383

    Re: Where to use actionlistener ?

    If I have now understood, then an action listener to the main frame (inherit, Eduard and the added components that? I am also a friend of convenient constructors. Maybe some of it is clear to me if I like dealing with your script. But you can say, I can choose one or two preferred way and this use if possible?

  4. #4
    Join Date
    Dec 2009
    Posts
    292

    Re: Where to use actionlistener ?

    The listener (since virtually the business logic is inside) separated from the GUI. However, I've got now the following problem, if I outsource the listener in an extra class, I can not within actionPerformed () to access without contortions as the object in which he sits frame (and the trigger button)

  5. #5
    Join Date
    Dec 2009
    Posts
    292

    Re: Where to use actionlistener ?

    Tthe listener (since virtually the business logic is inside) separated from the GUI. However, I've got now following problem: if I outsource the listener in an extra class, I can not within actionPerformed () to access without contortions as the object, he sits in the frame (and the trigger button). Basically, there is always a matter of course I usually try (though I've done lately very little GUI stuff, and certainly not with Java, and then certainly not with AWT / Swing ...) everything was manipulated in the GUI to keep listener, because you matter no way around it so easy. On the other hand it is quite a good idea what is behind your back in its own method (which has nothing at the hat with GUI) to outsource with a nice interface, which is then called in the listener.

  6. #6
    Join Date
    Sep 2009
    Posts
    152

    Re: Where to use actionlistener ?

    If I outsource the listener in an extra class, then I can within actionPerformed () is not without contortions to eg access the object in which they frame (and the trigger button) is sitting, it would under the MVC model, view and controller (Action = listener) know, so there would be a possible access (or you make changes to the model, then what the view is again recovered).

  7. #7
    Join Date
    Nov 2009
    Posts
    359

    Re: Where to use actionlistener ?

    Another note for the separation of Gui and business logic. Perfect sense. The catch is always accessible. Any need to pass something. Either one makes the components that will work with its contents could be accessible, I mostly work with database-based applications, I think it is commonly so.

  8. #8
    Join Date
    Nov 2009
    Posts
    518

    Re: Where to use actionlistener ?

    In one panel, a button ("save") and (organized in other panels) to the presenting / changing content is in text boxes. The text boxes will be made available in the Unterpanels (getText / setText), but only up to the level at which the button is located. On the plane of ActionListener listener is not stored directly. Instead, the storage method is a singleton or factory class that does all the database work. The method also performs any tests. The listener calls on and only gets from the text boxes the required parameters.

Similar Threads

  1. Pass JFrame to actionlistener
    By Freeza 2 in forum Software Development
    Replies: 5
    Last Post: 23-08-2010, 06:30 PM
  2. Add actionlistener to JButton
    By KAMANA in forum Software Development
    Replies: 6
    Last Post: 03-08-2010, 10:54 AM
  3. Access ActionListener in java
    By Windowed in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 02:57 PM
  4. Query regarding use of ActionListener in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 10:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,283,619.89169 seconds with 17 queries