2 Replies - 5432 Views - Last Post: 16 February 2008 - 04:19 PM Rate Topic: -----

#1 texasT12345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-February 08

ActionListener

Posted 15 February 2008 - 03:24 PM

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

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:

This post has been edited by Martyr2: 15 February 2008 - 03:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ActionListener

#2 torenrl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-February 08

Re: ActionListener

Posted 16 February 2008 - 01:11 PM

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

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:

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 February 2008 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: ActionListener

Posted 16 February 2008 - 04:19 PM

View PosttexasT12345, on 15 Feb, 2008 - 05:24 PM, said:

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.

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1