Keep Selection in JavaFX Textfield even if application is not focussed - css

Background:
I'm trying to implement a find/replace function for java-Fx table view. Whenever I find an occurance of the text to find, I switch the table view into edit mode and select the text found programatically in the textfield which is used during edit mode.
The focus in this situation remains in the modal find/replace dialog, so the selection in the textfield is not visible until I close the modal dialog.
The problem:
A textfield in Java-Fx shows it's selection only while it has the focus (as its standard behaviour). When the Java-Application loses focus the selection becomes invisible, when it gets the focus back the selection is visible again.
Here is the question:
Is it possible to keep the selection visible in a textfield though focus is lost?
What I tried:
I expected that CSS could help here:
textField.setStyle("-fx-highlight-fill: lightgray");
This changes the color of the highlighting but
I wanted to keep the original color for the focussed Textfield
I wanted to change the color from"invisible" to "lightgray" only for
the non-focussed Textfield
What is the correct way?
Or is there a way to highlight text in a TableView without activating the TextField?
Thanks
Ingo

If I am understanding correctly, the solution will be in the modality of your find/replace dialog.
It works for me when I do the following: Note that the java file this is written in extends Stage. If you are not extending stage then just call the methods on your find/replace stage.
owner = myApplicationStage;
initModality(Modality.NONE);//important for the solution!
initOwner(owner);
initStyle(StageStyle.UTILITY);
setScene(myFindAndReplaceScene);
stage = this;
stage.show();
This works perfectly for me, if you have any problems then hopefully I can help.

Related

Is there a way to show tooltip on disabled QWidget

I have a Qt form, where I have a button and menu. For various reasons I can disable certain elements, e.g button or some actions in the menu.
Is there a way I could show a tooltip or when the mouse is hovered over the disabled button or menu item with an explanation as to why it is disabled?
I am using Qt 4.8.
Thanks!
You can set the tooltip dynamically based on the state of the QWidget or by simply toggling both at the same time. Upon disabling/enabling the widget from somewhere just call QWidget::setToolTip(...) with the QString you want the tooltip to display when hovering with the mouse over the given widget. For example if you have a public slot called toggleButton(bool toggleFlag) which toggles the enable-setting of a button you can do:
void MyWidget::toggleButton(bool toggleFlag) {
this->ui->myButton->setEnabled(toggleFlag);
this->ui->myButton->setToolTip(toggleFlag ? QString("Enabled wohoo!") : QString("Disabled because I like it"));
}
You can of course do also change the tooltip by calling QWidget::isEnabled() and act upon its return value. Since you haven't given any code I can only assume how you toggle your button(s) so that's all I can give you for now.
UPDATE: It was pointed in the comments that tooltips don't work with disabled widgets due not receiving mouse events. Both statements are not true (note that I have used the same tooltip message since due to lack of minimal working example I didn't want to write a whole new project from scratch and used an existing one of mine instead):
Hovering a disabled button triggers the tooltip
Hovering an enabled button triggers the tooltip

How to lose selection focus of a QTextEdit?

I have a readonly QTextEdit. I'm able to select the text and copy it.
What I need to do: When the user is in selection mode(selecting text), if he leaves the area (with the left mouse button still holding) the widget must lose focus and selection focus must end too. When mouse exits out of bounds I want to be similar to mouse release. I tried to set this:
rootWidget->clearFocus();
rootWidget->close();
rootWidget->releaseMouse();
but even though visually the widget seems out of focus I still receive the TextEdit::mouseMoveEvent() of my widget.
I also tried to set focus to a dummy widget. The dummy widget gets focused but my textedit sitll gets mouseMoveEvent().
How can I lose focus of the widget completely without releasing the mouse button?
Thank you for any suggestion!
You can give the focus to something else using QWidget::setFocus(). you should probably clear the selection before changing focus.

What are the advantages of putting TextField inside Button (JavaFX)

In SceneBuilder it is possible to put TextField inside a Button:
Is it for just decorative purposes or it has some functionality like when clicking button it gets child info and so on? If it has some functionality, are there any examples?
From the documentation:
A simple button control. The button control can contain text and/or a
graphic.
The graphic is changeable for every subclass of Labeled.
You can put any node as graphic into e. g. the button. It gives you a very high level of customization.
A good example is the customization of the header of a TitledPane.
As for your concrete example I see no advantage, rather a disadvantage as it's not common to have a textfield inside a button. But you nevery know with what people come up with.

Changing colour of the text in Javafx textfield WHILE typing

i'm new to javafx. currently i'm trying to change the colour of a text INSIDE a textfield as the user is typing. I'm not trying to change the colour of the text somewhere else but in the textfield itself.
I've searched everywhere and everyone is talking about how to change the colour in another textfield, using Text object or TextFlow. But I'm trying to change the colour of a particular word when user is typing.
For example, if user type " go to school by 2pm" , i would like to change the colour of the word "by" to green (in the textfield itself when the user is typing).
is there anyway to achieve this? Or can this not be done?
Thank you in advance :)
You cannot change the color of an individual word in a TextField, only all words in the TextField. The closest you can do is subvert the selection highlight mechanism, but that is really a bit of a hack and isn't really applicable for your context where the user is actively typing in the TextField.
Rather than using the inbuilt TextField (which is not a good fit for your requirements), you might want to investigate alternate options such as Tomas Mikula's RichTextFX control.

How to keep focus on Spark TextInput after setting StageWebView source

I have a mobile application that has a Text Input used for searching. Below the search TextInput is a StageWebView. When I set the source of the StageWebView using loadURL() the key input is shifted to the StageWebView.
How can I prevent this?
I think I figured out the problem. When you set the stage property (which is basically setting the visibility to true) that's when it steals the focus. I was showing and hiding the web view depending on if the text input had any text (I was updating the webview source on text change). The fix was to set the web view to visible before putting the cursor in the Text Input. As long as the visibility doesn't change the focus stays in the text input.
I dont know if there is a better way but my solution is to try to set the focus back to the textInput by using myText.setFocus() after your load routine.

Resources