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.