wicket ajaxformcomponentupdatingbehavior get value of TextField by clicking button - java

I have a form and there is a TextField. I added button to a form, different than the default submit. I want to use Ajaxformcomponentupdatingbehavior to get the value from the TextField after clicking the button next to the TextField.
My code looks like:
private String string;
...
public ..() {
Form form = new Form("form") {
#Override
protected void onSubmit() {
//some code
};
add(form);
TextField textField = new TextField("string", new PropertyModel<String>(this,"string"));
textField.setOutputMarkupId(true);
form.add(textField);
Button button = new Button("evalButton");
form.add(button);
button.add(new AjaxFormComponentUpdatingBehavior("onclick") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.print(textField.getValue());
}
});
the value of the TextField is null, after clicking the button for the second time, I get the right value. How could I get the value of the TextField after one button click?

AjaxFormComponentUpdatingBehavior doesn't do quite what you think. The behaviour is actually applied to the TextField, not to the button. Your code is updating the model of the button rather than the text. See this previous question for an example.
I've done this before for a postcode lookup button in an address form. I used an `IndicatingAjaxButton' to push the entire form, and I disabled default form processing. I then grabbed the text input directly, pushed it through my validator which standardised the formatting, then processed:
final IndicatingAjaxButton lookup = new IndicatingAjaxButton("lookup", form) {
#Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
String code = postcode.getInput();
code = (new PostcodeValidator()).convertToObject(code,
getLocale());
... Postcode lookup here
target.add(ContactDetailsPanel.this);
}
#Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
}
};
lookup.setDefaultFormProcessing(false);
add(lookup);

Related

Show button if input text field is not empty in Wicket

There is input text field and submit button. I would like to show button text field not empty and otherwise button should be not visible.
TextArea<String> textMessageField = new TextArea<>("textMessage", textMessageModel);
Button submitBtn = new Button("saveReminderButton") {
#Override
protected void onConfigure() {
super.onConfigure();
String text = textMessageField.getModelObject();
setVisible(text != null && !text.isEmpty());
}
};
textMessageField.add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget target) {
target.add(submitBtn);
}
});
submitBtn.setOutputMarkupId(true);
I'm trying to use setVisible method in onConfigure but it doesnt work. I tried to use instead setEnabled and it was working but I need the same functionality with hide/show button
Since you make the button invisible you need to use submitBtn.setOutputMarkupPlaceholderTag(true); instead of submitBtn.setOutputMarkupId(true);
This is important because without setOutputMarkupPlaceholderTag(true) Wicket Wicket will render nothing for this component. With setOutputMarkupPlaceholderTag(true) it will render <htmlElement id="someId"></htmlElement>. This way Wicket Ajax could find it in the DOM and replace it with the HTML of the visible one.

How do you listen for mouse paste done in a TextField using JavaFX?

Copy text into your clipboard, right click text field and press "paste", is there a way how to listen when paste was clicked? Or rather that input text in the field changed after something was pasted this way. Because these do not work in this particular case:
setOnKeyReleased()
setOnInputMethodTextChanged()
The "paste" functionality is implemented in the TextInputControl superclass of TextField in public void paste(). So, while it's not really an event-driven or MVC approach, you can react to a "paste" action (whether it's invoked by mouse or keyboard shortcut, typically ctrl-V) by overriding this method:
TextField tf = new TextField() {
#Override
public void paste() {
super.paste();
System.out.println("text pasted in");
}
}
you could just listen to text property changes.
example with a search text field:
tf_search.textProperty().addListener((observableValue, oldValue, newValue) -> {
onSearch();
});
The other approach would be to override the appropriate method using Clipboard.
TextField tf = new TextField() {
#Override
public void paste() {
Clipboard clipboard = Clipboard.getSystemClipboard();
if (clipboard.hasString()) {
replaceSelection(clipboard.getString());
}
}
};

Select All text inside CellEditor

I have a tableViewer where I can edit 1 column. Everytime the cellEditor from this column gains focus I want all the text that is displayed to be selected. In a normal SWT text control, we would just do text.selectAll(); , so I've set this listener for the the cellEditor inside the column EditingSupport class:
editor.getControl().addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
Text text = (Text) editor.getControl();
text.selectAll();
}
});
I know I can cast the cellEditor control to a Text input because I've tested it with a normal setText(); and it Works! However, the selectAll(); is not working, how can I fix this?
SOLUTION
I found the error, the code in my question above works perfectly, the reason I wasn't able to see the selectAll(); doing something is in this method:
#Override
protected Object getValue(Object element) {
String valor = MathUTILS.getBigDecimalFormatted(((ColaboradoresDespesasDocumentosLinhas) element).getValor(), PatternVARS.PATTERN_BIGDECIMAL_MILLION);
Text text = InterfaceFormatUTILS.getControlNumberFormat(editor, PatternVARS.PATTERN_BIGDECIMAL_BILLION);
return valor;
}
This is also a method from the EditingSupport class, and for some reason formatting the text control (Text text = InterfaceFormatUTILS.getControlNumberFormat(editor, PatternVARS.PATTERN_BIGDECIMAL_BILLION);) deselects all the text. Also, doing the selectAll(); in this method doesn't work..

Wicket text input disappears after setEnable on TextArea

I have a TextArea, and a associated CheckBox to disable and enable the TextArea (setEnabled(Boolean)), which is used to decide if it is a required field or not.
My problem is that Wicket does not keep the text in the TextArea when it is disabled (setEnabled(false)). It clears the input.
How do I keep the value in the TextArea before setEnabled is called on it? I need to update the Model serverside,
Can I do a AJAX request to update it? I haven't found any example.
checkBox.add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
if (textField.isEnabled()) {
textField.inputChanged();
// Update text input in PropertyModel….
}
textField.setEnabled(false); // This change in state does not include text that have been typed in
tekstFelt.setRequired(false);
target.add(textField);
}
});
You must add an AjaxFormComponentUpdatingBehavior for the blur event to the TextArea so that when you type in the textarea and leave the textarea (lose focus = blur) what you typed will be persisted by the model. Sample code:
private TextArea textarea() {
TextArea textarea = new TextArea("textarea", new PropertyModel<String>(this, "value")){
#Override
protected void onConfigure() {
setEnabled(condition());
}
};
textarea.setOutputMarkupId(true);
textarea.add(new AjaxFormComponentUpdatingBehavior("blur") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
}
});
return textarea;
}
I also recommend that wheter the textarea is enabled or not you should set in onConfigure() of the textarea. When you want to updated the textarea you just add it to the AjaxRequestTarget and the textarea knows if it should be enabled. This is a best practice and is not directly correlated to your question.

Container Visibility control on Change behavior of text field in Wicket 1.4

I've a container which output markup placeholder tag is set to true. I want to display it only if I digit in a certain text field a certain string. For example if I digit "show" in text field, container appears, if I digit "hide" it disappears. I made this code:
container.setOutputPlaceHolderTag(true);
container.setOuputMarkupId(true);
textfield.add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget target) {
form.modelChanged();
if ("SHOW".equals(textfield.getModelObject())) {
container.setVisible(true);
} else {
container.setVisible(false);
}
target.addComponent(container);
}
this code works only if I write SHOW, BUT when I write another string it doesn't disappear. To make it disappear I've to refresh the whole form they are into (and I don't want it).
How can I solve this problem??
some details: all component I'm refering to are in a form, and only if I refresh the form setVisible(false) works. From now only setVisible(true) works, it seems the container stucks on visibility true.
This code works:
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
private String someValue;
private WebMarkupContainer container;
public HomePage(final PageParameters parameters) {
super(parameters);
add(container = container());
add(textfield());
}
private WebMarkupContainer container() {
WebMarkupContainer wmc = new WebMarkupContainer("container") {
#Override
protected void onConfigure() {
super.onConfigure();
setVisible("SHOW".equals(someValue));
}
};
wmc.setOutputMarkupPlaceholderTag(true);
return wmc;
}
private TextField textfield() {
TextField tf = new TextField("textfield", new PropertyModel(HomePage.this, "someValue"));
tf.add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget art) {
//just model update
art.add(container);
}
});
return tf;
}
}
Use
container.setOutputPlaceHolderTag(true);
container.setOuputMarkupId(true);
textfield.add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget target) {
form.modelChanged();
if ("SHOW".equals(((TextField<String>) getComponent()).getModelObject())) { //change this
container.setVisible(true);
} else {
container.setVisible(false);
}
target.addComponent(container);
}
I got this tip from Getting a Wicket text box's value in an AJAX onchange event

Categories

Resources