Re: parameters for actions?
Frank Meissner <[email protected]> Wed, 26 Jan 2005 21:43:19 +0100
| Newsgroups | gmane.comp.embedded.carlsbad-cubes |
|---|---|
| Message-ID | <[email protected]> |
Dennis Ponos wrote:
>
> Here's an (unrealistic) example that should point out what I'm trying to
> do.....
>
> Let's say that when a user presses a certain button, the menu items of a
> certain menu should be enabled/disabled depending on the value of a
> certain textfield. I would expect to write an "action" attribute for
> the button that looks like this.....
>
> action="doMenuItemChange(menu1, txtfld1)"
>
> and the object that gets passed to the SwingEngine's constructor would
> have a corresponding method to perform the action using the given
> parameters.....
>
> public void doMenuItemChange(JMenu aMenu, JTextfield aTextField) {
> .
> .
> .
> }
>
> I know that this is not how SWIXML works. As per the Actions example,
> it appears that SWIXML typically works with Actions.......
>
> Using Actions, I think I could get to menu1 and txtfld1 by using
> SwingEngine methods like find(String). However, my action would have to
> assume that I want menu1 and txtfld1 specifically, no? So if I wanted
> to do something similar for menu2 and txtfld2, I would need to write
> another Action?
No, not necessarily. You may write your action like this:
public class MyAction extends AbstractAction {
MyAction(String menuId, String textFieldId) {
this.menuId = menuId;
this.textFieldId = textFieldId;
}
public void actionPerformed(ActionEvent ae) {
JMenuItem jmi = swingEngine.find(menuId);
JTextComponent jtc = swingEngine.find(textFieldId);
...
jmi.setEnabled(<somewickedcomputation(jtc)>);
}
}
public class MySwingEngine extends SwingEngine {
public Action myAction1 = new MyAction("menu1", "text1");
public Action myAction2 = new MyAction("menu2", "text2");
MySwingEngine() {
comp = render("...");
...
}
}
Of course, this is a very simple example. But the path should be clear.
Depending on your requirements it may be necessary to have abstract
(Action) classes implementing the business logic as inner classes or the
like.
> If this is the case, I'm not saying that one way is
> better than the other, I just want to know how it works so I can
> visualize a possible design for my application. I can easily create 2
> actions that call some other class/method that performs the method
> generically.
Or, you could write your own components having a specialized setter
waiting for ids to be supplied.
public MyButton extends JButton {
public void setMyIds(String csvIds) {
...
}
}
<mybutton myids="id1,id2"...>
Frank
>
>
>
>
>
>
>
>
>
>
>
>
>
>>Frank
>>
>>
>>>
>>>I don't want to get into comparing XMLGUI frameworks, but since I see
>>>occasional references to Thinlet which gives me the impression that many
>>>on this forum are also familiar with Thinlet, I want to use Thinlet as
>>>an example. In Thinlet, it appears that actions are tied to methods,
>>>and you can specify parameters for those methods. I guess this was more
>>>of what I was expecting to see when looking at SWIXML.
>>>
>>>Any information on this subject will help me alot! Thanks.
>>>
>>>
>>>
>>>
>>>
>>>
>>>_______________________________________________
>>>Forum mailing list
>>>[email protected]
>>>http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com
>>
>>
>>
>>
>>_______________________________________________
>>Forum mailing list
>>[email protected]
>>http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com
>
>
>
> _______________________________________________
> Forum mailing list
> [email protected]
> http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com
>