动态生成界面

自动从xml页面生成java swing界面的功能,不过比较简单:
xslt可能生成web页面,也就是html或者其它脚本型的界面比较方便。

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<ConfigDialogTitle>BMC Patrol 采集配置</ConfigDialogTitle>
<ConfigGroup>
<title>基本配置</title>
<configItem name="采集点名称"></configItem>
<configItem name="采集点描述"></configItem>
<configItem name="采集点ID"></configItem>
</ConfigGroup>
<ConfigGroup>
<title>Patrol Agent连接配置</title>
<configItem name="Patrol Agent IP"></configItem>    
<configItem name="Patrol Agent 端口"></configItem>
<configItem name="Patrol Agent 用户"></configItem>
<configItem name="Patrol Agent 密码"></configItem>
</ConfigGroup>
</config>

代码:

Digester digest=new Digester();
        digest.addObjectCreate("config","com.ubi.config.demo.ConfigDialog");
        digest.addCallMethod("config/ConfigDialogTitle","setTitle", 0);
        digest.addObjectCreate("config/ConfigGroup", "com.ubi.config.demo.ConfigTab");
        digest.addCallMethod("config/ConfigGroup/title","setName",0);
        digest.addCallMethod( "config/ConfigGroup/configItem", "addConfigItem", 1 );
        digest.addCallParam( "config/ConfigGroup/configItem", 0,"name" );
        digest.addSetNext( "config/ConfigGroup", "addConfigTab" );

        try {
            BufferedReader r=new BufferedReader(new StringReader(cfgTemplate));
            Object obj=digest.parse(r);
            ConfigDialog dlg=(ConfigDialog)obj;
            dlg.setReaderTypeID(this.readerTypeIntID);
            dlg.setSize(new Dimension(600,400));
            dlg.show();
            System.out.println("parsed object is "+obj);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

相关类:

/*
 * Created on 2005-3-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ubi.config.demo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import com.ubi.config.demo.listener.CheckBoxTreeCellRenderer;

/**
 * @author ibm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ConfigDialog extends JDialog implements ActionListener{
    JTabbedPane configTabs;
    int readerTypeID;
    public void setReaderTypeID(int id){
        readerTypeID=id;
    }
    JPanel contentP=new JPanel();
    public ConfigDialog(){
        super();
        contentP.setLayout(new BorderLayout());
        this.setContentPane(contentP);
        this.setSize(new Dimension(200,150));
        configTabs=new JTabbedPane();
        contentP.add(configTabs, BorderLayout.CENTER);
        JPanel btnPane=new JPanel();
        contentP.add(btnPane, BorderLayout.SOUTH);
        btnPane.setLayout(new FlowLayout());
        JButton btnAdd=new JButton("确定");
        JButton btnCacel=new JButton("取消");
        btnPane.add(btnAdd);
        btnPane.add(btnCacel);
        btnAdd.addActionListener(this);
        btnCacel.addActionListener(this);
    }

    public void addConfigTab(ConfigTab newTab){
        configTabs.addTab(newTab.getName(), newTab);
    }
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent arg0) {
        if (arg0.getActionCommand().equals("取消")){
            this.hide();
        }
        if (arg0.getActionCommand().equals("确定")){
            buildEventFilterPage();
        }
    }
    void buildEventFilterPage(){
        APMJTree events=new APMJTree("事件过滤");
        events.setCellRenderer(new CheckBoxTreeCellRenderer());
        ResultSet rs=null;
        try {
            rs=ConfigMain.db.executeQuery("select * from coll_pnt_inst_event where coll_pnt_inst_uid="+this.readerTypeID);
            while (rs.next()){
                events.buildPath(rs.getString("coll_pnt_inst_event_id"));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                rs.getStatement().close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        JScrollPane scroll=new JScrollPane(events);
        events.expandRow(0);
        JDialog dlg=new JDialog();
        dlg.setTitle("采集事件过滤");
        dlg.getContentPane().setLayout(new BorderLayout());
        dlg.getContentPane().add(scroll,BorderLayout.CENTER);
        dlg.setSize(400,300);
        dlg.setModal(true);
        dlg.show();
    }
}
/*
 * Created on 2005-3-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ubi.config.demo;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author ibm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ConfigTab extends JPanel{
    public ConfigTab(){
        super();
        this.setLayout(null);
    }
    int  nextPointX=20;
    int positionY=20;
    int space=50;

    public void addConfigItem(String itemName){
        JLabel lb=new JLabel(itemName);
        lb.setBounds(positionY,nextPointX,150,20);        
        this.add(lb);
        JTextField txt=new JTextField();
        txt.setBounds(positionY+155,nextPointX,150,20);
        nextPointX+=space;
        this.add(txt);           
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值