Okay guys,
I feel *re-e-e-eally* stupid for asking this here, but I have tried J2SE 1.4.1 API
JavaDocs, google, JDC, javaworld, javalobby, and just about every other resource that
I could try in a reasonable amount of time, with no success.
So I come to you. Please feel free to tell me to RTFM, but first please point out
which FM to R....
I have extended javax.swing.AbstractAction (call it MyAction), instantiated MyAction
with both a text label and an image icon in the AbstractAction constructor. In the
app I have added that one instance to both a menu and a toolbar.
MyAction.actionPerformed(ActionEvent e) prints out e.getActionCommand() as the label
with which I had instantiated the action.
Sample code fragents:
--------------------------------------------
class MyAction extends AbstractAction
{
public MyAction( String label, ImageIcon icon )
{
super( label, icon );
}
public void actionPerformed( ActionEvent e )
{
System.out.println( "Action test: " + label );
}
}
--------------------------------------------
class MyApp
{
JMenuBar menuBar;
JToolBar toolBar;
public MyApp()
{
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add( fileMenu );
toolBar = new JToolBar();
URL testURL = ClassLoader.getSystemResource("test.gif");
ImageIcon testIcon = new ImageIcon( testURL );
String testLabel = "Test";
Action myAction = new MyAction( testLabel, testIcon );
fileMenu.add( myAction );
toolBar.add( myAction );
}
public static void main( String[] args )
{
MyApp myApp = new MyApp();
JFrame appFrame = new JFrame( "My App" );
appFrame.setJMenuBar( myApp.menuBar );
appFrame.getContentPane()
.add( example.toolBar, BorderLayout.NORTH );
appFrame.setSize( 400, 600 );
appFrame.setVisible( true );
}
}
--------------------------------------------
Now here is the weird part. When I select the menu item, actionPerformed() prints out
the label that I used to instantiate MyAction, but when I select the toolbar icon,
actionPerformed() prints out "null". And, yes, I have verified that in the latter
case "e.getActionCommand()" is actually returning a null reference.
Why does the menu reference to the action produce an event with a non-null "action
command", while the toolbar version does not? Is there a way to get the toolbar
reference to produce the desired action command?
I realize that there is an easy work-around here, I can capture the testLabel as an
instance variable (data member) of class MyAction and then refer "this.testLabel" in
the actionPerformed() method rather than "e.getActionCommand()". My purpose here is
to understand why the GUI component (View) used to communicate the Action (Model)
changes the resulting event handling (Controller). In my view it should not.
Thanks in advance!
-- Roger
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________