本文整理匯總了Java中javax.swing.JEditorPane.setSelectionStart方法的典型用法代碼示例。如果您正苦於以下問題:Java JEditorPane.setSelectionStart方法的具體用法?Java JEditorPane.setSelectionStart怎麽用?Java JEditorPane.setSelectionStart使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JEditorPane
的用法示例。
在下文中一共展示了JEditorPane.setSelectionStart方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: insertChar
import javax.swing.JEditorPane; //導入方法依賴的package包/類
protected void insertChar(String original, char insertText, String expected, String selection, boolean codeTemplateMode) throws Exception {
String source = original;
String reformatted = expected;
Formatter formatter = getFormatter(null);
int sourcePos = source.indexOf('^');
assertNotNull(sourcePos);
source = source.substring(0, sourcePos) + source.substring(sourcePos+1);
int reformattedPos = reformatted.indexOf('^');
assertNotNull(reformattedPos);
reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);
JEditorPane ta = getPane(source);
Caret caret = ta.getCaret();
caret.setDot(sourcePos);
if (selection != null) {
int start = original.indexOf(selection);
assertTrue(start != -1);
assertTrue("Ambiguous selection - multiple occurrences of selection string",
original.indexOf(selection, start+1) == -1);
ta.setSelectionStart(start);
ta.setSelectionEnd(start+selection.length());
assertEquals(selection, ta.getSelectedText());
}
BaseDocument doc = (BaseDocument) ta.getDocument();
if (codeTemplateMode) {
// Copied from editor/codetemplates/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandler.java
String EDITING_TEMPLATE_DOC_PROPERTY = "processing-code-template"; // NOI18N
doc.putProperty(EDITING_TEMPLATE_DOC_PROPERTY, Boolean.TRUE);
}
if (formatter != null) {
configureIndenters(doc, formatter, true);
}
setupDocumentIndentation(doc, null);
runKitAction(ta, DefaultEditorKit.defaultKeyTypedAction, ""+insertText);
String formatted = doc.getText(0, doc.getLength());
assertEquals(reformatted, formatted);
if (reformattedPos != -1) {
assertEquals(reformattedPos, caret.getDot());
}
}
示例2: getPane
import javax.swing.JEditorPane; //導入方法依賴的package包/類
protected JEditorPane getPane(String text) throws Exception {
if (!SwingUtilities.isEventDispatchThread()) {
fail("You must run this test from the event dispatch thread! To do that, add @Override protected boolean runInEQ() { return true } from your testcase!");
}
String BEGIN = "$start$"; // NOI18N
String END = "$end$"; // NOI18N
int sourceStartPos = text.indexOf(BEGIN);
int caretPos = -1;
int sourceEndPos = -1;
if (sourceStartPos != -1) {
text = text.substring(0, sourceStartPos) + text.substring(sourceStartPos+BEGIN.length());
sourceEndPos = text.indexOf(END);
assertTrue(sourceEndPos != -1);
text = text.substring(0, sourceEndPos) + text.substring(sourceEndPos+END.length());
} else {
caretPos = text.indexOf('^');
if (caretPos != -1) {
text = text.substring(0, caretPos) + text.substring(caretPos+1);
}
}
JEditorPane pane = new JEditorPane();
pane.setContentType(getPreferredMimeType());
final NbEditorKit kit = ((NbEditorKit)getEditorKit(getPreferredMimeType()));
Thread preload = new Thread(new Runnable() {
@Override
public void run() {
// Preload actions and other stuff
if (kit instanceof Callable) {
try {
((Callable) kit).call();
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}
kit.getActions();
}
});
preload.start();
preload.join();
pane.setEditorKit(kit);
pane.setText(text);
BaseDocument bdoc = (BaseDocument)pane.getDocument();
bdoc.putProperty(org.netbeans.api.lexer.Language.class, getPreferredLanguage().getLexerLanguage());
bdoc.putProperty("mimeType", getPreferredMimeType());
//bdoc.insertString(0, text, null);
if (sourceStartPos != -1) {
assertTrue(sourceEndPos != -1);
pane.setSelectionStart(sourceStartPos);
pane.setSelectionEnd(sourceEndPos);
} else if (caretPos != -1) {
pane.getCaret().setDot(caretPos);
}
pane.getCaret().setSelectionVisible(true);
return pane;
}