我正在尝试将我的XML数据放入jtable中,但它没有在面板上显示任何结果而没有显示任何错误

时间:2017-06-03 01:09:25

标签: java xml swing user-interface jtable

我试图将我的XML文件数据放在JTable的Movies.java的帮助下,但我不能。它没有向我显示任何类型的错误,但它也没有把它放在面板中。

myXmlFile:

@SuppressWarnings("serial")
public class TabMovieListPanel extends JPanel {


    private static CreateMovieXML moviexml;

    private static JButton del;
    JTable labels;
    private DefaultTableModel model;
    private JTable jTable = new JTable(model);

    private TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(jTable.getModel());

    private JTextField jtfFilter = new JTextField();
    private JButton jbtFilter = new JButton("Filter");


    public void TabMovieListPanel() {

        moviexml = new CreateMovieXML();

        String[] columnNames = { "TITLE", "GENERE", "DURATION", "DIRECTOR", "YEAR" };

        for (Movies currentValue : moviexml.getMoviesList()) {
            System.out.println(currentValue.toString());

            Object[][] data = { { currentValue.getTitle(), currentValue.getGenere(), currentValue.getDuration(),
                    currentValue.getDirector(), currentValue.getYear() } };
        }

        jTable.setRowSorter(rowSorter);

        JPanel panel = new JPanel(new BorderLayout());

        setLayout(new BorderLayout());

        jtfFilter.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = jtfFilter.getText();

                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = jtfFilter.getText();

                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); 
            }

        });

        del = new JButton("Delete");
        del.setFont(new Font("Stencil", Font.ITALIC, 20));
        del.setPreferredSize(new Dimension(100, 40));
        del.addActionListener(new DelListener());
        panel.add(new JLabel("Specify a word to match:"), BorderLayout.WEST);
        panel.add(jtfFilter, BorderLayout.CENTER);

        add(panel, BorderLayout.SOUTH);
        add(new JScrollPane(jTable), BorderLayout.CENTER);
        add(del);

    }

    private static class DelListener implements ActionListener {

        @SuppressWarnings("static-access")
        @Override
        public void actionPerformed(ActionEvent evt) {

        }

    };

    void reload() throws IOException {
        this.removeAll();
        // this.loadDataIntoPanel();
    }
}
public class Movies {

    private String title;
    private String genere;
    private String director;
    private String duration;
    private String year;

    public Movies(String title, String genere, String director, String duration, String year) {
        this.title = title;
        this.genere = genere;
        this.director = director;
        this.duration = duration;
        this.year = year;
    }

    public Movies() {

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenere() {
        return genere;
    }

    public void setGenere(String genere) {
        this.genere = genere;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return title + "," + genere + "," + director + "," + duration + "," + year;
    }
}
public class CreateMovieXML {

    private static ArrayList<Movies> moviesList = new ArrayList<Movies>();

    public CreateMovieXML() {

        try {
            readFile();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // DOM WRITER
    @SuppressWarnings("unused")
    public static void writeIntoFile(String title, String genere, String director, String duration, String year)
            throws ParserConfigurationException, SAXException, IOException {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse("Movies.xml");

        Element elementRoot = document.getDocumentElement();
        Collection<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies());

        for (Movies movie : movies) {

            Element elementMovie = document.createElement("movie");
            Element elementtitle = document.createElement("title");
            elementtitle.appendChild(document.createTextNode(title));
            elementMovie.appendChild(elementtitle);
            Element elementgenere = document.createElement("genere");
            elementgenere.appendChild(document.createTextNode(genere));
            elementMovie.appendChild(elementgenere);
            Element elementdirector = document.createElement("director");
            elementdirector.appendChild(document.createTextNode(director));
            elementMovie.appendChild(elementdirector);
            Element elementduration = document.createElement("duration");
            elementduration.appendChild(document.createTextNode(duration));
            elementMovie.appendChild(elementduration);
            Element elementyear = document.createElement("year");
            elementyear.appendChild(document.createTextNode(year));
            elementMovie.appendChild(elementyear);

            elementRoot.appendChild(elementMovie);
        }

        DOMSource input = new DOMSource(document);
        String path = "movies.xml";

        Result res = new StreamResult(path);
        TransformerFactory tf = TransformerFactory.newInstance();

        try {
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            try {
                transformer.transform(input, res);
                System.out.println("Data written successfully");
            } catch (TransformerException e) {
                e.printStackTrace();
            }
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }

    }

    // DOM READER
    public void readFile() throws FileNotFoundException {

        try {
            moviesList.clear();
            File input = new File("movies.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(input);
            doc.getDocumentElement().normalize();
            // System.out.println("Root element :" +
            // doc.getDocumentElement().getNodeName());
            NodeList nodeList = doc.getElementsByTagName("movie");
            // System.out.println("----------------------------");

            for (int temp = 0; temp < nodeList.getLength(); temp++) {

                Node node = nodeList.item(temp);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) node;

                    Movies movie = new Movies(eElement.getElementsByTagName("title").item(0).getTextContent(),
                            eElement.getElementsByTagName("genere").item(0).getTextContent(),
                            eElement.getElementsByTagName("duration").item(0).getTextContent(),
                            eElement.getElementsByTagName("director").item(0).getTextContent(),
                            eElement.getElementsByTagName("year").item(0).getTextContent());

                    moviesList.add(movie);

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<Movies> getMoviesList() {
        return moviesList;
    }
}

0 个答案:

没有答案