|
Before we begin, please open a window Java API reference. It helps explaining things with the reference by the side. Contents
Hello World!For some reason, most tutorials start with a "hello world!" example. This tutorial is no exception. For 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 components 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 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.
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. GUI LayoutCheck out the LayoutManager Tags Reference for a complete list of layout managers. There are simpler sample codes for each layouts. Begin ActionCookSwing also provides the facility of hooking up listeners with various Swing components. This demo basically shows that. To hook up an ActionListener with a Button, for instance, one just needs to have the action listener as a public variable: public ActionListener buttonAction = new ActionListener () { public void actionPerformed (ActionEvent e) { JOptionPane.showMessageDialog (null, e.getActionCommand ()); } }; When constructing the CookSwing object, it is necessary to tell CookSwing the object where variables can be found. CookSwing cookSwing = new CookSwing (this); Then in the XML document, you can have the buttonAction hooked up with the button. <button text="action button" actionlistener="buttonAction" /> Synchronized ButtonsA common problem using Java Swing is the synchronization of button states for menu items and toolbar buttons. Usually it can be done by hard coding the synchronization. Here we present a better solution, some what worry free for the programmer. In the previous action demo, you may notice that menu item 2 is synchronized with toolbar button 2, and menu item 3 is synchronized with toolbar button 3. The synchronization can be done in the following steps.
JTable, JList, etcTable Demo XML and Java Web Start Demo List Demo XML and Java Web Start Demo CookSwing provides tags for for creating JTable, JList, JTree, JCombox etc and provides tags for DefaultTableModel, DefaultListModel, DefaultComboBoxModel. What is missing is a good model for creating a tree model. I think that it is best left to the programmer to devise the syntax for the XML document that builds the tree model. It is very easy to extend the tag library to do so. ResourceBundle and LocaleThis demo demonstrates how to change string, icons etc based on the Locale. A string starts with @ will be used as a key to lookup the necessary resource from the resource bundle. If the key could not be found, then the original string is returned as-is. Beyond SwingAlthough this tutorial puts emphasis on creating Swing components, CookSwing actually does much more than just Swing components. As you might have seen from previous examples, CookSwing can be used to configure various string and icon resources. It is also possible to use it to load start up configurations etc. Questions? Comments? You can either contact me (although I may not have time to respond all your emails), or post it in the forum. |