How to disable AjaxLink button in wicket? - wicket

I'm trying to disable an AjaxLink button in wicket inside the
public void onClick(AjaxRequestTarget target) method;
I tried calling the setEnabled(false) directly but didn't work. some of the suggestions I saw online says call the isEnabled() or onConfigure() methods; but these cannot be implemented inside the onclick method; any help please?
{
buttonlabel.setDefaultModel(Model.of("Creating EWL"));
target.add(buttonlabel);
buttonlabel.setOutputMarkupPlaceholderTag(true);
boolean isWorkItemCreated = NewRecruitEWLUtil.createNewRecruitWorkItem(appInfo);
if (isWorkItemCreated)
{
buttonlabel.setDefaultModel(Model.of("EWL Created"));
target.add(buttonlabel);
buttonlabel.setOutputMarkupPlaceholderTag(true);
setEnabled(false);
target.add(this);
System.out.println("setEnabled ..." + isEnabled());
}
}

setEnabled(false) is the correct way! You also need to add the AjaxLink to the AjaxRequestTarget so it is repaint at the browser:
AjaxLink link = new AjaxLink("myLink") {
#Override
public void onClick(AjaxRequestTarget target) {
setEnabled(false);
target.add(this);
}
};
add(link);
link.setOutputMarkupId(true);

Related

GWT - enable button on KeyDownEvent in RichTextArea

I have following handler
textArea.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
//here
}
});
I need to enable save button with id "idsave", but I am not able to refer the button.
I am new to GWT, any help would be appreciated.
Typically, you do not use element ids in GWT. If you created a button, you can simply use it:
private Button saveButton;
...
saveButton = new Button("Save");
textArea.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
saveButton.setEnabled(true);
}
});
If you don't have the reference of the button then try with the id.
// get element by id
Element saveButtonElement = RootPanel.get("idsave").getElement();
// remove disabled attribute to make it enable
saveButtonElement.removeAttribute("disabled");

How to fire an event when a DialogBox got hided in GWT?

I want to call a method whenever my DialogBox is hidden. It doesn't matter how it is hidden, it could be someone click close button or it can be hidden by itself. When that happen the system will call a method.
Look at this code.
public class WishListDialogBox extends DialogBox {
#UiField Button closeButton;
public WishListDialogBox() {
setHTML("Wish List");
setWidget(uiBinder.createAndBindUi(this));
closeButton.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
hide();
}
});
}
#Override
public void hide() {
super.hide();
//call some action here;
}
}
The above code only work when I click CloseButton, but when the DialogBox was hidden by itself, nothing happened.
There is no onHide event in DialogBox.
In traditional Java, there is addWindowListener to handle his very easily, but that is missing in GWT DialogBox.
So, How to fire an event when a DialogBox is hidden in GWT?
Finally I found a solution
this.addCloseHandler(new CloseHandler(){
#Override
public void onClose(CloseEvent event) {
// TODO Auto-generated method stub
//do some action here
}
});

ClickHandler doesn't fire in jquery-mobile header & footer

after hours of searching and trying, I decided to ask here.
JqmHeader.java
public class JqmHeader extends ComplexPanel {
public JqmHeader() {
setElement(DOM.createDiv());
getElement().setAttribute("data-role", "header");
}
public void add(Widget widget) {
super.add(widget, getElement());
}
}
JqmPage.java
public class JqmPage extends ComplexPanel {
...
public JqmPage(String id) {
setElement(Document.get().createDivElement());
getElement().setAttribute("data-role", "page");
getElement().setAttribute("data-url", id);
RootPanel.get().add(page);
render(page.getId());
}
private native void render(String id) /*-{
$wnd.$("#" + id).page();
}-*/;
...
}
MyPage.java extends JqmPage.java
...
JqmHeader header = new JqmHeader();
Button b = new Button("TestButton");
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Window.alert("TestButton clicked");
}
});
header.add(b);
this.add(header);
...
My Problem
So, my Problem is, that the ClickHandler from the Button in the header bar doesn't fire. If I add the Button not to the header, but to the "RootPage", like
this.add(b)
, everything works.
I think it must lie at the jquery-mobile header implementation. Are there any workarounds /ideas?
Thanks from Berlin,
Alex
JQuery Mobile swallows the events on headers so they are not propagated to GWT. What I did in solving this for jqm4gwt (https://github.com/sksamuel/jqm4gwt) was to have a general listener on the page level and then compare event source. If it was a button in the header then I fire that event on the button manually.
Take a look at bindHeaderEvents in https://github.com/sksamuel/jqm4gwt/blob/master/src/main/java/com/sksamuel/jqm4gwt/JQMPage.java
Also, the jqm4gwt project might be a good solution for you to save you having to invent all these widgets yourself.
If you want to take a look at a pure GWT solution for building mobile apps you could take a look at http://www.m-gwt.com

GWT is making an unexpected event call

My code is below: I am seeing that on running the app the loadWidget method gets invoked even when the adminLink is not clicked. This is not want I want, but I'm not sure what is causing the issue. Please advise
public class LoginModule implements EntryPoint {
LoginPopup loginPopup;
private class LoginPopup extends PopupPanel {
public LoginPopup() {
super(true);
}
public void loadWidget(){
System.out.println("I am called 1");
CommonUi cUi = new CommonUi();
//#342 moved code to common area
FormPanel loginForm = cUi.getLoginFormUi();
setWidget(loginForm);
}
}
#Override
public void onModuleLoad() {
//#251 improved login popup ui.
final Anchor adminLink = new Anchor("User Login");
// final Label adminLink = new Label("User Login");
adminLink.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Instantiate the popup and show it.
loginPopup = new LoginPopup();
loginPopup.loadWidget();
loginPopup.showRelativeTo(adminLink);
loginPopup.show();
}
});
if(RootPanel.get("admin") !=null)
RootPanel.get("admin").add(adminLink);
}
}
Running Dev Mode, set a breakpoint in that method in your Java IDE, and take a look at the current stack, what code is calling that method. If that is the only code in your app, then this only appears to be invokable from that onClick handlers, so it is a matter of figuring out why that is being invoked.

not able to set focus on TextBox in a GWT app

This should not be causing me so much pain but it is. It is a very weird problem. In a GWT application, I have two .java files, login.java and application.java.
In login.java, I'm creating a user login page where if the username and password is verified the user is logged into the application and application.java takes from here.
Now in application. java's onModuleLoad() this is how i'm starting with a login page.
public void onModuleLoad() {
Login login = new Login();
login.textBoxUsername.setFocus(true);
RootLayoutPanel.get().add(login);}
This works great, except for the tiny problem of not being able to set focus on the username TextBox when the page loads. I have tried evrything I can think of. But the focus just doesn't get set on the TextBox. If anyone can suggest a solution, please do. Your help is greatly appreciated.
Solution: (In case it helps anyone facing the same issue)
final Login login = new Login();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
public void execute () {
login.textBoxUsername.setFocus(true);
}
});
RootLayoutPanel.get().add(login);
Try using Scheduler.scheduleDeferred():
public void onModuleLoad() {
Login login = new Login();
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand () {
public void execute () {
login.textBoxUsername.setFocus(true);
}
});
RootLayoutPanel.get().add(login);
}
Update: answer updated to use Scheduler.get().scheduleDeferred() instead of DeferredCommand, which is deprecated.
Why using DefferedCommand, I think it's better to use someWidget.getElement().focus() which is a native Javascript. I'm using it everywhere, I've not seen any problem.
If your Widget extends Composite, you can:
#Override
protected void onAttach() {
super.onAttach();
textBoxUsername.setFocus(true);
}
It would be so easy for GWT to store a 'wantsFocus' in the internal state, and call focus after the widget is attached. We are still waiting after many years for that feature however...
Still, even after the attach handler is called, setFocus does not always work.
So in the meantime, our GwtUtil library has used the following code. It is a combination of several other solutions, and has been wrapped in a utility function:
static public void setFocus(final FocusWidget focusWidget) {
if (focusWidget.isAttached()) {
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
#Override
public void execute() {
focusWidget.setFocus(true);
}
});
} else {
focusWidget.addAttachHandler(new AttachEvent.Handler() {
#Override
public void onAttachOrDetach(AttachEvent event) {
if (event.isAttached()) {
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
#Override
public void execute() {
focusWidget.setFocus(true);
}
});
}
}
});
}
}
And call it like this:
setFocus(myTextBox);
It makes sense to use a utility function; If and when GWT finally makes setFocus work, you won't have to change your source code in multiple places.