Problem with keyPressed
in
Programming Questions
•
1 year ago
Hi can anyone help with this?
I'm trying to avoid repeating code unnecessarily, so I'm creating a function:
void cursorSet(String cut, String empty, int cutLength, Textfield t)
{
int cut = empty.substring(0, empty.length() - 1);
int cutLength = cut.length();
if ( cutLength < 1 ) cut = " ";
if ( t.isFocus() && keyCode != SHIFT )
{
if ( key != BACKSPACE && keyCode != RIGHT && key != TAB ) empty = empty + " ";
if ( key == BACKSPACE || keyCode == LEFT ) empty = cut;
if ( keyCode == RIGHT && empty.length() < t.getText().length() ) empty = empty + " ";
if ( empty.length() == t.getText().length() && keyCode != LEFT) empty = cut + " ";
}
}
I want to call this multiple times from keyPressed() like so:
cursorSet(cut1, empty1, cutLength1, t1);
cursorSet(cut2, empty2, cutLength2, t2);
cursorSet(cut3, empty3, cutLength3, t3);
The code within cursorSet() works perfectly in keyPressed(), yet calling it as a method (as above) doesn't do anything.
It's the same code! why doesn't it work? I don't want to have to explicitly write the same code 15 times!
1