JavaFX How to stop cursor from moving to a new line in a TextArea when Enter is pressed - javafx

I have a Chat application. I'd like the cursor in the chatTextArea to get back to the position 0 of the TextArea chatTextArea.
This, however, won't work:
chatTextArea.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
if (ke.getCode().equals(KeyCode.ENTER)) {
ChatClient.main(new String[]{"localhost", String.valueOf(4444), chatTextArea.getText()});
chatTextArea.setText("");
chatTextArea.positionCaret(0);
}
}
});
How can I get it to work? Thank you.

The TextArea internally does not use the onKeyPressed property to handle keyboard input. Therefore, setting onKeyPressed does not remove the original event handler.
To prevent TextArea's internal handler for the Enter key, you need to add an event filter that consumes the event:
chatTextArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
if (ke.getCode().equals(KeyCode.ENTER)) {
ChatClient.main(new String[]{"localhost", String.valueOf(4444), chatTextArea.getText()});
chatTextArea.setText("");
// chatTextArea.positionCaret(0); // not necessary
ke.consume(); // necessary to prevent event handlers for this event
}
}
});
Event filter uses the same EventHandler interface. The difference is only that it is called before any event handler. If an event filter consumes the event, no event handlers are fired for that event.

Related

Xamarin Forms - Bind Label to a Command

Is there a way to bind a Label to a command so when certain event (not a touch event) has occurred, the command will fire?
Has anyone done something like this?
When you are using MVVM, we can able to fire an event in the code behind..
See the below sample:
In the Xaml code behind, override OnBindingContextChanged() method and register property changed event in it. So whenever, the value changes in the bindable property, this event will be fired. You can check the property name inside this event and do your logic.
view model declaration,
private MyApplicationsViewModel bindingv;
BindingContext override,
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
bindingv = (this.BindingContext as MyApplicationsViewModel);
if (bindingv != null)
{
bindingv.PropertyChanged += Bindingv_PropertyChanged;
}
}
PropertyChanged event method should be below, You can add your logic by checking the property that you have assigned before,
async void Bindingv_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(bindingv.FirstName)) // your property name which is used in the label binding
{
}
}

Event when adding a related page to a document

Apart from LogChange is there a more specific event that gets fired when adding/deleting a related page to a document ?
I want to be able to remove a page from cache when this happens
Have you tried using the global events defined by the CMS.DocumentEngine.RelationshipInfo.TYPEINFO.Events property? For example:
public class CustomRelationshipEvents : CMSLoaderAttribute
{
public override void Init()
{
RelationshipInfo.TYPEINFO.Events.Insert.After += Insert_After;
}
private void Insert_After(object sender, ObjectEventArgs e)
{
// Clear cache here
}
}
Then add your CustomRelationshipEvents attribute to an extension of Kentico's CMSModuleLoader class...

how to invoke fragmentB method in another fragmentA

I have a fragment_A with tabs, consider tabs as fragment_B and C. And am implementing custom keypad with "Done" key in it. In my main Activity iam calling the listener to press the done button
// Used when "Done" button pressed in keyboard
#Override
public void keylisten() {
((Housing) fragmentStack.lastElement()).whenokkeypressed();
}
Now i want to call a method from fragment_B which goes into the whenokkeypressed() of the fragment_A;
There are 2 things you should do
Make whenonkeypressed() static and call it from class name of the fragment like Fragment_A.whenonkeypressed()
(optional) instead of using keylisten of done in mainActivity you should prefer an anonymous inner class like
editText.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
whenonkeypressed();
} });

How to let the listener run without freezing on JavaFX?

What I'm trying to do is printing texts on textarea in real time
even after the button is clicked.
When I click the button, the UI freezes until its job finishes, and after that,
it prints all the texts together unlike System.out.println.
The code below is what I tried, and it doesn't work as I expected.
UI just hangs and doesn't show me 'test' on textarea.
There's no need to use the listener, and it's okay to use appendText only,
but I just can't find out how to let the ActionEvent for the button work without freezing the UI.
I will really appreciate your any help or much better code!!
final TextField announcement = new TextField();
Platform.runLater(new Runnable() {
#Override public void run() {
announcement.textProperty().addListener(new javafx.beans.value.ChangeListener<String>() {
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
textoutput.appendText(announcement.getText());
}
});
}
});
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
announcement.setText("test");
// bunch of codes below
}
});
Have you tried using Platform.runLater? I have code where I have long-running Tasks that periodically updates the UI using the Platform.runLater() calls. It does this by running the Runnable on the JavaFX application's main thread so it is executed separately from your long task.
E.g. (using Java8 lambdas - you could just pass in a new Runnable implementation instead; I prefer lambdas in this situation for clarity's sake):
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// Somewhere in your new Task()
Platform.runLater(() -> {
// Update UI
announcement.setText("test");
});
// bunch of codes below
}
});
The reason for this is that your event handler is executing on FX thread, so UI cannot be updated until it has finished. As already suggested by ItachiUchiha, you should put your code inside Task. When you need to update text in TextArea, use Tasks updateMessage() method. Put a ChangeListener on Tasks message property, and set text from within that listener.

Adding server-side event to extender control

I have an extender control that raises a textbox's OnTextChanged event 500ms after the user has finished typing. The problem with this is that OnTextChanged gets raised when the textbox loses focus, which causes problems (because of the postback).
What I'd like to do is give the extender control its own server-side event (say, OnDelayedSubmit) so I can handle it separately. The event will originate in the extender control's behavior script (after the 500ms delay), so putting a __doPostBack in onchanged is not an option.
Can anyone shed light on how to go about this?
After plenty of reading up on extender controls and JavaScript, I've cobbled together a solution that seems to be working so far.
The main trick was getting the necessary postback code from server-side to the client-side behavior script. I did this by using an ExtenderControlProperty (which is set in the control's OnPreRender function), and then eval'd in the behavior script. The rest was basic event-handling stuff.
So now my extender control's .cs file looks something like this:
public class DelayedSubmitExtender : ExtenderControlBase, IPostBackEventHandler
{
// This is where we'll give the behavior script the necessary code for the
// postback event
protected override void OnPreRender(EventArgs e)
{
string postback = Page.ClientScript.GetPostBackEventReference(this, "DelayedSubmit") + ";";
PostBackEvent = postback;
}
// This property matches up with a pair of get & set functions in the behavior script
[ExtenderControlProperty]
public string PostBackEvent
{
get
{
return GetPropertyValue<string>("PostBackEvent", "");
}
set
{
SetPropertyValue<string>("PostBackEvent", value);
}
}
// The event handling stuff
public event EventHandler Submit; // Our event
protected void OnSubmit(EventArgs e) // Called to raise the event
{
if (Submit != null)
{
Submit(this, e);
}
}
public void RaisePostBackEvent(string eventArgument) // From IPostBackEventHandler
{
if (eventArgument == "DelayedSubmit")
{
OnSubmit(new EventArgs());
}
}
}
And my behavior script looks something like this:
DelayedSubmitBehavior = function(element) {
DelayedSubmitBehavior.initializeBase(this, [element]);
this._postBackEvent = null; // Stores the script required for the postback
}
DelayedSubmitBehavior.prototype = {
// Delayed submit code removed for brevity, but normally this would be where
// initialize, dispose, and client-side event handlers would go
// This is the client-side part of the PostBackEvent property
get_PostBackEvent: function() {
return this._postBackEvent;
},
set_PostBackEvent: function(value) {
this._postBackEvent = value;
}
// This is the client-side event handler where the postback is initiated from
_onTimerTick: function(sender, eventArgs) {
// The following line evaluates the string var as javascript,
// which will cause the desired postback
eval(this._postBackEvent);
}
}
Now the server-side event can be handled the same way you'd handle an event on any other control.

Resources