Re: ActionEvent Performance Issue
From: Alan Moore (jbigboote_at_yoyodyne.com)
Date: 12/13/03
- Next message: Robert Ludewig: "Re: add JPanel and JLabel on JButton"
- Previous message: Andrew Thompson: "Re: Outlookstyle Side Bar"
- In reply to: Jim Cobban: "ActionEvent Performance Issue"
- Next in thread: Jim Cobban: "Re: ActionEvent Performance Issue"
- Reply: Jim Cobban: "Re: ActionEvent Performance Issue"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 13 Dec 2003 04:50:45 GMT
On Thu, 11 Dec 2003 13:46:33 -0500, "Jim Cobban" <jcobban@magma.ca>
wrote:
>I am trying to handle ActionEvents spawned by various components in a dialog
>panel in an efficient way. Perhaps I am missing something but I cannot find
>an example of how to do this.
>
>The examples I have found use setActionCommand on JButtons or JTextFields to
>assign an action command String to the component. This String is then
>passed in the ActionEvent to actionPerformed method of the supplied
>ActionListener. This method looks something like:
>
> public void actionPerformed(ActionEvent ev)
> {
> String actionCommand = ev.getActionCommand();
> if (actionCommand == SAVE)
> { // apply updates to the Register element
> performSaveAction();
> } // apply updates to the Register element
> else
> if (actionCommand == SAVE_AS)
> { // apply updates to the Register element
> performSaveAsAction();
> } // apply updates to the Register element
> else
> if (actionCommand == CANCEL)
> { // finished with dialog
> frame.doDefaultCloseAction();// close the dialog without updating
> } // finished with dialog
> else
> if (actionCommand == EDIT_FIELD)
> { // pop up dialog to edit attributes of Field
> performEditFieldAction();
>...
>What bugs me about this implementation is that I cannot replace the series
>of if-then-else statements with a switch statement to branch directly to the
>function to be performed. That is because the interface for
>setActionCommand specifies that its parameter must be a String, rather than
>an Object. If it were an Object then I could pass in an Integer containing
>the desired case statement value.
>
>The only alternative that I can see is to associate a separate Action with
>each JButton or JTextField, which I feel is excessive overhead. Using
>setActionCommand, I need merely implement the ActionListener interface
>within the class creating the dialog, as opposed to creating a separate
>class implementing Action corresponding to each component in the panel.
You're talking about something that happens when a user clicks on a
button; performance is not an issue.
- Next message: Robert Ludewig: "Re: add JPanel and JLabel on JButton"
- Previous message: Andrew Thompson: "Re: Outlookstyle Side Bar"
- In reply to: Jim Cobban: "ActionEvent Performance Issue"
- Next in thread: Jim Cobban: "Re: ActionEvent Performance Issue"
- Reply: Jim Cobban: "Re: ActionEvent Performance Issue"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]