How do I make text not appear in a TextField? - javafx

Well... I have a TextField and I would like the typed text not to appear in the component, just be read.
The reason for this is that I want to read a key using the onKeyPressed event and display the key pressed through the getEvent (). GetName () methods of a KeyEvent.
But there are problems:
when you press the Delete key, the letter 'D' is deleted from the TextField text.
when you press the Space key, a space is added at the beginning.
...
This is my code:
this.myTextField.setOnKeyPressed((KeyEvent event) -> {
shortcut = event.getCode();
myTextField.setText(shortcut.getName());
});
shortcut is a KeyCode
myTextField is a TextField
Thanks for the answers!

Use setOnKeyReleased instead of setOnKeyPressed method
textField.setOnKeyReleased(e -> {
KeyCode key = e.getCode();
textField.setText(key.getName());
});

Well...
I've found a solution, and it's very simple.
I only changed the TextField to a Button. Yes, that's it (with a bit of CSS, of course).
The Button has the same events and it have the possibility to set a text. That's all I needed.

Related

Xamarin forms iOS CollectionView steals focus after ItemsSource property is changed

I have a page with a collection view and a search bar that filters its contents.
I want the filtering to happen as the user types in the search bar, so I bind the TextChanged event of the SearchBar to a command in the view model, like so
var eventToCommandBehavior = new EventToCommandBehavior()
{
EventName = nameof(searchBar.TextChanged),
};
eventToCommandBehavior.SetBinding(EventToCommandBehavior.CommandProperty, nameof(MyViewModel.StartOrResetSearchTimerCommand));
searchBar.Behaviors.Add(eventToCommandBehavior);
In the view model:
public ICommand StartOrResetSearchTimerCommand => new Command(() =>
{
StartOrResetSearchTimer();
});
private void StartOrResetSearchTimer()
{
if (!timerStarted)
{
searchTimer = new Timer(_ => PerformSearch(), null, searchTimeout, searchTimeout);
timerStarted = true;
}
else
ResetTimer();
}
private void PerformSearch()
{
//my code
OnPropertyChanged(collectionViewItemsSourceBinding);
}
The StartOrResetSearchTimerCommand filters the ItemsSource binding, and calls OnPropertyChanged(itemsSourceBinding) to update the UI.
On Android and UWP everything works as expected. However on iOS, when OnPropertyChanged is called, the focus moves out of the search bar, resulting to the soft keyboard being closed after each keyboard input.
Has anyone else encountered this? Any suggestions?
I have already tried not using this approach, and only filter the ItemsSource when the search button is pressed, which works, when there is something to search for (ie, there is some input in the search bar)
When the search bar text is empty (ie after Backspace) then the search button is greyed out.
Update
For now, I am using this workaround:
Perfom the search only when the search button is pressed, and on TextChanged, check if the text is empty and reset the ItemsSourceworka
TextChanged is called anytime the text in the query box is changed. You can use this event to update your ItemsSource when the Text of the searchbar changes. You can refer to this part of the official example. First, given your ItemsSource, when you enter text in the searchbar, call the OnTextChanged event to update your ItemsSource, so that the real-time search keyboard will not lose focus.

Xamarin.Forms.EntryCell: How to trigger the event while input any key

I have a problem with Xamarin.Forms.EntryCell. I want to know how to trigger an event if the text of the EntryCell has changed. Not the event 'Completed', it is just triggered when I press Enter after inputting anything.
<EntryCell
Label="User Id"
x:Name="UserIdEntryCell"
HorizontalTextAlignment="End"
Completed="UserIdCompleted"/>
Obviously there is no event that is fired when the text is changed (see the docs). Anyway, this does not mean that you can't achieve what you want, by means of Text property. Since you are using the event I do not assume that you use MVVM (you should really give it a try, though), hence we'll have to create a property we will bind the EntryCell.Text to in your view (I'm assuming a view, but for a page it would be quite similar)
In your code behind add a property Text that calls HandleTextChanged from its setter:
class MyView : ContentView
{
string _text;
public string Text
{
get => _text;
set
{
_text = value;
HandleTextChanged();
}
}
private void HandleTextChange()
{
// do whatever you need to do
}
}
You can bind this property to EntryCell.Text from your XAML
Now MyView.Text will be set every time the text in your EntryCell changes.

C# winforms DGV Add a button to a datagrid with variable text

I know how to add a button to a datagridview. What I am having a problem with is changing the text displayed based on whether a associated record exists in another table. I would like to change the text on the button to "View SR" or "Add SR" depending on if a service request exists for the record. if it even possible, how I would go about this ?
You just need to capture the exact cell and cast it in a Grid View Button Cell to adjust the value displayed. Following is a tried solution:
if (SomeTrueValue)
{
((DataGridViewButtonCell)dataGridView1.Rows[0].Cells[0]).Value = "Hello";
}
else
{
((DataGridViewButtonCell)dataGridView1.Rows[0].Cells[0]).Value = "World";
}

JavaFX FXML - Does Button have OnEdit Like Method?

I have a class that extends Button where a String is constructed like so,
text = button.getText();
However, I need to always update text when it changes i.e. a listener. Does Button have a method like onEdit where I can make sure the button's text is always the same as the variable text?
Register a listener with the button's text property:
button.textProperty().addListener((obs, oldText, newText) -> {
// do whatever you need with newText
});

TextInput Component event is not working

I am working on a AS3 only project in Flex....I tried to listen ENTER event when use clicks enter/return in my textinput box....but it seems not working well...I did try using TextEvent.TEXT_INPUT and it worked fine but not Component.ENTER...any help??? Thanks a lot!!
import fl.events.ComponentEvent;
searchInput=new TextField();
searchInput.type=TextFieldType.INPUT;
searchInput.background=true;
searchInput.backgroundColor=0xecffee;
searchInput.defaultTextFormat=TF;
searchInput.width = 200;
searchInput.height=16;
searchInput.x=50;
searchInput.y=180;
addChild(searchInput);
searchInput.addEventListener(ComponentEvent.ENTER, testEnter);
}
private function testEnter(e:ComponentEvent):void{
if(searchInput.text!=null){
beginSearch(searchInput.text);
}
searchInput.selectable = true;
and maybe
searchInput.mouseEnabled = true;
try this then:
searchInput.addEventListener(MouseEvent.CLICK, testEnter);
private function testEnter(event:MouseEvent):void
{
}
sorry, i was bit too quick on that one...
here it is
searchInput=new TextInput();
TextField doesn't extend the UIComponent class , therefore it can't handle ComponentEvent. Instead of telling you to change the event , I should have told you to change the event handler. My understanding is that you write AS3 projects with Flex so I looked at the TextField first!
I'm not sure if you can use the TextInput class in an AS3 project since it's part of the Flex framework...
in which case you may have to revert to the MouseEvent but not triggered by the TextField though but by a simple 'Search' button
The event you're looking for is 'change' / flash.events.Event.CHANGE which is dispatched when the control value is modified. The flex documentation is quite helpful here: Flex 3 TextInput
If you want to listen to the user pressing ENTER you should use the keyDown-event. Otherwise (like also mentioned) you can use the change-event.
As you are using the TextField-component the right documentation is link text
The TextInput-component has also an valueCommit-event, maybe you can use this one...

Resources