In this example MarvinSketch is used as a JavaBean component. The actual JavaBean class is called MSketchPane.
The main goal of the example is to show how to create and embed the MarvinSketch bean in a JPanel with some additional functionalities such as getting and setting molecules.
A GUI component is created in the example which consists of three major parts:
The String representation of the molecule is called the source of the molecule or simply source.
The image below shows the component that is created:
There are two major operations, export and import. When the Export button is pressed, the source of the current molecule will be set in the text area. The format of the molecule source is determined by the state of a format chooser combo box. Pressing the import button makes the bean read a molecule by converting the string content of the text area to a molecule.
The component has 3 working modes:
Creating the JavaBean component is very easy. With a single line of code it is ready and can simply added to any Swing container.
sketchPane = new MSketchPane();
The complexity rises at using the parameters of the bean since there are many of them. The following examples will show the ones that are the most frequently used.
The following method exports the current molecule being visible on the MarvinSketch canvas in the desired format.
String molS = sketchPane.getMol(format);
The content of the MarvinSketch canvas can be changed by setting a molecule. This replaces the current molecule by discarding it, and it can only be retrieved using the Undo command. If the format is not set explicitly, MSketchPane automatically detects the format of the molecule being set in the String representation.