• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transfer value between JFrame

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Halo....

i have a question like this....
i have 2 JFrame first one i call it TestA has 1 JTextField and 1 JButton and the second JFrame i call it TestB has 1 JTextField and 1 JButton.

When i run TestA and i click button in JFrame TestA, it will show JFrame TestB(Dialog). and if i write any word in JTextField in JFrame TestB and i press thhe button in TestB then The JFrame TestB save the string into an object(i use object because it will not return just String but can return any value) and JFrame TestB will close and then i move the value that i just save in an object into JTextField in JFrame TestA...

How can i do it...? can any one help me please....?

thanks before.....
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a good idea to separate your data from your views.

FrameA asks to be notified when the "model" holding your golden data changes. Look into Observer and Observable in the JavaDoc. When FrameB updates the data, the model "publishes" notification that it has been changed to all interested listeners. When FrameA gets this notice, it fetches the data it cares about and updates its view. As an alternative option, the Model could send the changed data long with the notification so the "get data" call is not needed.

This may seem like a lot of moving parts - and more advanced patterns add even more - but it pays off as applications get bigger and more complex. If this looks like something you want to try, let's talk about how to avoid a circular dependency where FrameA knows about the Model and the Model knows about FrameA.
 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Mr. Stan James, i will try your suggestion. thanks....
 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean by "avoid a circular dependency where FrameA knows about the Model and the Model knows about FrameA"??
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First lets take a limited definition of dependency: A depends on B if B must be present to compile A or if changing B can cause A to change or break.

If FrameA has a variable of type Model so it can get data, and Model has a variable of type FrameA so it can send notification we have a cycle. You can't compile either without the other and changing either one will change the other. This goes against some design goals like isolating change and having only one reason to ever change a class. Also it's a common desire to reuse a given model with different views, so you don't want model depending on view.

Observer/Observable classes and interfaces in the JDK take care of this by making both FrameA and Model depend on them. FrameA still probably knows about the Model to retrieve data, but the Model is not tied to any particular frame. No cycle.

In bigger systems we can move the dependency discussion up to components or packages. Now the Model component might define an interface that an interested View must implement to receive notification. The View depends on the Model to compile, but we don't have to have any View at all to compile the Model. I like to imagine that we could sell a shrink-wrapped version of our Model that any developer that we never met could use with a new View that we never thought of.

If this kind of thing sounds interesting, scroll on down to the OO, UML, etc forum. We're probably pushing the boundaries here in the beginner forum.
 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so in another word, i should create a class Model with it's attribute. and create FrameA class that had an attribute type of Model, then when i call FrameB i send a Model refer from FrameA to FrameB, if i change any value Model in FrameB it's also infected Model in FrameA. should i send the Model referance from FrameA into DrameB from constructor or there is another way? hehehehe
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My little diagram showed FrameA and FrameB both accessing the Model so it's reasonable for both frames to have a reference to Model. You can pass it from FrameA to FrameB through a constructor.

In a more elaborate model we might introduce a Controller (or two) to glue the application together and "interpret user gestures" into application actions. I like my action listeners to have only one line that tells the controller what the user is asking. The controller then knows how to do it. For example, one little database utility has the action listeners calling the controller like this ... each line is in a different listener, the comments were added just for this discussion ...

That's about all the code there is on the window aside from creating Swing widgets. Does that make any sense?

BTW: I wouldn't actually call the model class "Model." If it's fairly simple you might give it a name meaningful in the domain. Same for your frames or views ... I wouldn't want FrameA and FrameB outside of these examples. Or was I taking you too literally?
[ October 05, 2006: Message edited by: Stan James ]
 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Mr. Stan, your not taking it too literally. In a matter of fact, your clue and suggestion already describe all the things that i want to no.

Can i ask another question hehehehe?

i want to create an example like this (web project) that including java applet in it. the problem is when we run applet on a client, the client must download all the signed jar that included in the applet. (If the project is small it's doesn't matter) but when the project is big is a matter....
because i included all the classes in one jar. so i want to separate all the classes, so the client doesn't had to download to much jar. i want to leave the classes that had dependance with a database and a model in server but the classes that had dependance with a view in a client.....

what i don't know is how can i do it/what should i do?

thanks for all your suggestion
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to make a custom jar with only exactly the classes required for the applet. How do you make your jars? Ant tasks?

I'd probably make a whole new project in Eclipse and work to keep the project / properties / build path dependencies as small as possible. There are some tools to help with this, too. I think one was called FatJar that could build a jar with all the dependencies it could identify from a starting class.

This is quite a topic swing. Maybe start a new thread with a topic title about applet jars and see who else jumps in.
 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Mr. Stan i'll add new subject later. i want to try the model and frame first. thanks for the suggestion.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic