So,i have a bunch of textfields inside a scrollpane. More textfields are added when the user focuses the last one from the bottom. My question is,how can i make the scrollpane scroll to the focused textfield ? I mean,the user will press TAB or ENTER to jump to the next textfield but he won't be able to see it without scrolling himself. I could simulate pressing the down arrow or PageDown when the last textfield has focus but that would be ugly,even if it would do what i need to.
I tried something like this from what i've found thru searching but i couldn't make it work.
public void focusGained(FocusEvent e) {
currentview = t1;
int cons = i - 1;
Rectangle r = new Rectangle(t1.getX(), t1.getY(), 1, 1);
jScrollPane1.scrollRectToVisible(r);
if (t1.getName().equals("prod" + cons)) {
newproduct();
};
}
Two things arise.
Firstly,
JTextField#scrollRectToVisible
has being overridden and does something slightly different from other components...annoying....Secondly, you need to be converting the fields location relative to the it's parent and then using the parent's
scrollRectToVisible
method, for example...