Click to See Complete Forum and Search --> : send TextField input to object methods, HELP!
yarunas
August 23rd, 2009, 07:01 AM
hey,
I'm stuck with a project that I have to deliver in 2 days.
i'll try to explain my problem as good as I can.
my project is about formula 1 system.
so, I have a SysF1 class that contains many methods (i.e. addRace(String raceid)).
now I've been asked to make gui for this program.
so I made a MainFrame window, and Internal Frames for each method (i.e. internal frame called addRace and inside that window I have label with TextField for entering race ID).
now my problem is: how do I send info entered by user in internal frame textField (like race id) to the method addRace(String raceId) in the SysF1 class?
let's say i'm creating in MainClass object like: SysF1 sys=new SysF1();
how I can send info from internal frame's textField to sys.addRace(raceId)?
hope my problem is clear enough...
many thanks for helpers!
dlorde
August 23rd, 2009, 10:19 AM
Assuming the text field is declared in the internal frame, you could make the internal frame a subclass of JFrame and give it a method that returns the race id string to anyone that wants it. This requires that the caller knows when the race id is ready to pick up.
Alternatively, you could pass a reference to the SysF1 object into the internal frame when you construct it, and call the addRace method from the internal frame. This is a bit inelegant as the internal frame doesn't really need to know the whole SysF1 API, so it would be better to use a limited interface, in this case an interface of one method. There is a popular idiom for this kind of thing called the 'Observer' pattern, where the SysF1 object would be an observer of (or 'listener' to) the internal frame (the observed object), which would make notification calls to the SysF1 object when its text field changes. For this pattern, the observer class implemenents an interface containing a callback method, and the observed class implements methods (optionally a 'Subject' interface) that allow observers to be added and removed from a list, and that can iterate the list of observers notifying them when a suitable event has occurred. For example:interface NewRaceObserver {
void newRaceAdded(String raceId);
}
public class SysF1 implements NewRaceObserver {
...
public void newRaceAdded(String raceId) {
... // handle new race id.
}
...
}
public class NewRaceFrame extends JFrame {
private List<NewRaceObserver> newRaceObservers = new ArrayList<NewRaceObserver>();
// This is where the NewRaceFrame knows the race id has been added
public void raceIdHandler(String raceId) {
...
notifyNewRaceObservers(raceId); // notify observers
}
public void addNewRaceObserver(NewRaceObserver o) {
newRaceObservers .add(o);
}
public void removeNewRaceObserver(NewRaceObserver o) {
newRaceObservers .remove(o);
}
public void notifyNewRaceObservers(String raceId) {
Iterator i = newRaceObservers.iterator();
while (i.hasNext()) {
NewRaceObserver o = (NewRaceObserver) i.next();
o.newRaceAdded(raceId);
}
}
}
...
newRaceFrame.addNewRaceObserver(sysF1); // somewhere, SysF1 is added to the NewRaceFrame as an observer
...There are many variations on this theme - I've made the elements specific to a particular change ('new race added'), but it can be done in a more generic way, and you don't necessarily need all the list handling stuff if there will only ever be one observer. I haven't used a 'Subject' interface for the NewRaceFrame's add & remove observer methods because it doesn't seem necessary here.
There's plenty online about Observer/Observed and Listener patterns.
Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
A. Aho and J. Ullman
yarunas
August 24th, 2009, 06:59 AM
thanks for your help, it did help me manage a solution.
yarunas
August 24th, 2009, 07:02 AM
I have another question:
how I can get method to print it's output in a window?
I mean, I have a method in SysF1 class that sorts array and I want that when the user click on a button called "print sorted cars" a new window will appear with the sorted cars list in it.
how can I make it happen?
thanks!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.