Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField[ ] - index of event source?  RSS feed

 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think I am missing something obvious, or forgetting something very fundamental.

I am creating a user interface which requires an (n x n) grid of input areas (initial values can be read from file or entered manually by the user), and I have chosen to make an array of JTextField arrays



I then populate the fields (or not) based on initial values, and if my program generates new values / additional values, I am able to update the display.



If a user inputs something to a field, this information is visible on screen, however I have no idea where to start in order identify *which* field so that I can update my data structure.

I can think of the following options:
Create an ActionListener which can be added to all the fields, and somehow

i) use getSource() to (somehow) figure out which field I am dealing with
or
ii) iterate through the whole thing reading the text, comparing to previous state, and dealing with text if it has changed
or
iii) any suggestions?

Should I try to isolate more of my code to show what/how I am thinking?

Katrina
[ April 11, 2007: Message edited by: Katrina Owen ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would simply instanciate a separate listener for each field.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem I have with separate listeners, is that I can have anything from 16 to 100-plus fields, and this number will vary every time the program is run.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you will have 16 to 100 listeners. Why is that a problem?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of using JUST JTextFields, why not incorporate an object that
not only has a JTextField as private member data, but a specific private
string that the object can use to uniquely identify that certain JTextField with?

Justin
[ April 11, 2007: Message edited by: Justin Fox ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but then again, i dont understand what you are doing? is the user updating
the database then you go in and save it in the actual array of JTextfields?

if thats the case, you don't need to know the actual name/spot in the array of the modified textfield, just make a back up string or just go through and JTextField.setText() to each one every time the use presses
"update" or "re-set"

is this what you are wanting to be able to do?

Justin
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My trouble with making 16 to 100 listeners is that I don't know how to make a variable amount of listeners as I need them, rather than hard coding them. Hard coding 144 listeners seemed like overkill (and pretty daunting).

Now that I have thought about it, there are probably appropriate factory methods for such things, but I am still a bit green for such things.

While working on my code I realized what essential little bit of obviousness had escaped me:



(D'oh!)

I also decided to add an inner Location class to my view



This allows me to use my JTextFields as keys in a HashMap, the value being the Location. Consequently, I can use the position of the text field to update the information at the relevant location elsewhere in the program, or to obtain information for the relevant location.



The project is a GUI for a game - no database involved

Thanks for the feedback and help on this!

Katrina
[ April 11, 2007: Message edited by: Katrina Owen ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!