Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2006

Tip: Looking for answers? Try searching our database.

Return values from an ActionListener?

Thread view: 
Dave Reid - 27 Sep 2006 18:59 GMT
    I'm still relatively new to Java/Swing programming and have a pretty
simple question about using ActionListeners.
       I have a big array of stuff, and I want to send that array to a
method to do some editing of the array but using a button ActionListener.
So its no problem to send the array to the called method, but how would I
get the changed array back to the part of my program that called it?  I
understand that actionListeners don't allow for returned values.
       I'm sure this is likely very simple, so I appreciate the help.

dave
Knute Johnson - 27 Sep 2006 20:57 GMT
>         I'm still relatively new to Java/Swing programming and have a pretty
> simple question about using ActionListeners.
[quoted text clipped - 6 lines]
>
> dave

When you pass an array to a method you are really passing a reference to
the array to the method.  So you don't really have to get the array back
since you still have the reference to it.  Your method can just modify
the array as I have done here.  Then you can access the array again with
the existing reference.  So you are correct, it is very simple, in fact
so simple that it is confusing :-).

import java.awt.*;
import java.awt.event.*;

public class test6 {
    public static void main(String[] args) {
        final int[] array = new int[10];

        Frame f = new Frame();
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        Button b = new Button("Press Me");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                addOne(array);
                for (int i=0; i<array.length; i++)
                    System.out.println(array[i]);
            }
        });

        f.add(b);
        f.pack();
        f.setVisible(true);
    }

    static void addOne(int[] array) {
        for (int i=0; i<array.length; i++)
            array[i] += 1;
    }
}

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.