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 the following working modes:
The following paragraphs demonstrate the major parts of the [SketchSimple.java
] code.
Creating the JavaBean component is very easy. With a single line of code, it is ready and can simply be 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.
sketchPane.setMol(molS);
The MarvinSketch bean fires a java.beans.PropertyChangeEvent
every time the molecule is modified or replaced, which can be handled by using a java.beans.PropertyChangeListener
.
sketchPane.addPropertyChangeListener("mol", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if(behavior == EXPORTER) {
exportActionPerformed();
}
}
});