JavaFX requestFocus() deletes contents of TextField when opening with spacebar - java

I'm running into an issue with the requestFocus() method. Whenever I click on a link from a VBox table in my application, a details menu drops down indicating username etc which are editable. The problem is, when I open the link in the VBox using the spacebar, JavaFX puts a focus on the TextField, but highlights it. By the time I release the spacebar, the highlighted text is deleted.
Basically, when the TextField is given focus using a spacebar, the entire contents are highlighted and replaced with a space character. Pushing enter to open the link still highlights the text, but doesn't do anything else since it doesn't add any characters.
How do I make sure the text inside a TextField doesn't get highlighted?
Thanks.

The solution for TextField (entire content) doesn't get highlighted is adding the .selectedEnd() after the .requestFocus()
textField.requestFocus();
textField.selectEnd();
However, i'm not really sure for your case that deal with spacebar. Hope this help.

Related

How to check if the insertion point is on a bold or normal text

I am making a text application where I can change the text to bold (I will add more in the future).
I put the bold option in a JMenu as a JCheckBox.
I want this checkbox to get selected or deselected depending on whether the insertion point (the blinking line) is on a bold or normal text. Just like it is in Microsoft Word.
The application
Quick answer from my phone:
You need to add a CaretListener to the document. When triggered, you will detect the character style with StyledDocument.getCharacterAttributes. To get the bold attribute, you check it with StyleConstants.isBold. Be careful to not trigger your bold action while setting the check box state.

How to remove text from textflow in javafx?

I'm new to javafx and am trying to display large amount of text in textflow. It displays fine, but I just cannot figure out how to delete the text.
So what I'm trying to do is delete all Text nodes from textFlow like so
textFlow.getChildren().removeAll();
But when I do this and add something to textFlow, it shows after the text that was already displayed there. I would like the text that was there removed and show the added text from the beginning of the textflow.
I guess I have to somehow rerender the view of textflow, but I don't know how. So how do I delete everything and add Text anew?
removeAll(...) will remove all the values you pass as parameters: in your case there are none, so it doesn't remove anything. Use
textFlow.getChildren().clear();

Several TextFields and search Button in Vaadin

I have several TextFields and a search Button in Vaadin. I would like to execute my search button with Enter key, no matter in which TextField I am. First I initialize all my components and set
searchButton.setClickShortcut(KeyCode.ENTER);
searchButton.setImmediate(true);
which works all times except the first time after opening my VerticalLayout.
I also tried Both ENTER shortcut and TextArea in Vaadin, to set ENTER key to every single TextField, which works anytime, except the first time, when I open my VerticalLayout.
If I do the same for ESCAPE key it works all the times.
Have any suggestion?
Thanks in advance.

Un-editing / removing focus from a Vaadin TextField

Does anyone know how to cancel the editing state of a Vaadin TextField that's inside a Table? My concrete use case is that I have a table with multiple TextFields in it, that show different values when they are edited as opposed to viewed. The user clicks on a TextField, this makes the field display its editable content. If the user changes his mind and wants to stop editing the field (by pressing ESC) the field should cancel any changes the user might have made to its content and revert to displaying its view value.
So far I've been able to move the focus to another TextField but this is not what I ultimately want to achieve. Moving the focus to the parent Table would be acceptable, but doing so programmatically by calling table.focus() does not produce any visible results.
Thanks for your help,
Radu
To revert the value of any field - including TextField - before it gets committed, AbstractField#discard()

JTable custom cell renderer focus problem

I have a table like this. The second column uses a JTextField renderer and the third column uses a JPasswordField based renderer and editor.
Looks good. But the problem is We have to type the values and must hit "ENTER". In that image, I have typed my password but didn't hit Enter. So If I click the 'Save & Close' button, it'll show me an error that password field is empty.
Previously I have used only JTextFields and JPasswordFields under JTabbedPane and it worked well. When I had to add more and more stuff, I changed it to a table like this.
For now I have put a label to let people know that they should hit the ENTER.. This is not nice. Another big issue. Atleast in Nimbus Look and feel, we get an idea that that field is still in focus. In Windows system look, there's not much visible difference whether the field is focused or not.
I need the Username field or password field to set it's value when I click 'Save & Close' button. Please help me.
So your problem is, that you are still editing the cell. So you have to stop the editing and then the cell will be changed.
At your button you can get the cell who is being edited with
TableCellEditor cellEditor = table.getCellEditor();
then you can stop the editing with
if(cellEditor!=null){
cellEditor.stopCellEditing();
}
and then you can save the value
Tell the table to automatically commit when losing focus:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

Categories