Welcome to Dream.In.Code
Become a Java Expert!

Join 150,356 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,903 people online right now. Registration is fast and FREE... Join Now!




ActionListener

 
Reply to this topicStart new topic

ActionListener

texasT12345
15 Feb, 2008 - 02:24 PM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 1

I have a class GUIExample which extends JFrame and in the class is an array list being made. In another outside class ButtonListener I implement an Actionlistener, and in this listener I am trying to access the arraylist in my GUIExample, but I do not know how to pass a reference of the list into the ButtonListener class to access it. Could someone help me get throught this problem? Thanks

CODE

import java.util.*
import javax.swing.*;

public class GUIExample extends JFrame{


public GUIExample(){

ArrayList<JButton> list = new ArrayList<JButton>();

for(int i=0; i<5; i++){

list.add(new JButton("0"));
}
}
public static void main(String[] args){
JFrame frame = new GUIExample();

frame.setSize(350,350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fame.setVisible(true);
}
}

// another file in same folder
import java.awt.event.*;

public class ButtonListener implements ActionListener{

    public void actionPerformed(ActionEvent e){

       for(int j=0; j<list.size(); j++){
             list.remove(j);
}
}
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 15 Feb, 2008 - 02:30 PM
User is offlineProfile CardPM
+Quote Post

torenrl
RE: ActionListener
16 Feb, 2008 - 12:11 PM
Post #2

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 7


My Contributions
I'm not shure if i understand excactly what you mean, but you could use a constructor to pass a list to the actionlistener:

CODE

public class ButtonListener implements ActionListener{

    // List in actionlistener
    ArrayList<JButton> list = new ArrayList<JButton>();

    // Constructor
    public ButtonListener(ArrayList<JButton> list){
        this.list = list;
    }

    public void actionPerformed(ActionEvent e){

    for(int j=0; j<list.size(); j++){
        list.remove(j);
}
}
}


But it might be better to refer to the original list:

CODE

public class ButtonListener implements ActionListener{

    public void actionPerformed(ActionEvent e){

    for(int j=0; j<GUIExample.list.size(); j++){
        GUIExample.list.remove(j);
}
}
}


This post has been edited by torenrl: 16 Feb, 2008 - 12:15 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: ActionListener
16 Feb, 2008 - 03:19 PM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(texasT12345 @ 15 Feb, 2008 - 05:24 PM) *

I am trying to access the arraylist in my GUIExample


I'm not entirely sure what you're looking for. You code never registers the buttons with any listeners or adds them to any form! Here's some same code that may clarify some things.

CODE

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUIExample extends JFrame{
    class ButtonListener implements ActionListener{
        // show me the button I pressed
        public void actionPerformed(ActionEvent e){
            System.out.println("button pressed: " + e.getActionCommand() );
        }
    }
    
    public GUIExample(){
        // good layout for just throwing stuff on a form
        this.setLayout(new FlowLayout());
        // create an instance of our ActionListener
        // This reference is lost when we leave is method,
        // but the listener will be live
        ButtonListener listener = new ButtonListener();
        // Not sure what this is for
        // If your trull want our listener to have a reference to it
        // see previously offered code
        ArrayList<JButton> list = new ArrayList<JButton>();
        for(int i=0; i<5; i++){
            // make a button instance, we need to do some stuff with it
            JButton button = new JButton(String.valueOf(i));
            // add it to our array list.  Why?  Don't know, but adding.
            list.add(button);
            // add to this JFrame, so we have something to see
            this.getContentPane().add(button);
            // add to our listener, so it has a reason to exist
            button.addActionListener(listener);
        }
    }
    
    public static void main(String[] args){
        JFrame frame = new GUIExample();
        frame.setSize(350,350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month