Popular Posts
Convert pfx file to certificate file and private key file Use openssl Internal Table: declare types ARR type I occurs 10. " 定義長度為10的數值table/array types: begin of CONTACT, " 定義CONTACT為... Install subversion with apache server on linux (ubuntu) # 更新 apt package sudo apt-get update # 安裝 package sudo apt-get install subversion apache2 libapache2-svn apache2-utils # 建立 svn 資料夾 sudo...
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.