How to show result in JFrame Form if data source is a XML file?

99封情书 提交于 2020-01-06 17:28:32

问题


Here is the codes:

I write XML file to a comment under this post.

Developer:

public class Developer {

    private String id;
    private String name;
    private String surname;
    private int age;

    public Developer(String id, String name, String surname, int age) {
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public int getAge() {
        return age;
    }
}

The method:

public static void main(String[] args) throws Exception {

    File xmlFile = new File("data.xml");

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    org.w3c.dom.Document document = documentBuilder.parse(xmlFile);

    NodeList list = document.getElementsByTagName("Developer");

    List<Developer> developers = new ArrayList<>();

    for (int i = 0; i < list.getLength(); i++) {

        Node node = list.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {

            Element element = (Element) node;

            Developer developer = new Developer(
                element.getAttribute("Id"),
                element.getElementsByTagName("Name").item(0).getTextContent(),
                element.getElementsByTagName("Surname").item(0).getTextContent(),
                Integer.parseInt(element.getElementsByTagName("Age").item(0).getTextContent())
            );
            developers.add(developer);
        }
    }

    // at this point we have a list of developers

    // print out all the developers
    for (Developer developer : developers) {
        System.out.println("ID: " + developer.getId());
        System.out.println("Name: " + developer.getName());
        System.out.println("Surname: " + developer.getSurname());
        System.out.println("Age: " + developer.getAge());
    }
}

JFrame code:

public class window extends javax.swing.JFrame {


    public window() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        rbClick = new javax.swing.JButton();
        lbResult = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        rbClick.setText("Click");

        lbResult.setText("Result");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbResult)
                    .addComponent(rbClick))
                .addContainerGap(203, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(rbClick)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbResult)
                .addContainerGap(202, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new window().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel lbResult;
    private javax.swing.JButton rbClick;
    // End of variables declaration                   
}

How to do the following? If i click the rbClick button, in the lbResult show only the persons who's age from 18 to 24?

来源:https://stackoverflow.com/questions/41192935/how-to-show-result-in-jframe-form-if-data-source-is-a-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!