How to access jtextPane in a different form?

你说的曾经没有我的故事 提交于 2019-11-26 23:40:54

问题


I am developing an application where, when I select a value(file) from list it should be opened in jTextPane of a different form. I am using two panels one is mainpanel where my list is shown and one is ExcelSheet, when i click on a list value then mainpanel is closed and new form ExcelSheet is displayed but not the content of doc file in jTextPane.

XWPFWordExtractor extractor=null;
    File file=null;
    String str=(String) list.getSelectedValue();
    mainPanel.setVisible(false);
    new ExcelSheet().setVisible(true);
    ExcelSheet obj=new ExcelSheet();
        try {
             file=new File("C:\\Users\\Siddique Ansari\\Documents\\CV Parser\\"+str);   


        FileInputStream fis=new FileInputStream(file.getAbsolutePath());
        XWPFDocument document=new XWPFDocument(fis);
        extractor = new XWPFWordExtractor(document);
        String fileData = extractor.getText();
        Document doc = obj.jTextPane1.getDocument();      

            System.out.println(fileData);
            doc.insertString(doc.getLength(), fileData, null);

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

回答1:


Use Action to encapsulate the code that updates the text pane in order to display a given file. You can invoke the action from a ListSelectionListener added to your JList. You can also use the action in a menu item or a toolbar button, as shown here. ImageApp is a related example.

For example, each instance of your action will need the target text pane and file:

class FileAction extends AbstractAction {

    JTextPane target;
    File file;

    public FileAction(JTextPane target, File file) {
        this.target = target;
        this.file = file;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // render file in target
    }
}


来源:https://stackoverflow.com/questions/16441169/how-to-access-jtextpane-in-a-different-form

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