Domanda

I have parsed an XML file with some elements that I want to display in a JList. The parsing is working just fine but displaying it in the Jlist is not working at all.

I have tried to do the following:

  1. Parse the XML.
  2. Add the node into a DefaultModelList.
  3. Add the model into the Jlist.

My code:

public class ReadXMLFile {

    private DefaultListModel model = new DefaultListModel();

    private static ReadXMLFile instance = null;

    public static ReadXMLFile getInstance() {

        if (instance == null) {

            instance = new ReadXMLFile();

        }

        return instance;
    }

    public void ParserForObjectTypes() throws SAXException, IOException,
            ParserConfigurationException {

        try {
            FileInputStream file = new FileInputStream(new File(
                    "xmlFiles/CoreDatamodel.xml"));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                    .newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            XPath xPath = XPathFactory.newInstance().newXPath();

            String expression = "//OBJECT_TYPE";
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
                    xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {

                model.addElement(nodeList.item(i).getFirstChild()
                        .getNodeValue());
                System.out.println(nodeList.item(i).getFirstChild()
                        .getNodeValue());
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

    public DefaultListModel getModel() {
        return model;
    }

}

Then in my GUI constructor I'm calling my initialize method that will my model into the JList. The important part of initialize() looks like this:

private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 823, 515);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JList obTypeJList = new JList(ReadXMLFile.getInstance().getModel());
    JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu contentMenuBar = new JMenu("File");
        menuBar.add(contentMenuBar);

        JMenuItem OpenFileItemMenu = new JMenuItem("Open File");
        contentMenuBar.add(OpenFileItemMenu);

    }


public XmlEditorMain() {
    initialize();
    ReadXMLFile file = new ReadXMLFile();
    try {
        file.ParserForObjectTypes();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}
È stato utile?

Soluzione

You are creating singleton instance, but you forgot to initialize it. Change:

public static ReadXMLFile getInstance() {
       if (instance == null) {
            instance = new ReadXMLFile();
        }
        return instance;
    }

to

public static ReadXMLFile getInstance() {
       if (instance == null) {
            instance = new ReadXMLFile();
            instance.ParserForObjectTypes();
        }
        return instance;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top