Java by API/javax.accessibility/AccessibleText
AccessibleText: getCaretPosition()
import java.awt.Dimension;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleText;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainClass extends JFrame {
MainClass() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(300, 50));
JTextField jtf = new JTextField(20);
jtf.setPreferredSize(new Dimension(100, 20));
p.add(jtf);
getContentPane().add(p);
pack();
setVisible(true);
}
public static void main(String[] args) {
MainClass ad8 = new MainClass();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
ad8.dumpTextInfo(ad8.getAccessibleContext());
}
void dumpTextInfo(AccessibleContext ac) {
AccessibleText at = ac.getAccessibleText();
if (at != null) {
System.out.println("Caret position = " + at.getCaretPosition());
System.out.println("Selected text = " + at.getSelectedText());
return;
}
int nChildren = ac.getAccessibleChildrenCount();
for (int i = 0; i < nChildren; i++)
dumpTextInfo(ac.getAccessibleChild(i).getAccessibleContext());
}
}
AccessibleText: getSelectedText()
import java.awt.Dimension;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleText;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainClass extends JFrame {
MainClass() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(300, 50));
JTextField jtf = new JTextField(20);
jtf.setPreferredSize(new Dimension(100, 20));
p.add(jtf);
getContentPane().add(p);
pack();
setVisible(true);
}
public static void main(String[] args) {
MainClass ad8 = new MainClass();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
ad8.dumpTextInfo(ad8.getAccessibleContext());
}
void dumpTextInfo(AccessibleContext ac) {
AccessibleText at = ac.getAccessibleText();
if (at != null) {
System.out.println("Caret position = " + at.getCaretPosition());
System.out.println("Selected text = " + at.getSelectedText());
return;
}
int nChildren = ac.getAccessibleChildrenCount();
for (int i = 0; i < nChildren; i++)
dumpTextInfo(ac.getAccessibleChild(i).getAccessibleContext());
}
}