Computer Chemistry Consultancy - XML4Pharma, Katzelbachweg 18, A-8052 Thal, Austria, info@CompChemCons.com

Goodies

Periodic Table
Radioactive Decay
JDBCtoXML
JNumberField
JIntegerField

This page contains a number of goodies, code that you may download, copy, change, ...
However, this is code "as is", so we cannot guarantee that it works 100% perfectly.
If you find any bugs in the code, please let us know.

See also our achievements for other projects

Periodic Table of Elements Java Bean

There are a lot of "Table of Elements" - applets on the internet. However, this one is written as a Java Bean, and it comes with source code ! The Java Bean itself is implemented as a JPanel, so that you can easily integrate it in other panels and containers, as part of a larger application. The bean has a large number of "getters and setters" so that you can easily adapt it to what you want with very little programming effort. A demo is given where the bean is incorporated in a Dialog (i.e. it works as a chemical element chooser).


Radioactive Decay Calculator

This Java Radioactive Decay Applet was designed, constructed, tested and deployed in just somewhat more than 2 days. It shows that when a developer has the right expertise, and making use of reusable compounds, a robust application can be made in very little time.
The applet is made modular, so that we can easily integrate it in any web-application, such as a Radioactive Materials Supply Management System.
The applet enables to calculate radioactivity levels at any date when the activity at a certain date is known. Also one can calculate the date at which a certain radioactivity was or will be reached for the given nuclide. This applet is free for use on intranet or internet.

This applet is very popular: it has been downloaded over 1500 times from our website (status March 2004).


JDBCToXML

When you retrieve information within a Java application/applet/servlet, you usually do this by JDBC (or JDBC-ODBC coupling). You submit an SQL statement and you retrieve a Java Resultset. The following Java class (with an additional helper class file) transforms the Resultset into XML. The class is an extension of Danny Ayers JDBCToXML class. The transformation can be done in 2 ways:
  • as XML without attributes (default). E.g.:

    <?xml version="1.0"?>
    <!DOCTYPE SalaryList SYSTEM "Salaries.dtd">
    <SalaryList>
    <EMPLOYEE>
    <ENAME>SMITH</ENAME>
    <SAL>800</SAL>
    </EMPLOYEE>
    <EMPLOYEE>
    <ENAME>ALLEN</ENAME>
    <SAL>1600</SAL>
    </EMPLOYEE>
    </SalaryList>

    • as XML with the information in the attributes

      <?xml version="1.0"?>
      <!DOCTYPE SalaryList SYSTEM "Salaries.dtd">
      <SalaryList>
      <EMPLOYEE ENAME="SMITH" SAL="800"/>
      <EMPLOYEE ENAME="ALLEN" SAL="1600"/>
      </SalaryList>
Where the SQL statement was: SELECT ENAME, SAL FROM EMP

A typical way the class is used consists of:

rsXML = stmt.executeQuery("SELECT ENAME, SAL FROM EMP");
JDBCToXML jtox = new JDBCToXML();
jtox.setDocumentName("SalaryList");
jtox.setDoctype(true, "Salaries.dtd"); //whether the doctype is external or not
jtox.setRecordName("EMPLOYEE");
jtox.setAsAttribute(true); // all the information in the attributes
String s = jtox.getXML(rsXML);

The XML is returned as a String that already contains carriage returns, so that it can easily be written to a file using some outputstream.
Danny Ayers original class returned the XML with the information in the attributes. This class returns it as XML without attributes by default, but you can choose to do it Danny's way using the method:

jtox.setasAttributes(true)

Here is the code:

JDBCToXML.java
XMLUtil.java, a helper class

Important Remark: You can get in serious difficulties if the data that come from the database themselve contain characters like >, <, " or &. In order to avoid problems we strongly advise you to use the filter method of the class "ServletUtilities" that can be found below, or at http://www.coreservlets.com.
This class and method filters out these characters and replaces them by the strings &gt; , &lt; , &quot; and &amp;.

Do you understand why ?

You can find the answer in the following section


Filtering characters for use in XML

When reading text (from a form, database, etc..) and putting the text in an XML element or attribute, some characters can lead to invalid XML data. For example, if you add the text "doses <5 mg or >10 mg", this can be recognized as an XML element (<5 or>), but as it does not occur in the DTD, the XML file is considered invalid by applications (the element name is invalid anyway, as it contains a blank).

To avoid such problems, software that reads text and converts it XML should always filter specific characters, and replace them by their entities. The Java source code to do this is given here. It comes from the book "Servlets and Java Server Pages" by Marti Hall.


JNumberField

JNumberField is a Swing JTextField that only accepts numbers. Characters that cannot belong to a number are rejected during typing. This means that it is impossible to get an error when reading out the JNumberField and converting it to a number. JNumberField accepts numbers in different formats, also in scientific notation. E.g.:

  • 3.1415927
  • 0.3415927e1
  • 0.3415927E+01
  • 34.15927E-1
  • 34.15927e-01
  • -34.15927E-01
  • etc.
If you try e.g. to type -3.1415A927, the "A" will simply be ignored.

Try it out here !

JNumberField has all the methods of JTextField, plus one additional one: getDoubleValue(). This returns a double.

The constructor is like the normal JTextField constructor, with one addition: you can also determine the width (number of characters) of the JNumberField. E.g.:


    • JNumberField jn = new JNumberField(); //normal constructor
    • or : JNumberField jn = new JNumberField(10); //create a JNumberField that is 10 characters wide.


JNumberField is a class of the package "JosAertsBeans". So be sure to do a proper import when using JNumberField:
e.g.:

import javax.swing.*;
import JosAertsBeans.*;
...
JNumberField jn = new JNumberField(10);
...
double d = jn.getDoubleValue(); // get the double
...

The archive file comes in two versions: one for JDK1.3.x (JNumberField.jar) and one for JDK1.1.x (x>=7) (JNumberField1.jar). We couldn't get JNumberField to work correctly under JDK1.2 (bug in JDK?). However, as Swing is an integral part of Java2, we encourage you to use JNumberField under JDK1.3.x. For applets, this means the use of the Java2-plugin in the browser.

All (including the javadoc documentation) is included in the following ZIP-file:
JNumberField.zip

Please let me know about your experience with JNumberField.


JIntegerField

JIntegerField works very analogously to JNumberField. The main difference is that it only accepts integers. This means that any other typed characters (excluding + and -) are simply rejected during typing.

Try it out here !

JIntegerField has all the methods of JTextField, plus one additional one: getIntValue(). This returns an integer (int).

The constructor is like the normal JTextField constructor, with one addition: you can also determine the width (number of characters) of the JNumberField. E.g.:


    • JIntegerField ji = new JIntegerField(); //normal constructor
    • or : JIntegerField ji = new JIntegerField(10); //create a JIntegerField that is 10 characters wide.


The archive file we provide was created with JDK1.3, so it will not work with earlier versions of the JDK. For applets, this means the use of the Java2-plugin in the browser.

All (including the javadoc documentation) is included in the following ZIP-file:
JIntegerField.zip

Please let me know about your experience with JIntegerField


Acknowledgement: JNumberField is derived from a class constructed by Marcel Dijkgraaf & Harald Lammers, trainees from the "Hogeschool Enschede", which they constructed during their traineeship at Akzo Nobel Central Research at the end of 1999. Thanks Marcel and Harald, you were fine guys !

Computer Chemistry Consultancy, July 2001