开发者

JPopupMenu getParent() returning a null?

Ok so I'm working on an Eclipse plugin based on the JGraph example. The problem is I can't really get the "save" method to work, here's how the program works in short: - I have a DiagramEditor class with an init() method, where I create a GraphEditor object and call the createFrame() methord of that object. - GraphEditor extends the BasicGraphEditor (which extends JPanel), the createFrame() method returns a JFrame and has a line "frame.setJMenuBar(menuBar)" - the "menuBar" is an object variable, which is initialized in the BasicGraphEditor.

Till here everything is cool, the problem is with the action listener which is supposed to save a file. To get the graph I need to get the GraphEditor component, so I do a Component component = (Component) e.getSource() whitch is the ActionEvent passed to that action listener and at that stage is the JMenuItem "save", then I get the parent which is the JPopupMenu, then I want to get that JPopupMenu's parent which should be the GraphEdiotor, but instead I get a null. Any idea why?

Here's some source code:

    DiagramEditor.java:

    @Override
public void init(IEditorSite site, IEditorInput input)
        throws PartInitException {
    setSite(site);
    setInput(input);
    this.diagram = ((DiagramEditorInput)input).getDiagram();
    setPartName(this.diagram.getName());

    gEditor = new GraphEditor();
    gEditor.createFrame().setVisible(true);
}

    BasicGraphEditor.java:

    public JFrame createFrame()
{
    JFrame frame = new JFrame();
    frame.getContentPane().add(this);
    frame.setJMenuBar(menuBar);
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(870, 640);
    return frame;
}

    In the constructor:

    menuBar = new JMenuBar();

    menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(menu);

    JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
    // ADD FILE OPENING
    //openMenuItem.addActionListener(menuListener);
    menu.add(openMenuItem);


    JMenuItem saveMenuItem = new JMenuItem("Save", new ImageIcon("/images/save.gif"));
    saveMenuItem.setMnemonic(KeyEvent.VK_S);
    saveMenuItem.addActionListener( new SaveAction(false) );
    menu.add(saveMenuItem);
   // menu.add(new SaveAction(false));

    JMenuItem saveMenuItemAs = new JMenuItem("SaveAs", new ImageIcon("/images/saveas.gif"));
    //saveMenuItemAs.setMnemonic(KeyEvent.VK_S);
    save开发者_如何学CMenuItemAs.addActionListener( new SaveAction(true) );
    menu.add(saveMenuItemAs);
    //menu.add(new SaveAction(true));       

    JMenuItem closeMenuItem = new JMenuItem("Close", KeyEvent.VK_C);
    closeMenuItem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            }
    );
    menu.add(closeMenuItem);


The menu is attached to the frame not your class. Probably the best option is to make sure your SaveAction can see what it needs directly. When you construct your SaveAction, it can have an implicit or direct reference to the GraphEditor.

If you define your SaveAction class INSIDE the GraphEdtior class like this:

class SaveAction extends AbstractAction
{
        public void actionPerformed(ActionEvent e) {
            GraphEditor myGE = GraphEditor.this;

            .. do whatever 
        }
}

You'll see your SaveAction already has access to the GraphEditor (an implicit reference).

If your SaveAction class is defined as static, or defined in a different class or file, then you simply need to give it the GraphEditor when you construct it and make it hold the reference:

class SaveAction extends AbstractAction
{
    private GraphEditor graphEditor;
    private boolean myBoolean;

    public SaveAction(GraphEditor graphEditor, boolean myBoolean)
    {
        this.myBoolean = myBoolean;
        this.graphEditor = graphEditor;
    }

        public void actionPerformed(ActionEvent e) {

            this.graphEditor....
            .. do whatever 
        }
}

In Both cases, your actionPerformed() in your saveAction will have access to the GraphEditor to do what you require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜