Popular Posts
Word break tag : <wbr/> (HTML5) The  HTML  <wbr>  tag  is  used  defines  a  potential  line  break  point  if  needed.  This  stands  for  Word  BReak. This  is  u... CTE, recursive search WITH DepartmentSearch(DeptID, DeptParent, DeptName, OuID) AS (     -- 找出簽核者所屬部門     SELECT d.DeptID, d.DeptParent, d.DeptName, d.OuID     FR... Create web service client cross SSL with eclipse When creating web service cross SSL using eclipse, it occuss some error like below: And it can't build the client source from this wa...
Stats
CookSwing
CookSwing is a library which builds Java Swing GUI from XML documents. It is under continuously active development, since I make a living by doing Swing GUI :) Unlike many other XUL toolkits, Swing is complete in its capability dealing with Swing and beyond. It is mature and stable. If you like this library, please help spreading the word.

CookSwing: XML to Java Swing GUI

MyPanel.xml
<?xml version="1.0" encoding="UTF-8"?>
<panel>
    <flowlayout>
        <label id="lbHello" text="Hello World Again!" horizontalalignment="CENTER" foreground="#ff0000" font="Serif,bold italic,20" />
        <button text="test" actionlistener="btnTest_Click" />
    </flowlayout>
</panel>
MyPanel.java
package cs;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import cookxml.cookswing.CookSwing;

public class MyPanel extends JPanel {

    public String msg;

    // action variant for CookSwing
    public Action btnTest_Click = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), MyPanel.this.toString());
        }
    };

    // constructor with arguments
    public MyPanel(String msg) {
        this.msg = msg;
    }

    // customized method, this method can't be assign to rendered object
    public void randomCode() {
        // do something
        JOptionPane.showConfirmDialog(JOptionPane.getRootFrame(), "Test code");
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("CookSwing basic usage");

        MyPanel panel = new MyPanel("My name is bruce.");
        CookSwing cs = new CookSwing(panel);
        Container container = cs.render("cs/MyPanel.xml");

        // pane & container are two different instance
        System.out.println(panel);
        System.out.println(container);
        // get object by id
        System.out.println(cs.getId("lbHello").object);

        frame.setContentPane(container);
        frame.pack();

        Dimension sc = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((sc.width - frame.getWidth()) / 2, (sc.height - frame.getHeight()) / 2);
        frame.setVisible(true);
    }

}

It is a pity that CookSwing, not like SwiXml, can't render object itself, but create a new instance for using. In this condition, I msut create a component by assign a instance (variant) and a xml document, instead of by using new . And I can't specify a public method for redered object to invoke some process.