Add menus "Save" and "Save as" to MViewP

User 818520b6b8

04-04-2005 14:26:01

Hi all,





I want to add the menus "Save" and "Save as" to a MViewPane created programatically.





I know how to add java menus, but I'd like these menus to have the default MViewPane functionality.





Thanks.

ChemAxon 7c2d26e5cf

05-04-2005 09:31:03

In default, MViewPane has not got a menubar. The menubar what you see in Marvin View examples, belongs to the frame that embeds the MViewPane component.


You can create a custom menu and assign it to the MViewPane component or its frame.


See the following example:


http://www.chemaxon.com/marvin/examples/view-popup/MViewPopupExample.java.txt

User 818520b6b8

05-04-2005 11:00:36

OK,





but I want the "Save as" and "Save" menus to allow my application to export to SDF (SDFiles).





How can I do that?

User b22f714996

05-04-2005 11:43:21

Hello





That should be possible if you get the molecule from the MViewPanel and use





molecule.toFormat("SDF");





on it.





tobias

ChemAxon 7c2d26e5cf

05-04-2005 11:56:43

Firstly, you should create a menubar (if you have not got one yet) and add it to your frame which includes:


Code:
// create main menu


JMenuBar menubar = new JMenuBar();


frame.setJMenuBar(menubar);


JMenu menu = new JMenu("File");


menubar.add(menu);


JMenuItem mi;


mi = new JMenuItem("Save");


mi.setActionCommand("save");


menu.add(mi);


mi.addActionListener(this);






Then you have to implement an ActionListener interface (in the same class where the menubar is defined) to receive actions from the menu. (It means, you need an actionPerformed method.)


To save structures in the viewer, you have to get structures from the viewer's cells


and write them into a file.


Code:
public void actionPerformed(ActionEvent event) {


    String cmd = event.getActionCommand();


    if(cmd.equals("save")) {


         try {


              String filename = getFilename();


              FileWriter out = new FileWriter(filename);


              for(int i = 0; i < viewPane.getCellCount(); i++) {


                   String molstr = viewPane.getM(i,"sdf");


                   out.write(molstr);


              }


              out.close();


         } catch(IOException ioe) {


               ioe.printStackTrace();


         }


    }


}





private String getFilename() {


   // insert your code here which determines the name of the output file


}


User 818520b6b8

05-04-2005 12:22:32

Thanks a lot.