Hello guys.
I'm writing an editor application using javafx, with some special features like:
1) by pressing the ENTER key inside the first text field, a new text field will be created just at the bottom of the previous one.
2) by pressing the ALT+ENTER key, a new text field will be created at the bottom and it will have extra indentation in compare to it's upper text field.
The point is that all the newly generated text field's, has the same response to ENTER and ALT+ENTER key's as the first field does.
a)When i try to write a handler and inside the handle method, assign the handler itself to the new generated text field(recursive pattern) i face an exception that makes sense to me because the handler has not declared yet;
b)I'm already aware of writing a custom MyTextField class extending the original TextField, but i don't know which method i should override for this key event handling that is inside a text field, and i think overriding replaceText() and replaceSelection() methods, is not a solution to my case.
I have used a POJO to assign details like indentation for each text field by using setUserData() method on each instance:
class Info{
private int columnNumber; // unit of indentation for each text field.
private int indent; // real indentation value, computed using the column number.
public Info(int columnNumber)
{
this.columnNumber= columnNumber;
this.indent=columnNumber*25; // indentation by factor of 25px.
}
setters & getters
}
I have another method that insert the new text field to my layout pane(in my case: VBox) by using an index as a parameter
public void insert (TextField tf , int index)
{
Info info = (Info) tf.getUserData();
double indent = info.getIndent();
vbox.getChildren().add(index,tf);
Insets margin = new Insets( 0 , 0 , 0 , indent ); //Applying indentation
vbox.setMargin(tf,margin);
}
before i pass each instance to insert() , i set the user data:
TextField tf = new TextField();
tf.setUserData ( new Info( 1 ) ); //assuming that i want this field on the column number: 1 (25 px indentation)
then:
insert( tf , index );
and finally, the main part, i came up with the idea of this method:
public void recursiveHandler ( TextField tf )
{
Info info=(Info) tf.getUserData(); //extract the info about the current node.
tf.setOnKeyReleased( e -> {
if (e.isAltDown() && e.getCode()==KeyCode.ENTER)
{
TextField tf2 = new TextField();
int index = getIndex(tf); //current index.
tf2.setUserData( new Info ( info.getColNo() + 1 ) ); //the newly created text field will be on the next column.
insert ( tf2,index );
recursiveHandler ( tf2 ); // recursively assign the handling code to the newly created field.
}
else if (e.getCode()== KeyCode.ENTER)
{
TextField tf2 = new TextField();
int index = getIndex(tf); //current index.
tf2.setUserData(new Info( info.getColNo() ) ); //the newly created text field will be on the same column.
insert ( tf2 , index );
recursiveHandler ( tf2 );
}
}
}
I know that the pattern i used is some sort of stupied, i will be really glad if you guys help me with your opinion, as in this case there are more key combinations inside text field and using this pattern has made my code dirty and it has affected the performance of javafx too.
with all respect.
Thank u!