如何以不同的形式访问 jtextPane? [英] How to access jtextPane in a different form?
问题描述
我正在开发一个应用程序,当我从列表中选择一个值(文件)时,它应该在不同形式的 jTextPane 中打开.我正在使用两个面板,一个是显示我的列表的主面板,一个是 ExcelSheet,当我单击列表值时,主面板关闭并显示新表单 ExcelSheet,但不显示 jTextPane 中的 doc 文件的内容.
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();}
推荐答案
使用 Action
封装更新文本窗格以显示给定文件的代码.您可以从 ListSelectionListener
<调用操作/a> 添加到您的 JList
.您还可以在菜单项或工具栏按钮中使用该操作,如此处所示.ImageApp
是一个相关示例.
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
}
}
这篇关于如何以不同的形式访问 jtextPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!