Ava
Ava

Reputation: 2132

how to use jface/swt actions?

I'm working on an application using jface/swt and would like to use org.eclipse.jface.action.Action to implement menuItems, buttons etc. I've search high and low for some documentation or tutorial explaining how to use actions, but have been unable to find any. Someone care to point me to tutorials, or enlighten me themselves?

Thanks in advance!

Note: This is a java application, not an eclipse plugin.

Upvotes: 2

Views: 4545

Answers (2)

Unplug
Unplug

Reputation: 709

I am also having some problem understanding how the menu bar, events, and actions in JFace work and there is very little useful posts online from what I can see. I sort of know how to use MenuManager. My question is whether I need to create 10 different classes for 10 menu items, if the actions are different.

You can study the source code here. Check chapter 4 about Action and IContributionManager. Also see chapter 9.

Here is a sample JFace menu program that works.

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class TestApp extends ApplicationWindow {

    public TestApp() {
        super(null);
        addMenuBar();
    }

    public Control createContents(Composite parent) {
        getShell().setText("JFace menu demo");
        getShell().setSize(800, 600);
        return parent;
    }

    protected MenuManager createMenuManager() {
        MenuManager mainMenu = new MenuManager();
        MenuManager fileMenu = new MenuManager("File");
        MenuManager helpMenu = new MenuManager("Help");

        // File popup menu
        fileMenu.add(new OpenFile());
        fileMenu.add(new Exit(this));

        // Help popup menu
        helpMenu.add(new About());

        mainMenu.add(fileMenu);
        mainMenu.add(helpMenu);

        return mainMenu;
    }

    public static void main(String[] args) {
        TestApp win = new TestApp();
        win.setBlockOnOpen(true);
        win.open();             
        Display.getCurrent().dispose();
    }

    class OpenFile extends Action {
        public OpenFile() {
            super("&Open Filer@Ctrl+O", AS_PUSH_BUTTON);
        }
        public void run() {

        }
    }

    class Exit extends Action {
        ApplicationWindow win;
        public Exit(ApplicationWindow aWin) {
            super("E&xit@Alt+X", AS_PUSH_BUTTON);
            this.win = aWin;
        }

        public void run() {
            this.win.close();
        }
    }
    class About extends Action {

        public About() {
            super("About", AS_PUSH_BUTTON);
        }
        public void run() {

        }
    }
}

Upvotes: 2

Alexey Romanov
Alexey Romanov

Reputation: 170745

Use IContributionManager.

Upvotes: 2

Related Questions