Search code examples
javaswingapache-poijtextpane

Displaying contents of doc file in jTextPane


i m trying to display contents of a doc file into jTextPane. But it is displaying only the last line of document while on console it is displaying whole document. I m using Apache POI library.

File file = null;
    WordExtractor extractor = null ;
    try {

        file = new File("C:\\Users\\Siddique Ansari\\Documents\\CV Parser\\Siddique_Resume.doc");
        FileInputStream fis=new FileInputStream(file.getAbsolutePath());
        HWPFDocument document=new HWPFDocument(fis);
        extractor = new WordExtractor(document);
        String [] fileData = extractor.getParagraphText();
        for(int i=0;i<fileData.length;i++){
            System.out.println(fileData[i]);
            jTextPane1.setText(fileData[i]);

        }
    }
    catch(Exception exep){}

Solution

  • jTextPane1.setText(fileData[i]); will override the current value each time.

    Instead, append to the underlying document:

    Document doc = jTextPane1.getDocument();
    
    // ... in your loop:
    doc.insertString(doc.getLength(), fileData[i], null);