IntelliJ open source
[Logo] JavaRanch » Big Moose Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic
Author

JTextField[ ] - index of event source?

Katrina Owen
Bartender

Joined: Nov 03, 2006
Messages: 1105

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

Joined: Jul 11, 2001
Messages: 14091

I would simply instanciate a separate listener for each field.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Katrina Owen
Bartender

Joined: Nov 03, 2006
Messages: 1105

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
Bartender

Joined: Oct 14, 2005
Messages: 7202

So you will have 16 to 100 listeners. Why is that a problem?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Messages: 782

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 ]

You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Messages: 782

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

You down with OOP? Yeah you know me!
Katrina Owen
Bartender

Joined: Nov 03, 2006
Messages: 1105

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 ]
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic
IntelliJ open source