Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 
 /*
  * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
  * subject to license terms.
  */ 
 
 package examples;
 
@Action basics.

A trivial @Action example: the buttons set/clear the Frame's title:

 public class ActionExample1 extends Application { 
     @Action public void setTitle() {
         appFrame.setTitle(textField.getText());
     }
     @Action public void clearTitle() {
         appFrame.setTitle("");
     }
     // ...
 }
 
The only wrinkle worth noting is that the Action objects we've created are going to call the methods on this object. So when we lookup the ActionMap for this class, we have to pass along the ActionExample1 instance as well:
 ApplicationContext ac = ApplicationContext.getInstance();
 ActionMap actionMap = ac.getActionMap(getClass(), this);
 setTitleButton.setAction(actionMap.get("setTitle"));
 clearTitleButton.setAction(actionMap.get("clearTitle"));
 
Since our @Actions have been defined in the Application subclass itself, we can use the no-argument version of getActionMap(), which returns the ActionMap for the application:
 ApplicationContext ac = ApplicationContext.getInstance();
 ActionMap actionMap = ac.getActionMap();
 setTitleButton.setAction(actionMap.get("setTitle"));
 clearTitleButton.setAction(actionMap.get("clearTitle"));
 

Author(s):
Hans Muller (Hans.Muller@Sun.COM)
public class ActionExample1 extends Application {
    private JFrame appFrame = null;
    private JTextField textField = null;
    @Action public void setTitle() {
    }
    @Action public void clearTitle() {
    }
    @Override protected void startup() {
	 = new JFrame("");
	 = new JTextField("<Enter the window title here>");
        .setFont(new Font("LucidSans"., 32));
	JButton clearTitleButton = new JButton();
	JButton setTitleButton = new JButton();
	JPanel buttonPanel = new JPanel();
	buttonPanel.add(setTitleButton);
	buttonPanel.add(clearTitleButton);
	.add(buttonPanel.);
	/* Lookup up the Actions for this Application 
	 * and bind them to the GUI controls.
	 */
	ActionMap actionMap = getContext().getActionMap();
	setTitleButton.setAction(actionMap.get("setTitle"));
	.setAction(actionMap.get("setTitle"));
	clearTitleButton.setAction(actionMap.get("clearTitle"));
    }
    public static void main(String[] args) {
        Application.launch(ActionExample1.classargs);
    }
New to GrepCode? Check out our FAQ X