Results 1 to 6 of 6
  1. #1
    oim415 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default ActionListener question

    If you have an array of objects, TextFields or buttons in a frame, how can you reference that object's index in the actionlistener? For example, I have 7 TextFields set up as an array and need to pass it's index as a string to a method when it gets the focus.


    What I have is:

    public static void main(String[] args) {
    //other code
    ASimpleClass asc = new ASimpleClass();
    JTextField[] loc = new JTextField[7];
    for(int i=0; i<7;i++){
    loc[i] = new JTextField(""+i,3);
    loc[i].addFocusListener(this);
    add(loc[i]);
    }
    //more code
    }

    public void FocusGained(FocusEvent e){

    /* Alternatively I could do this but it seems brute force.
    JTextField source = (JTextField)e.getComponent();
    String mystring = asc.getResult(source.getText());

    */

    String mystring = asc.getResult("the index of the textfield that got Focus");
    //do some stuff
    }

    public class ASimpleClass(){

    public String getResult(String str) {

    String result = str;
    return result;
    }
    }

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

    Default Re: ActionListener question

    Consider creating and posting a very small, simple compilable and runnable program that shows your problem, an SSCCE.

  3. #3
    oim415 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: ActionListener question

    Ok, so here's a little app. Right now I have each text field hold the number of the index which I use to compare with the location of Waldo. I'd like to be able to reference the TextField's index (if that's possible). If not, is there a more elegant way that I could do this. I'd like to extend this out to a multi-dimentional array of TextFields.

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class WhereIsWaldo extends JFrame implements FocusListener {
    
    	private JPanel contentPane;
    	private JPanel gridPane;
    	private JPanel messagePane;
    	int waldoIsHere = (int) (Math.random()*5); //generate a random number between 0-4
    	ASimpleClass dot;
    	JLabel msg;
    	
    	public WhereIsWaldo() {
    		super("Where is Waldo");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		gridPane = new JPanel();
    		JTextField[] loc = new JTextField[5];
    		for(int i=0; i<loc.length;i++){
    			loc[i] = new JTextField(""+i,3);
    			loc[i].addFocusListener(this);
    			gridPane.add(loc[i]);
    		}		
    		messagePane = new JPanel();
    		msg = new JLabel("Where is Waldo?.");
    		messagePane.add(msg);	
    		contentPane.add("North", gridPane);
    		contentPane.add("South", messagePane);
    		setVisible(true);	
    		dot = new ASimpleClass(); 
    		dot.setLocation(waldoIsHere);
    	}
    	
    	public void focusGained(FocusEvent e){
    		JTextField source = (JTextField)e.getComponent();	
    		if (dot.getLocation()==Integer.parseInt(source.getText())){
    			msg.setText("You've found Waldo!");
    		} else{
    			msg.setText("Try Again!");
    		}	
    	}
    	
    	public void focusLost(FocusEvent e){		
    	}
    
    	public static void main(String[] args) {
    		WhereIsWaldo frame = new WhereIsWaldo();
    		frame.setVisible(true);			
    	}
    }
    class ASimpleClass{
    	int location;
    	
    	public int getLocation() {
    		return location;
    	}
    	public void setLocation(int locs){
    		location = locs;
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener question

    To do what you wish, all you need to do is make loc a class field, not a variable that is local to the constructor. Then in the actionPerformed use a for loop to iterate through loc[i] to see if the source == loc[i], and if so, check if i == whereiswaldo. That's it.

  5. #5
    oim415 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: ActionListener question

    thanks. got it to work.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener question

    Good deal, and thanks for getting back to us with your success!

Similar Threads

  1. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 04:59 PM
  2. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 07:05 PM
  3. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 08:43 AM
  4. Question on ActionListener in combo box
    By Levian in forum AWT / Swing
    Replies: 7
    Last Post: 07-05-2011, 10:28 AM
  5. Calbutton actionListener question
    By bequick01 in forum New To Java
    Replies: 0
    Last Post: 05-04-2011, 04:17 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
  •