本文整理汇总了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;
}