Over a million developers have joined DZone.

Adding an Action to the Tab of a TopComponent

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

I've been working on a 7.0 branch for the JFugue Music Notepad project and added a "Rename Score" action on the tab of the Score window:

Here's how, in the TopComponent:

@Override
public Action[] getActions() {
    int existingActions = super.getActions().length;
    int existingActionsPlusRename = existingActions + 1;
    Action[] scoreActions = new Action[existingActionsPlusRename];
    List<Action> scoreList = new ArrayList(existingActionsPlusRename);
    scoreList.add(new AbstractAction("Rename Score") {
        public void actionPerformed(ActionEvent e) {
            NotifyDescriptor.InputLine newname = new NotifyDescriptor.InputLine("Name: ", "Rename Score");
            DialogDisplayer.getDefault().notify(newname);
            setDisplayName(newname.getInputText());
        }
    });
    scoreList.addAll(Arrays.asList(super.getActions()));
    for (int i = 0; i < scoreList.size(); i++) {
        Action action = scoreList.get(i);
        scoreActions[i] = action;
    }
    return scoreActions;
}

Can't promise it's the best solution, though. Instead of hard coding the Rename Action, it could be defined in the layer and then read from its folder (Utilities.actionsForPath), which would make the tab extensible from the outside.

 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}