Tutorial 1: Hello World!

For some reason, most tutorials start with a "hello world!" example. This tutorial is no exception.

In this example, we are going to have a JFrame and a JLabel as the frame's content pane. As you may notice, all the tags and attribute names are in lower case. CookSwing is quite case sensitive, and all tags and attributes should be in lower case. Another thing is that all java Swing component tags have their first letter J removed. Thus JFrame becomes frame and JLabel becomes label.

What attributes can be set for an object directly correspond to the setters and publicly accessible variables of that object. For example, in case of JFrame, it has setTitle(String) function, thus one could assign a String object to the attribute title. In case of setSize(Dimension) function, however, one must convert the string "640,480" to a dimension object some how, before such assignment can be taken place. CookSwing provides a number of converters that convert a string value to various target object types, such as Date and Color. More can be found in the converters reference page.

HelloWorld.java

import cookswing.CookSwing;

public class HelloWorld { public static void main (String[] args) { CookSwing cookSwing = new CookSwing (); cookSwing.render ("examples/xml/helloworld.xml").setVisible (true); } }

Let's explain the code step by step.

  1. CookSwing cookSwing = new CookSwing ();
    This line of code creates a CookSwing object, which can be used multiple times to parse XML documents.
  2. cookSwing.render ("examples/xml/helloworld.xml").setVisible (true);
    This line of code parses the XML document and obtains a Container object. The function render actually is just a call to xmlDecode and assumes the return value is a Container object. Then setVisible is just a Container function that display the component.
The Java code should be simple. After all, that is a part of reason using the toolkit, to reduce the burden of hard coding complex GUI elements.