【问题标题】:read a code file open in netbeans with a plugin使用插件读取在 netbeans 中打开的代码文件
【发布时间】:2015-12-13 04:37:43
【问题描述】:

我已经完成了一个插件 netbenas,我的问题是如何读取在 netbeans 中打开的文件的内容,即,如果我在点击插件中的按钮后打开了 test.java 读取

【问题讨论】:

    标签: java netbeans plugins


    【解决方案1】:

    一种选择是从“编辑器”模式中获取 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);
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-27
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 2015-04-06
      • 2019-03-26
      • 2014-07-09
      相关资源
      最近更新 更多