JPopupMenu getParent() returning a null?
- by Zenzen
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);
saveMenuItemAs.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);