java 如何在 JTextField 中移动光标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31322783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move the cursor position in a JTextField
提问by mmgro27
I have a JTextField inputTextField
and I would like the symbol ∨
to be added to the text field at the cursor position when the orButton
is pressed. The method below works but after the button is pressed the cursor appears at the end of the field instead of at the position it was before the button is pressed.
我有一个JTextField inputTextField
,我希望∨
在orButton
按下时将符号添加到光标位置的文本字段中。下面的方法有效,但按下按钮后,光标出现在字段的末尾,而不是按下按钮之前的位置。
How can I set the cursor to move back to its previous position?
如何设置光标移回之前的位置?
private void orButtonActionPerformed(java.awt.event.ActionEvent evt)
{
int caretPosition = inputTextField.getCaretPosition();
String currentText = inputTextField.getText();
String newText = currentText.substring(0, caretPosition) +
"∨" + currentText.substring(caretPosition, currentText.length());
inputTextField.setText(newText);
inputTextField.requestFocus();
}
I thought setCaretPosition()
might be what I was looking for but it didn't work.
我想这setCaretPosition()
可能是我要找的东西,但没有用。
回答by Axel
setCaretPosition()
should be the right method. However it seems the caret is set to position 0
again when the text field gets the focus. You could try wrapping it inside a SwingUtilities.invokeLater()
call like this:
setCaretPosition()
应该是正确的方法。但是,0
当文本字段获得焦点时,插入符号似乎再次设置为定位。您可以尝试将其包装在这样的SwingUtilities.invokeLater()
调用中:
private void orButtonActionPerformed(java.awt.event.ActionEventevt) {
final int caretPosition = inputTextField.getCaretPosition();
String currentText = inputTextField.getText();
String newText = currentText.substring(0, caretPosition) +
"∨" + currentText.substring(caretPosition, currentText.length());
inputTextField.setText(newText);
inputTextField.requestFocus();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
inputTextField.setCaretPosition(caretPosition);
}
});
}
Note that you have to declare caretPosition
as final for this to work.
请注意,您必须声明caretPosition
为 final 才能使其工作。
回答by Gobinath
setCaretPosition works as expected. You need to call this before or after calling requestFocus method. The working example is provided below:
setCaretPosition 按预期工作。您需要在调用 requestFocus 方法之前或之后调用它。下面提供了工作示例:
private void orButtonActionPerformed(java.awt.event.ActionEvent evt)
{
int caretPosition = inputTextField.getCaretPosition();
String currentText = inputTextField.getText();
String newText = currentText.substring(0, caretPosition) +
"∨" + currentText.substring(caretPosition, currentText.length());
inputTextField.setText(newText);
inputTextField.requestFocus();
inputTextField.setCaretPosition(caretPosition);
}
回答by Roberto Attias
what resets the position of the caret is the setText()
, not the field getting the focus. If you put the setCaretPosition()
between setText()
and requestFocus()
it should work.
重置插入符号位置的是setText()
,而不是获得焦点的字段。如果你把它放在两者setCaretPosition()
之间setText()
,requestFocus()
它应该可以工作。
This is a working example:
这是一个工作示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
JTextField tf = new JTextField();
JButton b = new JButton("button");
tf.setText("abcdefghijklmnop");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
int caretPosition = tf.getCaretPosition();
String currentText = tf.getText();
String left = currentText.substring(0, caretPosition);
String right = currentText.substring(caretPosition, currentText.length());
String newText = left + "v" + right;
tf.setText(newText);
tf.setCaretPosition(caretPosition+1);
tf.requestFocus();
}
});
JPanel p = (JPanel)f.getContentPane();
p.setLayout(new BorderLayout());
p.add(b, BorderLayout.EAST);
p.add(tf, BorderLayout.CENTER);
f.setSize(640, 400);
f.setVisible(true);
}
}