Results 1 to 6 of 6
Thread: ActionListener question
- 12-09-2012, 04:46 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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;
-
Re: ActionListener question
Consider creating and posting a very small, simple compilable and runnable program that shows your problem, an SSCCE.
- 12-10-2012, 12:26 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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; } }
-
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.
- 12-11-2012, 05:59 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
Re: ActionListener question
thanks. got it to work.
-
Similar Threads
-
ActionListener Help
By rakosky in forum AWT / SwingReplies: 4Last Post: 04-06-2012, 03:59 PM -
ActionListener
By jaylimix in forum Java AppletsReplies: 5Last Post: 11-06-2011, 06:05 PM -
Please Help With ActionListener
By Daman12 in forum New To JavaReplies: 29Last Post: 10-26-2011, 07:43 AM -
Question on ActionListener in combo box
By Levian in forum AWT / SwingReplies: 7Last Post: 07-05-2011, 09:28 AM -
Calbutton actionListener question
By bequick01 in forum New To JavaReplies: 0Last Post: 05-04-2011, 03:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks