JavaFX 2 setting table carriage position edits TextField after focus request

In JavaFX 2, I created a custom TableCell that overrides the startEdit () method to request focus. Therefore, as soon as I call the edit command in the cell, the edit text box that appears appears.

The next step is to set the caret position to the end of the text field. But for unknown reasons, this does not work. It is always placed before the 1st char.

Here is what I have tried so far:

public void startEdit() { if (!isEmpty()) { super.startEdit(); createTextField(); setText(null); textField.end(); setGraphic(textField); ((TextField)getGraphic()).end(); textField.end(); Platform.runLater( new Runnable() { @Override public void run() { getGraphic().requestFocus(); } }); } } public void startEdit() { if (!isEmpty()) { super.startEdit(); createTextField(); setText(null); setGraphic(textField); textField.end(); Platform.runLater( new Runnable() { @Override public void run() { getGraphic().requestFocus(); textField.end(); ((TextField)getGraphic()).end(); } }); } } public void startEdit() { if (!isEmpty()) { super.startEdit(); createTextField(); setText(null); textField.end(); setGraphic(textField); ((TextField)getGraphic()).end(); getGraphic().requestFocus(); ((TextField)getGraphic()).end(); textField.end(); } } 

The logical approach would be to request focus on the text box and then move the caret, but it doesn't seem to work for any reason.

Can someone enlighten me?

+4
source share
1 answer

I had the same problem, and after I tried many different things, I finally found a solution.

Don't ask me why, but the problem seems to be caused by creating a new TextField for each edit. If you reuse an existing text box, it works!

So try the following:

 public void startEdit() { if (!isEmpty()) { super.startEdit(); if (textField == null) createTextField(); else textField.setText(getString()); setText(null); setGraphic(textField); Platform.runLater( new Runnable() { @Override public void run() { textField.requestFocus(); textField.deselect(); textField.end(); } }); } } 
+1
source

Source: https://habr.com/ru/post/1440746/


All Articles