我正在开发一个应用程序,当我从列表中选择一个值(文件)时,应该在另一种形式的jTextPane中打开它。我正在使用两个面板,一个是显示我的列表的mainpanel,另一个是ExcelSheet,当我单击列表值时,mainpanel关闭并显示新表单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();}
使用Action封装,为了显示给定文件更新文本窗格中的代码。您可以从ListSelectionListener添加到中调用操作JList。您也可以使用动作菜单项或工具栏按钮,如图所示这里。ImageApp是一个相关的例子。
Action
ListSelectionListener
JList
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 } }