Is there a way to return the character position in a JTextField. I mean, if I have a JTextField with some values ββin it. For example, the field contains the value ABCDEFJ. The user decides to place the cursor immediately after the 'C' character to enter a new value. Is there any way to get where it enters a new character. In this example, that will return 3.
JTextField.getCaretPosition()
JTextField.setCaretPosition(int pos)
Try using the CaretListener interface:
CaretListener
public class A extends JFrame implements CaretListener { //Assume you have a text field. public A() { JTextField field = new JTextField("bla bla"); field.addCaretListener(this); ..... } public void caretUpdate(CaretEvent e) { int index = e.getDot(); ..... } }
getDot() method of the CaretEvent class returns the desired result, you can assign it to a global variable, which will be used later.
getDot()
CaretEvent
Here is your answer:
http://docs.oracle.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getCaretPosition%28%29
Use an ActionListener to wait for an action. When the user types, find the position of the carriage.
Source: https://habr.com/ru/post/917998/More articles:Using jQuery to add metadata to title tag - jqueryCan I use JTable data for a Jasper report - javahttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/917995/android-adding-components-to-relativelayout&usg=ALkJrhgy0vk-WngeXFzZEwECzkVTPnxzvAEntity Framework Code First: how to map a flat table to a class with nested objects - c #HttpClient coding in .NET 4.5 - c #Launch Apache Felix 4.0.2 on IntelliJ IDEA 11 - javaPowerShell: Converting an Object to a String - powershell"Error: JAVA_HOME is not defined correctly." in construction Jikes rvm - javaHow can we declare a local variable and infer its value? - asp.net-mvcHow to start IPython Notebook with the specified namespace - ipythonAll Articles