【问题标题】:How to access jtextPane in a different form?如何以不同的形式访问 jtextPane?
【发布时间】:2013-05-08 12:49:03
【问题描述】:

我正在开发一个应用程序,当我从列表中选择一个值(文件)时,它应该在不同形式的 jTextPane 中打开。我正在使用两个面板,一个是显示我的列表的主面板,一个是 ExcelSheet,当我单击列表值时,主面板关闭并显示新表单 ExcelSheet,但不显示 jTextPane 中 doc 文件的内容。

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();}

【问题讨论】:

  • 这是一个对象,你可以在任何你喜欢的地方传递它
  • 向您的 Excelsheet 添加一个方法,该方法接受一个值(可以是 jTextpane 或只是一个字符串),然后使用该方法将值传递给 ExcelSheet 对象。

标签: java swing netbeans jtextpane


【解决方案1】:

使用Action 封装更新文本窗格以显示给定文件的代码。您可以从添加到您的JListListSelectionListener 调用该操作。您还可以在菜单项或工具栏按钮中使用该操作,如here 所示。 ImageApp 是一个相关的例子。

例如,您的每个操作实例都需要目标文本窗格和文件:

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
    }
}

【讨论】:

    猜你喜欢
    • 2020-02-29
    • 2017-08-14
    • 2012-01-26
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2019-04-06
    • 1970-01-01
    相关资源
    最近更新 更多