Programmatic Access to Jmol

From Jmol

Jump to: navigation, search

Jmol can be embedded in your existing Java applications. After embedding the Jmol Viewer in your application, it can be accessed programmatically. This allows developers to accomplish every thing in Jmol by running scripts.

To embed Jmol and to show the structure of a molecule using its PDB file, follow the example below.

package org.jmoltest;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jmol.adapter.smarter.SmarterJmolAdapter;
import org.jmol.api.JmolAdapter;
import org.jmol.api.JmolSimpleViewer;

public class MyJmolViewer {

   private String title = "Programmatic Access to Jmol";
   JmolSimpleViewer viewer;

   JmolPanel jmolPanel;
   JFrame jframe;

   public MyJmolViewer() {
       try {

           PDBFileReader pdbr = new PDBFileReader();          

           String pdbCode = "5pti";

           Structure struc = pdbr.getStructureById(pdbCode);

           initializeJmolPanel ex = new initializeJmolPanel();

       } catch (Exception e){

   public initializeJmolPanel() {
       frame = new JFrame();
       frame.addWindowListener(new ApplicationCloser());
       Container contentPane = frame.getContentPane();
       jmolPanel = new JmolPanel();

       jmolPanel.setPreferredSize(new Dimension(200,200));

   public void setTitle(String label){

   public JmolSimpleViewer getViewer(){
       return jmolPanel.getViewer();

   static class ApplicationCloser extends WindowAdapter {
       public void windowClosing(WindowEvent e) {

   static class JmolPanel extends JPanel {
       private static final long serialVersionUID = -3661941083797644242L;
       JmolSimpleViewer viewer;
       JmolAdapter adapter;
       JmolPanel() {
           adapter = new SmarterJmolAdapter();
           viewer = JmolSimpleViewer.allocateSimpleViewer(this, adapter);


       public JmolSimpleViewer getViewer() {
           return viewer;

       public void executeCmd(String rasmolScript){

       final Dimension currentSize = new Dimension();
       final Rectangle rectClip = new Rectangle();

       public void paint(Graphics g) {
           viewer.renderScreenImage(g, currentSize, rectClip);

For a shorter example, use Biojava. For more details, visit Biojava Cookbook.

Personal tools