0

我有一个小项目要完成。我必须解析一个 XML 文件,然后显示到用户界面(swing)上。我做了一些研究并尝试了 SAX 解析。现在,我正在考虑如何将对象模型转换为 UI。这是我找到的教程链接: http ://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

4

2 回答 2

2

在这里,我们完成了您想要的事情。

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;

public class JTableComponent {
    public static void main(String[] args) {
        new JTableComponent();
    }

    public JTableComponent() {
        JFrame frame = new JFrame("Creating JTable Component Example!");
        JPanel panel = new JPanel();

        DefaultTableModel aModel = new DefaultTableModel() {
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        Staff s1 = new Staff("vinod1", "BCA", "A", "10000");
        Staff s2 = new Staff("vinod2", "BCA", "A", "20000");
        Staff s3 = new Staff("vinod3", "BCA", "A", "30000");
        Staff s4 = new Staff("vinod4", "BCA", "A", "40000");
        ArrayList<Staff> l = new ArrayList<Staff>();// here you will get parser
                                                    // object this is as sample
        l.add(s1);
        l.add(s2);
        l.add(s3);
        l.add(s4);
        Iterator<Staff> is = l.iterator();

        String col[] = { "firstname", "lastname", "nickname", "salary" };
        aModel.setColumnIdentifiers(col);
        JTable table = new JTable(aModel);
        while (is.hasNext()) {
            Staff t = is.next();
            String firstname = t.getFirstname();
            String lastname = t.getLastname();
            String nickname = t.getNickname();
            String salary = t.getSalary();
            String temp[] = { firstname, lastname, nickname, salary };
            aModel.addRow(temp);
        }
        table.setModel(aModel);
        panel.add(table, BorderLayout.CENTER);

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Bean 为您显示 xml 文件

public class Staff 
{
    String firstname= "";
    String  lastname ="";
    String nickname = "";
    String salary = "";

    public Staff(String firstname,String lastname,String nickname,String salary)
    {
         this.firstname= firstname;
         this.lastname =lastname;
         this.nickname = nickname;
         this.salary = salary;
    }

    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getSalary() {
        return salary;
    }
    public void setSalary(String salary) {
        this.salary = salary;
    }

}
于 2012-10-30T09:40:59.917 回答
1

可能是 XMLEditorKit http://java-sl.com/xml_editor_kit.html会有所帮助。它允许解析 XML 文件并在 JEditorPane/JTextPane 中显示它们。

您可以获取源代码并添加您自己的表示。

于 2012-10-30T10:40:10.070 回答