0
我有一個插件netbenas完成了,我的問題是我如何讀取netbeans中打開的文件的內容,即如果我打開一個test.java按鈕後觸摸讀取位於插件中用插件讀取netbeans中打開的代碼文件
我有一個插件netbenas完成了,我的問題是我如何讀取netbeans中打開的文件的內容,即如果我打開一個test.java按鈕後觸摸讀取位於插件中用插件讀取netbeans中打開的代碼文件
一種方法是從模式「編輯器」中獲取TopComponents,然後搜索具有您正在查找的名稱的TopComponents。在它上面查找FileObjects(你只能得到一個),你可以用asText()函數獲得文本。
@Override
public void actionPerformed(ActionEvent e) {
TopComponent tcArray[] = WindowManager.getDefault().findMode("editor").getTopComponents();
for (TopComponent tc : tcArray) {
System.out.println("tc = " + tc);
if (null != tc && null != tc.getDisplayName()
&& tc.getDisplayName().equals("test.java")) {
Collection<? extends FileObject> fileobjs = tc.getLookup().lookupAll(FileObject.class);
for (FileObject fo : fileobjs) {
try {
String text = fo.asText();
System.out.println("text = " + text);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
}
謝謝,我是非常有幫助的,:) – hjupiter