本文整理汇总了Java中javax.swing.JTextArea.setCaretPosition方法的典型用法代码示例。如果您正苦于以下问题:Java JTextArea.setCaretPosition方法的具体用法?Java JTextArea.setCaretPosition怎么用?Java JTextArea.setCaretPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JTextArea
的用法示例。
在下文中一共展示了JTextArea.setCaretPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import javax.swing.JTextArea; //导入方法依赖的package包/类
public void handle(Throwable e) {
e.printStackTrace();
JTextArea area = new JTextArea(10, 40);
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
area.setText(writer.toString());
area.setCaretPosition(0);
String copyOption = resources.getString("dialog.error.copy");
JOptionPane pane = new JOptionPane(new JScrollPane(area), JOptionPane.ERROR_MESSAGE,
JOptionPane.YES_NO_OPTION, null, new String[]{copyOption, resources.getString("cancel")});
pane.createDialog(WorldFrame.this, e.toString()).setVisible(true);
if (copyOption.equals(pane.getValue())) {
area.setSelectionStart(0);
area.setSelectionEnd(area.getText().length());
area.copy(); // copy to clipboard
}
}
示例2: testReplaceAll4
import javax.swing.JTextArea; //导入方法依赖的package包/类
/**
* Test of replaceAll method, of class EditorFindSupport.
*/
@Test
public void testReplaceAll4() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "ahoj");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "xxx");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
JTextArea ta = new JTextArea("0123456789 ahoj ahoj svete");
ta.setCaretPosition(18);
instance.replaceAllImpl(props, ta);
assertEquals("0123456789 ahoj ahoj svete", ta.getText());
}
示例3: testReplaceAll7
import javax.swing.JTextArea; //导入方法依赖的package包/类
/**
* Test of replaceAll method, of class EditorFindSupport.
*/
@Test
public void testReplaceAll7() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "ahoj");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "xxx");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
JTextArea ta = new JTextArea("0123456789 ahojahojahoj svete");
ta.setCaretPosition(ta.getText().length()-1);
instance.replaceAllImpl(props, ta);
assertEquals("0123456789 xxxxxxxxx svete", ta.getText());
}
示例4: testReplaceAll9
import javax.swing.JTextArea; //导入方法依赖的package包/类
/**
* Test of replaceAll method, of class EditorFindSupport.
*/
@Test
public void testReplaceAll9() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "a");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "b");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
JTextArea ta = new JTextArea("aa");
ta.setCaretPosition(1);
instance.replaceAllImpl(props, ta);
assertEquals("bb", ta.getText());
}
示例5: testReplaceAll10
import javax.swing.JTextArea; //导入方法依赖的package包/类
/**
* Test of replaceAll method, of class EditorFindSupport.
*/
@Test
public void testReplaceAll10() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "a");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "b");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
JTextArea ta = new JTextArea("aa");
ta.setCaretPosition(1);
instance.replaceAllImpl(props, ta);
assertEquals("ab", ta.getText());
}
示例6: UncaughtExceptionForm
import javax.swing.JTextArea; //导入方法依赖的package包/类
public UncaughtExceptionForm(Thread t, Throwable e) {
JScrollPane pane = new JScrollPane();
JTextArea text = new JTextArea();
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
text.setText(sw.getBuffer().toString());
text.setEditable(false);
text.setCaretPosition(0);
pane.setViewportView(text);
this.setResizable(false);
this.setIconImage(Main.icon);
this.setTitle(Main.name);
this.setSize(600, 700);
this.setLocationRelativeTo(null);
this.add(pane);
}
示例7: testReplaceAllFinishes_165497_e
import javax.swing.JTextArea; //导入方法依赖的package包/类
/**
* Test of replaceAll method, of class EditorFindSupport for regressions in #165497.
*/
@Test
public void testReplaceAllFinishes_165497_e() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "a");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "a");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
final boolean [] finished = new boolean[1];
Thread t = new Thread(new Runnable() {
@Override
public void run() {
JTextArea ta = new JTextArea("aaaa");
ta.setCaretPosition(2);
instance.replaceAllImpl(props, ta);
finished[0] = true;
}
});
t.start();
Thread.sleep(2000);
if (!finished[0]) {
t.stop();
}
assertTrue(finished[0]);
}
示例8: testReplaceFind
import javax.swing.JTextArea; //导入方法依赖的package包/类
@Test
public void testReplaceFind() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "a");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "b");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
JTextArea ta = new JTextArea("aaaa");
ta.setCaretPosition(0);
instance.replaceImpl(props, false, ta);
instance.findReplaceImpl(null, props, false, ta);
assertEquals("baaa", ta.getText());
instance.replaceImpl(props, false, ta);
instance.findReplaceImpl(null, props, false, ta);
assertEquals("bbaa", ta.getText());
instance.replaceImpl(props, false, ta);
instance.findReplaceImpl(null, props, false, ta);
assertEquals("bbba", ta.getText());
}
示例9: testReplaceFindFocused
import javax.swing.JTextArea; //导入方法依赖的package包/类
@Test
public void testReplaceFindFocused() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "a");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "b");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
JTextArea ta = new JTextArea("aaaa");
ta.setCaretPosition(0);
instance.setFocusedTextComponent(ta);
instance.replace(props, false);
instance.find(props, false);
assertEquals("baaa", ta.getText());
instance.replace(props, false);
instance.find(props, false);
assertEquals("bbaa", ta.getText());
instance.replace(props, false);
instance.find(props, false);
assertEquals("bbba", ta.getText());
}
示例10: testReplaceFindNewLine
import javax.swing.JTextArea; //导入方法依赖的package包/类
@Test
public void testReplaceFindNewLine() throws Exception {
final Map<String, Object> props = new HashMap<>();
props.put(EditorFindSupport.FIND_WHAT, "foo");
props.put(EditorFindSupport.FIND_REPLACE_WITH, "bar");
props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
final EditorFindSupport instance = EditorFindSupport.getInstance();
JTextArea ta = new JTextArea("foo\nfoo\nfoo\nfoo\n");
ta.setCaretPosition(0);
// instance.setFocusedTextComponent(ta);
instance.replaceImpl(props, false, ta);
instance.findReplaceImpl(null, props, false, ta);
assertEquals("bar\nfoo\nfoo\nfoo\n", ta.getText());
instance.replaceImpl(props, false, ta);
instance.findReplaceImpl(null, props, false, ta);
assertEquals("bar\nbar\nfoo\nfoo\n", ta.getText());
instance.replaceImpl(props, false, ta);
instance.findReplaceImpl(null, props, false, ta);
assertEquals("bar\nbar\nbar\nfoo\n", ta.getText());
}
示例11: getDocument
import javax.swing.JTextArea; //导入方法依赖的package包/类
private Document getDocument(String str) {
JTextArea ta = new JTextArea(str);
if (str != null && str.length() > 0) {
ta.setCaretPosition(1);
}
Document doc = ta.getDocument();
return doc;
}
示例12: GoToLine
import javax.swing.JTextArea; //导入方法依赖的package包/类
public static void GoToLine(){
int sel = jTabbedPane1.getSelectedIndex();
JTextArea textPane = (JTextArea) ((JScrollPane) ((JDesktopPane)jTabbedPane1.getComponentAt(sel)).getComponent(0)).getViewport().getComponent(0);
do
{
try
{
String str = (String) JOptionPane.showInputDialog(null,"Enter Line number : "+"(1 - "+getLineCount(textPane)+" )", "GoTo Line",JOptionPane.PLAIN_MESSAGE, null, null, null);
if (str == null)
{
break;
}
int lineNumber = Integer.parseInt(str);
_lineCount = getLineCount(textPane);
if (lineNumber > _lineCount)
{
JOptionPane.showMessageDialog(null,"Line number out of range", "Error....",JOptionPane.ERROR_MESSAGE);
continue;
}
textPane.setCaretPosition(0);
textPane.setCaretPosition(SetCursor(lineNumber,textPane));
return;
}
catch (Exception e) { }
}
while (true);
}
示例13: Console
import javax.swing.JTextArea; //导入方法依赖的package包/类
public Console() {
setLayout(new BorderLayout());
setBorder(new TitledBorder("控制台"));
textarea = new JTextArea();
textarea.setCaretPosition(textarea.getText().length());
textarea.setFont(new Font("新宋体", Font.PLAIN, 12));
add(textarea);
DefaultCaret caret = (DefaultCaret) textarea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
scrollPane = new JScrollPane();
scrollPane.setViewportView(textarea);
add(scrollPane, BorderLayout.CENTER);
welcome();
}
示例14: actionPerformed
import javax.swing.JTextArea; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
JTextArea area = TEdit.getTextArea();
String txt = area.getText();
if(txt == null)
return;
String s = TEdit.getLookingFor();
if(s.contentEquals(""))
return;
if(txt.contentEquals("") || txt.length()<1)
return;
if(area.getCaretPosition() < s.length()-1){
JOptionPane.showMessageDialog(TEdit.getFrame(),"Reached the beginning of the text");
return;
}
if(area.getCaretPosition()-s.length()<=0){
area.setCaretPosition(txt.length()-1);
}
int foundAt = txt.substring(0,area.getCaretPosition()-s.length()).lastIndexOf(s);
if(foundAt == -1){
JOptionPane.showMessageDialog(TEdit.getFrame(),"Reached the beginning of the text");
return;
}
area.setCaretPosition(foundAt);
area.select(foundAt,foundAt+s.length());
}
示例15: actionPerformed
import javax.swing.JTextArea; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
JTextArea area = TEdit.getTextArea();
Highlighter hilite = area.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
int Shift=0;
if(hilites != null){
if(hilites.length == 0 &&
TEdit.getSwingPool().containsKey("area.hilites")){
hilites = (Highlighter.Highlight[])TEdit.getSwingPool().get("area.hilites");
TEdit.getSwingPool().remove("area.hilites");
}
for (Highlighter.Highlight hilite1 : hilites) {
area.replaceRange("",hilite1.getStartOffset()-Shift, hilite1.getEndOffset()-Shift);
Shift = Shift -(hilite1.getEndOffset()-hilite1.getStartOffset());
}
if(hilites.length>0){
area.setCaretPosition(hilites[0].getStartOffset());
area.getCaret().setVisible(true);
}
TEdit.setEnabled("Calc",false);
TEdit.setEnabled("Delete",false);
TEdit.setEnabled("Save",true);
TEdit.setEnabled("SaveAs",true);
}
}