MarvinView JFileChooser Example¶
This section demonstrates the possible integrations of Marvin to Swing components in a practical example.
In this example, we use Marvin as a custom accessory component of a file chooser to provide a preview to molecule files.
In case the currently selected item in the list of the file chooser is a structure file that Marvin can import, the accessory component displays a thumbnail sketch of the molecule rendered by Marvin.
Otherwise, the accessory component is empty.
The file chooser with this custom accessory component is shown in the image below:
The following text about creating custom accessory components is quoted from the Java Tutorials:
- Any object that inherits from the JComponent class can be an accessory component.
- The component should have a preferred size that looks good in the file chooser.
- The file chooser fires a property change event when the user selects an item in the list.
- A program with an accessory component must register to receive these events to update the accessory component whenever the selection changes.
The following paragraphs describe the major steps of setting up the accessory component.
Creating and Setting up the Renderer¶
The MolPrinter class is applied to render the molecules. In case the molecule coordinates are not given in the structure file, the 2D coordinates are automatically computed.
Following File Selection Changes¶
To update the preview of the molecule in case the user selects a different file, the accessory component has to react to file selection change events with the help of a property change listener. In the example the SimpleJFileChooserAccessory object itself serves as the java.beans.PropertyChangeListener for the specific JFileChooser.SELECTED_FILE_CHANGED_PROPERTY property of the file chooser.
Assigning the Custom Accessory Component to the File Chooser¶
The example calls the setAccessory method to establish an instance of the SimpleJFileChooserAccessory class, as the chooser's accessory component:
Sample code: SimpleJFileChooserAccessory.java
Advanced Preview of Multi-molecule Files¶
Aside from a simple previewer, the accessory component can be set up to offer the possibility of browsing inside multi-molecule files without actually opening them.
The following image shows the advanced accessory component:
In this advanced example there are two additional buttons inside the custom accessory component that enables browsing inside multi-molecule files.
The management of the two buttons are not described in detail, only the parts closely related to Marvin, but the full source code is available.
MolImporter refers to each individual molecule in a multi-molecule file by its index (position in file, starting from 0) and provides a method to position to a molecule specified by its index.
The second argument is an optional progress monitor object which is omitted here.
The custom file chooser accessory shown in this section is already used in several Chemaxon applications and proven to be useful.

