Re: Generating GUI at run time

Frank Meissner <[email protected]> Tue, 07 Sep 2004 23:02:01 +0200
Newsgroups gmane.comp.embedded.carlsbad-cubes
Message-ID <[email protected]>
Kevin Duffey wrote:
> Frank, thanks for the reply.
> 
> I left out an important part I guess, and that is the
> ability to use multiple xml files to build the UI. At
> runtime, if you load a dynamic plugin after the
> application is running and it has swixml xml, how do
> you generate the menu/UI stuff dynamically? Or does
> swixml only support a single file to build the entire
> UI, and that is it? Can you supply pieces of the UI
> with separate xml files? If so, can the pieces be
> added dynamically at runtime?

Hm, I usually use code like this to render a small menu (three items or 
less):

SwingEngine se = new SwingEngine();
menu = se.render("my-menu-for-current-component.xml");

This happens in some constructor where the "se" variable is a local one 
and can be garbage collected afterwards. There the xml contains only the 
menu, nothing more.

Another possibilty to provide "dynamic" behaviour is to render a panel 
as top-level component. This you may integrate in a call JOptionPane 
(the parameter "Object message" (or similar) may be a component). If you 
carefully use the topmost sufficient component, you can easily switch 
between JLabel, JTextArea or JTextField and the like. Sometimes (as in 
JLabel and the implementations of JTextComponent) you may require a 
"instanceof" operation or more...

If you want to dynamically change the behaviour of the main application 
menu, there are some possibilities. If you want to have, say, the edit 
menu different for some components, just have a complete edit menu for 
each component in a separate file. If the components are changed, fetch 
the edit menu from the swing engine which rendered the main application 
menu, remove it and add the component menu at the position of the 
previous edit menu. Or make the old edit menu invisible or the like...

> 
> If not, I would request that as a big feature
> improvement. Allowing pieces of the UI to be loaded
> dynamically..

This would clearly be a important feature missing but I use the 
techniques you mentioned. I think your problem lies somewhere else...

Yours,

Frank