Why are these two ways to associate a GUI element with an action different from their results?

I am currently studying this tutorial.

The tutorial describes two different ways to map a text editing command (instance Action) to a GUI element.


Option 1:

Get an array containing all available actions from a specific JTextComponentone by calling component.getActions(). After that, sort them by name in HashMap and enter a method that takes a name and returns an action from HashMap.

Then, the mapping of the GUI element to the action on the JTextComponent is performed, as in the following example:

JMenu menu = new JMenu("Edit");
...
menu.add(getActionByName(DefaultEditorKit.cutAction));

Option 2:

Initiate an action directly, and then associate a GUI element with it. For instance:

JMenu menu = new JMenu("Style");

Action action = new StyledEditorKit.BoldAction();
action.putValue(Action.NAME, "Bold");
menu.add(action);
...

, , 1, JTextComponents , , 2, JTextComponent.

, . 2 JTextComponent, 1 JTextComponent.

, , , .

( ).

+4
1

1 Action GUI. 2 Action GUI. , 1, GUI, , , 2 .

0

Source: https://habr.com/ru/post/1539060/


All Articles