Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Interapp Communication  RSS feed

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been discussed numerous times before however, I don't feel the problem has ever been addressed aside from quesses and "this worked for me". So I'll propose it again and we'll see where it takes us.

What is the best way for swing components to notify other components (specifically outside of it's own class) that something needs to change. Let's talk about a simple example...

Suppose I have a JFrame with a JMenuBar and a JToolBar. When I choose a specific JToolBar button, another JToolBar button should be disabled. As with most applications the corosponding JMenuItem should be disabled as well.

Typically, I like to keep my JToolBar, JFrame, and JMenuBar all in their own classes. So what is a good solution for the given problem?
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg,

These are 2 separate issues.
1) Enabling/Disabling corresponding menu item and button:
Use actions. There is a tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html
2) When pressing a button causes something to happen:
I was always just enabling/disabling apropriate controls
in the action listeners.


Or something like that if you know what I mean...

P.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petr,

Thanks for responding however your reply is axactly the types of replies that lead nowhere. No offense. What I mean is, I have seen that Action tutorial 100 times and it does not address my question. And your solution for disabling/enabling is a hack solution and assumes everything is in one class or has some sort of static accessor to the object.

So I coded a small test app with several classes which I will list below. My question is how, in my example, would I add text to the JTextField in my MainFrame class from my TestAction class? Or rather, how is it supposed to be done. I know hack ways of doing it.

[MainFrame.java]


[Menu.java]


[ToolBar.java]


[TestAction.java]

[ August 26, 2004: Message edited by: Gregg Bolinger ]
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I manage this kind of complexity is to take the MVC paradigm so prevalent in Swing and throw it over the whole application. Your app above is pretty much all View (i.e. GUI components). The only model is the text being passed around. In a more complex app the model may be a database and the associated persistance logic. I added a Controller class which registers for all events and distributes the effects of those events to listeners. Though I take advantage of the existing EventListener interface, it may make sense to create custom events in order to prevent Controller.actionPerformed() method from becoming unweildly. I think this practice gives you a level of indirection so that changes to the controller or GUI won't necessarily affect the other. It respects the encapsulation of the various classes you've created and it makes it possible to encapsulate this MVC as a component and use it inside a larger application. Check it:
Controller


TextUpdateListener


MainFrame


Menu


ToolBar


I apologize for not using TestAction, but you know, different strokes. . .
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe, now this is what I am looking for. I am going to toy around with your idea for a bit and see how it suits what I am doing. Thanks for the tips and code. I'll be back I'm sure with some complaints and questions though.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in the JGoodies Binding framework which eases the work of associating a model with a lot of different types of controls. Basically the problem you're describing is the problem that MVC solves so neatly. Once you have a single, unambiguous model that you can use at any time to determine the state of each of the visual components, it just becomes a matter of passing the model around to any components that need it. Some components are so frequently used without referencing their underlying models that we forget the models are even there.

Here is a completely contrived example. I create three models at the very beginning of the program and the rest of the code just provides ways to access and change those models. It's a bit cumbersome to create the models and bind them properly to views (again, see JGoodies Binding for some convenience methods) but if you embrace the separation of model and view throughout your program you'll get an immense inner joy every time you see two components change at the same time and reflect on how little code it took to implement that.

Just a side note, I've done something completely unnecessary here in making the carets for two text component mimic each other even though you won't actually be able to see both carets at once. Use control-tab to switch between text areas without changing where the carets are.



[ August 27, 2004: Message edited by: David Weitzman ]
[ August 27, 2004: Message edited by: David Weitzman ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!