2015-12-13 35 views

回答

0

一種方法是從模式「編輯器」中獲取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); 
       } 
      } 
     } 
    } 
} 
+0

謝謝,我是非常有幫助的,:) – hjupiter