當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。