GWT:adding clickHandler on HyperLink - java

I am having this HyperLInk in my cellTable ,but its Clickhandler is not working
myHyperLInk.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
Window.alert("test");
}});
It displays a warning :
The method addClickHandler(ClickHandler) from the type Hyperlink is
deprecated
When i click on the link, it simple go to the previous page from history. How can i simply say show a message dialog on pressing a hyperLink?

I think what you want may be an anchor widget vs a hyperlink.
http://google-web-toolkit.googlecode.com/svn/javadoc/2.3/com/google/gwt/user/client/ui/Anchor.html
Hyperlinks are usually used for working with the history.

Hyperlink link0 = new Hyperlink("link to foo","foo");
link0.addDomHandler(handler, ClickEvent.getType());
ClickHandler handler = new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("test");
}
};

Related

Problems when creating containers in CodenameOne

For my main App I would need a custom container for navigation purpose, see here (Stackoverflow).
As the solution posted did not work for me (I tried with simple System.out.println), I began a new Project to understand, how the container embedding works, but it does not work the way I expected.
So the new App is the Hi World application with the orange color.
I created a new blank container in the GUI designer and added 3 Buttons.
I created a new blank container in the GUI designer and added 3 Buttons.
I added an actionListener to them within the container
I added the container to the form
My StateMachine looked like this:
#Override
protected void onButtonCont_ButtonAction(Component c, ActionEvent event) {
System.out.println("button1 clicked");
}
#Override
protected void onButtonCont_Button1Action(Component c, ActionEvent event) {
System.out.println("button2 clicked");
}
#Override
protected void onButtonCont_Button2Action(Component c, ActionEvent event) {
System.out.println("button3 clicked");
}
(The container I created was named ButtonCont..) Nothing else modified in StateMachine or elsewhere!
Now I started the app and clicked the buttons - but nothing happened.
So I
Opened the GUI Builder
Selected MainForm
selected the three Buttons one after another
added ActionListeners to each one
Now my StateMachine looks like this:
#Override
protected void onMain_Button2Action(Component c, ActionEvent event) {
System.out.println("button3 -now- clicked");
}
#Override
protected void onMain_Button1Action(Component c, ActionEvent event) {
System.out.println("button2 -now- clicked");
}
#Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
System.out.println("button1 -now- clicked");
}
(in addition to the previous onButtonCont- methods)
Starting the app and clicking the buttons results in this output:
button1 -now- clicked
button3 -now- clicked
button2 -now- clicked
What am I doing wrong?
I found the answer myself.
Instead of adding the container to the Form under the section "user defined", you simply need to add an embedded container and select "embedded | [null]" to your own container

Click event not firing on label in GWT

I have created a click handler on a gwt label, but it fails to fire. Whats wrong? Same method works for other widgets like icon etc.
#UiField Label fileName;
---
---
public void addClickHandler() {
fileName.sinkEvents(Event.ONCLICK);
handler = this.addHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
event.preventDefault();
event.stopPropagation();
Window.alert("UI clicked");
}
}, ClickEvent.getType());
}
As I pointed out in comment. Reason why it is not working is because you are adding native event handler to this, and as i but you need to sink DOM events on element to be able to handle them. As you did not do this for this element but for Label it won't work.
As You want to handle click element on Label, you need to add your handler to fileName and that will work

java gwt clickhandler

I have a MenuButton which consists of a Image and a Text under the image. The whole thing is build like this:
ImageResource icon = ...;
final Element span = DOM.createSpan();
Image image = new Image(icon);
span.insertFirst(image.getElement());
Element div = DOM.createDiv();
div.setInnerHTML(text);
span.insertAfter(div, span);
image.sinkEvents(Event.ONCLICK);
getElement().insertFirst(span);
The click event is set in the presenter like this:
...
private void bindEvents() {
display.getButton().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("test");
}
});
The click event works just partly, when I click on the text under the image the click event works fine but when I click on the Image no click is performed!?
A different approach:
FlowPanel myButton = new FlowPanel();
myButton.add(new Image(icon));
myButton.add(new Label(myButtonText));
ClickHandler h = new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// do something
}
};
myButton.addDomHandler(h, ClickEvent.getType());
This will catch a click event on both image and the text under it.

GWT addSubmitCompleteHandler called twice

I have a form panel (myForm) with a submit button added onto a simple panel. Every time the submit is pressed myForm.addSubmitCompleteHandler is called twice
mySubmit.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
myForm.submit();
}});
...
myForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
#Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// what ever's here happens twice
}
});
I've double checked my code and ordering and placing of widgets and panels. What could possibly be causing this?
What I am trying to achieve is an alert that submission is complete.
It seems like adding the line
event.preventDefault()
in the addClickHandler of the submit button helps. Also make sure you don't have duplicate setAction Calls for the form

Is there any way to create something similar to Javascript's alert in GWT?

Hi is there any way to create something similar to Window.alert() in GWT?
Basically I wanted to customize the Window.alert()'s "Ok" button to say something else but
as I researched there is no way to customize the alert boxes.
Thanks.
Window.alert() is already available in GWT. It opens a native dialog box which contais OK button localized by the browser's locale. This alert box can not be changed.
Use PopupPanel or DecoratedPopupPanel.
You could use the PopupPanel.
I usually code a generic dialog box, that is created once and when I need it again the html content and title is replaced. You can also add a OK/Cancel button combination, this all is rather straightforward.
private DialogBox dialog = null;
private HTML dialogHtml = new HTML();
public void onDialog(final String title, final String html) {
if (dialog == null) {
dialog = new DialogBox();
dialog.getElement().getStyle().setZIndex(99);
dialog.setWidth("500px");
dialog.setGlassEnabled(true);
dialog.setAnimationEnabled(true);
dialog.setModal(true);
VerticalPanel vp = new VerticalPanel();
vp.add(dialogHtml);
HorizontalPanel hp = new HorizontalPanel();
hp.setWidth("100%");
Button close = new Button("close");
close.setWidth("200px");
close.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
dialog.hide();
}
});
hp.add(close);
hp.setCellHorizontalAlignment(close, HasHorizontalAlignment.ALIGN_CENTER);
hp.getElement().getStyle().setMarginTop(40, Unit.PX);
vp.add(hp);
vp.setSpacing(10);
dialog.add(vp);
}
dialogHtml.setHTML(html);
dialog.setHTML(title); // the actual title
dialog.show();
dialog.center();
}
The HTML content is something very simple, i.e.
<div style="width: 500px; overflow: auto;">...</div>

Categories

Resources