• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two Swing Questions

 
Chris Lamey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
First off, I'm building a Swing app using the 1.4.1 JDK. The app has tabbed panes where each pane has complicated JPanels (containing more tabbed panes, tree views, and tables) where events in one pane need to be propagated to objects in other panes. The objects intending to generate and listen to the events are created by their immediate containers which are far down the hierarchy in the panes. Currently they don't have direct references to objects in other panels. Our current quick and dirty answer is to use getParent() in the event handler to go up the chain to a common parent container and then make a straight call that propagates down to the listening component. But I don't like that because it relies on the hierarchy staying static, which I'm not sure is true and seeing things like getParent().getParent().getParent().getParent() triggers a red flag. Other ideas include a application wide 'uber-event' handler that everything uses, a more specific dispatcher-type-object that gets passed around and handles the events, and some other ideas. Does anyone out there have a good example of or guidelines on how to manage this sort of thing? How do other people deal with event handling in complicated applications?
Secondly, in this application we need to put various cominations of JLabels and JTextFields, JLists, etc into a single object reference. The idea is that we need to be able to take any one of our system's business object attributes which are of various types (text, datetimes, addresses, enumerated distances, multi-valued text, currency ranges, etc) and present them in the UI. Our current thinking is to extend JPanel and have each child know how to take a specific type of attribute and put whatever it needs to visually represent the attribute onto itself. A factory would then be able to take an attribute and return a JPanel that could be drawn pretty much anywhere in the application. Does that seem like a decent idea or is there another container/component besides JPanel that we should consider? Or is there an altogether different strategy that might be better?
Any thoughts are appreciated.
Thanks,
Chris
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
you should consider working with data models. these models are objects that wrap your data and prepare the output for the views.
just as you say: you have different data (dates, numbers or whatever) to be inserted in the views (most of the time they will be inserted in JTextFields, I suppose). so the models have getter methods that return the string value that shall be displayed. changing the data means changing the model that will notify its view(s) by dispatching events. the model has no reference to any view, so you can replace the views as you whish. (while each view has a reference to its model.)
a well designed model should spare you from handling component references all over the place and calls to getParent(). Swing itself has many examples for model-view seperation (JTable, JTree etc.), but one model need not be confined to one view: in fact you can have one main model which handles the "big data", and then have some small models that take part of this main model's data and transform it to fit into some view.
hope this did help
Chantal
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"clamey" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!