Button and Menu Actions
Swing supports an easy way to configure buttons and menu items to perform a specific task, implemented in the class Action.
The most important feature of Action is the sharing and synchronizing of properties (For example, Button text) and state (For example, checked/not checked) between multiple GUI-Elements.
The class forge.gui.ForgeAction extends the features of Action. Instead of taking a text in the constructor, it takes a localized property key, such as "%s/ErrorViewer/save". ForgeAction then uses "%s/ErrorViewer/save/button" as the label.
The setupButton() method configures a button for the forge action. The method returns the input component to save programmers from declaring local variables. For a Button configured with setupButton() instead of setAction(), if the button is a JMenuItem, "%s/ErrorViewer/save/menu" is alternately used to determine the label. additionally, the method can be overridden to have more control over the configuration of individual buttons.
To point out how to use ForgeAction, here is an example class:
import everything;
public class Example extends JFrame {
private ForgeAction exampleAction;
public Example() {
setupActions();
setupMenu();
setupGUI();
}private void setupActions() {
exampleAction = new ForgeAction("%s/Example/exampleAction") {
public void actionPerformed(ActionEvent ev) {
//the action to perform}};
}private void setupMenu() {
JMenuBar b = new JMenuBar();
setJMenuBar(b);
b.add(exampleAction.setupButton(new JMenuItem()));
}private void setupGUI() {
add(exampleAction.setupButton(new JButton()), BorderLayout.NORTH);
}}
See also
- ForgeProps and NewConstants – Information about config files and properties


