• Post Reply Bookmark Topic Watch Topic
  • New Topic

listening for events in other windows

 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does any one have a code example for how to listen for events in other windows? I have a JFrame that is like a control window, but it also spawns other windows (in JFrames). When a user clicks a button in one of the child windows, how can I get that event in the control window, plus the information the user entered in text boxes, etc?
Thanks in advance,
Jon
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon,
Probably the best way to handle it is by using java.beans.PropertyChangeSupport. You can create pseudo properties to dispatch between registered components.
Here is the way I did it for my Developer Certification:
PropertyDispatcher.java

ComponentMediator.java

Now all you have to do is have your components implement PropertyChangeListener and register with the dispatcher those properties that it is interested in. For example:

And whenever we wish to let other components know that something has changed we do something like:

When we do that then all the components that have registered for the nameField property will have their propertyChange() method called.
Hope this helps,
Michael Morris
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
thanks for the help. your code got me thinking, and i came up with the following that (so far) is working. i don't know if it's "proper form" for swing (especially since it is returning a reference to another window's component), but it just may serve my purposes.
Jon
msg.java

guitest.java
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon,
That's a pretty good solution but as you pointed out in your code:

// you have to be careful with this though, because
// msg.getSendButton() returns a reference to the actual
// button object in the button class. that means that you
// could do something like m.getSendButton().setLabel("foo");
// and it would actually change the button in the message window.

Now you have coupled the send button to this class. There is really nothing wrong with that on a limited basis such as this one. In every program, some coupling will exist, but it's good practice to keep it to a minimum. The Mediator pattern decouples classes by being an itermediary between them. The only coupling involved is between the Mediator itself and the individual classes.
Michael Morris
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback. Since the application won't be very complex, I'll probably go with "coupling" the necessary objects and having methods to return the values for the user entries. if it were going to be any more in depth, i'd definitely use your code.
thanks again
Jon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!