当前位置: 首页>>代码示例>>Java>>正文


Java JEditorPane.setCaretPosition方法代码示例

本文整理汇总了Java中javax.swing.JEditorPane.setCaretPosition方法的典型用法代码示例。如果您正苦于以下问题:Java JEditorPane.setCaretPosition方法的具体用法?Java JEditorPane.setCaretPosition怎么用?Java JEditorPane.setCaretPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.JEditorPane的用法示例。


在下文中一共展示了JEditorPane.setCaretPosition方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: performOpen

import javax.swing.JEditorPane; //导入方法依赖的package包/类
private void performOpen(JEditorPane[] panes) {
    if (panes == null || panes.length == 0) {
        StatusDisplayer.getDefault().setStatusText(Bundle.ERR_ShellConsoleClosed());
        return;
    }
    JEditorPane p = panes[0];
    Rng[] fragments = theHandle.getFragments();
    
    int to = fragments[0].start;
    p.requestFocus();
    int pos = Math.min(p.getDocument().getLength() - 1, to);
    p.setCaretPosition(pos);
    try {
        Rectangle r = p.modelToView(pos);
        p.scrollRectToVisible(r);
    } catch (BadLocationException ex) {
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:OpenAction.java

示例2: canDrop

import javax.swing.JEditorPane; //导入方法依赖的package包/类
@Override
public boolean canDrop(DropTargetDragEvent e) {
    //check if the JEditorPane contains html document
    JEditorPane pane = findPane(e.getDropTargetContext().getComponent());
    if (pane == null) {
        return false;
    }
    int offset = getLineEndOffset(pane, e.getLocation());
    if (!containsLanguageAtOffset(pane.getDocument(), offset)) {
        return false;
    } else {
        //update the caret as the user drags the object
        //needs to be done explicitly here as QuietEditorPane doesn't call
        //the original Swings DropTarget which does this
        pane.setCaretPosition(offset);

        pane.requestFocusInWindow(); //pity we need to call this all the time when dragging, but  ExternalDropHandler don't handle dragEnter event

        return canDrop(e.getCurrentDataFlavors());
    }

}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:23,代码来源:CssExternalDropHandler.java

示例3: MyFrame

import javax.swing.JEditorPane; //导入方法依赖的package包/类
public MyFrame() {
    JEditorPane editpane = new JEditorPane();
    editpane.setEditable(false);
    editpane.setText(content);
    editpane.setCaretPosition(0);

    JScrollPane scrollpane = new JScrollPane(editpane);

    add(scrollpane);

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    setSize(new Dimension(200, 200));
    bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
    setResizable(false);
    setVisible(true);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:SynthScrollbarThumbPainterTest.java

示例4: testDoNotAutocompleteQuteInHtmlAttribute

import javax.swing.JEditorPane; //导入方法依赖的package包/类
public void testDoNotAutocompleteQuteInHtmlAttribute() throws DataObjectNotFoundException, IOException, BadLocationException {
    FileObject fo = getTestFile("testfiles/test.html");
    assertEquals("text/html", fo.getMIMEType());

    DataObject dobj = DataObject.find(fo);
    EditorCookie ec = dobj.getCookie(EditorCookie.class);
    Document document = ec.openDocument();
    ec.open();
    JEditorPane jep = ec.getOpenedPanes()[0];
    BaseAction type = (BaseAction) jep.getActionMap().get(NbEditorKit.defaultKeyTypedAction);
    //find the pipe
    String text = document.getText(0, document.getLength());

    int pipeIdx = text.indexOf('|');
    assertTrue(pipeIdx != -1);

    //delete the pipe
    document.remove(pipeIdx, 1);

    jep.setCaretPosition(pipeIdx);
    
    //type "
    ActionEvent ae = new ActionEvent(doc, 0, "\"");
    type.actionPerformed(ae, jep);

    //check the document content
    String beforeCaret = document.getText(pipeIdx, 2);
    assertEquals("\" ", beforeCaret);

}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:31,代码来源:CssBracketCompleterTest.java

示例5: updateTextInAWT

import javax.swing.JEditorPane; //导入方法依赖的package包/类
private void updateTextInAWT(EditorCookie es, final String in) throws IOException, BadLocationException {
    StyledDocument tmpdoc = es.getDocument();
    if (tmpdoc == null)
        tmpdoc = es.openDocument();

    //sample editor position

    JEditorPane[] eps = es.getOpenedPanes();
    JEditorPane pane = null;
    JViewport port = null;
    int caretPosition = 0;
    Point viewPosition = null;
    if (eps != null) {
        pane = eps[0];
        caretPosition = pane.getCaretPosition();
        port = getParentViewport (pane);
        if (port != null)
            viewPosition = port.getViewPosition();
    }

    // prepare modification task

    final Exception[] taskEx = new Exception[] {null};
    final StyledDocument sdoc = tmpdoc;

    Runnable task = new Runnable() {
        public void run() {
            try {
                sdoc.remove (0, sdoc.getLength());  // right alternative

                // we are at Unicode level
                sdoc.insertString (0, in, null);
            } catch (Exception iex) {
                taskEx[0] = iex;
            }
        }
    };

    // perform document modification

    org.openide.text.NbDocument.runAtomicAsUser(sdoc, task);

    //??? setModified (true);  

    //restore editor position

    if (eps != null) {
        try {
            pane.setCaretPosition (caretPosition);
        } catch (IllegalArgumentException e) {
        }
        port.setViewPosition (viewPosition);
    }

    if (taskEx[0]!=null) {
        if (taskEx[0] instanceof IOException) {
            throw (IOException)taskEx[0];
        }
        throw new IOException(taskEx[0]);
    }
    
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:63,代码来源:XMLTextRepresentation.java

示例6: performTest

import javax.swing.JEditorPane; //导入方法依赖的package包/类
protected void performTest(String source, int caretPos, String textToInsert, String toPerformItemRE, String goldenFileName, String sourceLevel) throws Exception {
    this.sourceLevel.set(sourceLevel);
    File testSource = new File(getWorkDir(), "test/Test.java");
    testSource.getParentFile().mkdirs();
    copyToWorkDir(new File(getDataDir(), "org/netbeans/modules/java/editor/completion/data/" + source + ".java"), testSource);
    FileObject testSourceFO = FileUtil.toFileObject(testSource);
    assertNotNull(testSourceFO);
    DataObject testSourceDO = DataObject.find(testSourceFO);
    assertNotNull(testSourceDO);
    EditorCookie ec = (EditorCookie) testSourceDO.getCookie(EditorCookie.class);
    assertNotNull(ec);
    final Document doc = ec.openDocument();
    assertNotNull(doc);
    doc.putProperty(Language.class, JavaTokenId.language());
    doc.putProperty("mimeType", "text/x-java");
    int textToInsertLength = textToInsert != null ? textToInsert.length() : 0;
    if (textToInsertLength > 0)
        doc.insertString(caretPos, textToInsert, null);
    Source s = Source.create(doc);
    List<? extends CompletionItem> items = JavaCompletionProvider.query(s, CompletionProvider.COMPLETION_QUERY_TYPE, caretPos + textToInsertLength, caretPos + textToInsertLength);
    Collections.sort(items, CompletionItemComparator.BY_PRIORITY);
    
    String version = System.getProperty("java.specification.version") + "/";
    
    assertNotNull(goldenFileName);            

    Pattern p = Pattern.compile(toPerformItemRE);
    CompletionItem item = null;            
    for (CompletionItem i : items) {
        if (p.matcher(i.toString()).find()) {
            item = i;
            break;
        }
    }            
    assertNotNull(item);

    JEditorPane editor = new JEditorPane();
    editor.setDocument(doc);
    editor.setCaretPosition(caretPos + textToInsertLength);
    item.defaultAction(editor);

    File output = new File(getWorkDir(), getName() + ".out2");
    Writer out = new FileWriter(output);            
    out.write(doc.getText(0, doc.getLength()));
    out.close();

    File goldenFile = new File(getDataDir(), "/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/" + version + goldenFileName);
    File diffFile = new File(getWorkDir(), getName() + ".diff");

    assertFile(output, goldenFile, diffFile, new WhitespaceIgnoringDiff());
    
    LifecycleManager.getDefault().saveAll();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:54,代码来源:CompletionTestBase.java


注:本文中的javax.swing.JEditorPane.setCaretPosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。