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
Related
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
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
Having a
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
//lot of method calling here then
getWindow().removeWindow(popup);
log.warn("Removed Popup");
}
I would expect to show a popup window and after some milisecundom (after the expensive method calls) it should hide itself. The log says :
2014-02-19 15:26:51 WARN xyzClass:82 - Added POPUP
2014-02-19 15:26:51 WARN xyzClass:135 - Removed Popup
But the truth is that there is no popup showing here.
If i only show it, and not remove it later (the popup will show)
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
//lot of method calling here then
log.warn("Removed Popup");
}
My main reason for this i want to achieve a glasspanel/loading screen functionality # Vaadin, and not had found better solution yet. Any solution/description why the popup not shown up i would appreciate
Just do not have time to render it. You add it and immediately remove.
Try this approach, for example:
private MyPopup popup;
public void buttonClick(ClickEvent event) {
Thread workThread = new Thread() {
#Override
public void run() {
// some initialization here
getWindow().removeWindow(popup);
}
};
workThread.start();
popup = new MyPopup();
getWindow().addWindow(popup);
}
Depending on Vaadin version you can make use of ICEPush plugin (Vaadin 6) or built-in feature called Server Push (Vaadin 7).
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
// start background thread with ICEPush or ServerPush
}
// Background thread in a separate class
// update UI accordingly when thread finished the job
getWindow().removeWindow(popup);
log.warn("Removed Popup");
Thanks to it you can move your time-consuming operations to another class thus decouple your business logic from the presentation layer. You can find examples of usage in the links above.
Simplified code
play = new PushButton("Play");
play.getUpFace().setImage(new Image(pathToImages+offImage));
play.getUpHoveringFace().setImage(new Image(pathToImages+hoverImage));
play.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//This event is captured
}
});
I require a keyboard button as a shortcut to play button accordingly I have added
Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event)
{ //Call the functionality of play button for the approriate key press
}
});
But once I click the play button using mouse the above nativePreviewHandler does not capture the key press event from keyboard.
So I added a keyDownHandler to the play button
play.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
//This event is never fired
}
});
But the above onKeyDown() method is never called.
Only after I click at some other place on screen is the key press event captured by the nativePreviewHandler.
Just as additional info, I tried following without success
Added DomHandler to the play button instead of keyDownHandler
Removed the images set to the play button
Tried to unfocus the play button every time it is pressed using play.setFocus(false);
Any possible solutions or suggestions are appreciated.
The play button need to have the focus in order to be able to catch the keyDownEvent.
Could you check this ?
If you can't set the focus on that button: check this answer. You need to set a keyDownHandler to a container and ensure it is focused at all time in order to catch the events even if your button is not focused.
This will work:
Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getNativeEvent().getType().equals("keydown")) {
Window.alert("Code: " + Integer.toString(event.getNativeEvent().getKeyCode()));
}
}
});
I have a JButton titled "select"
In the class that creates that JButton and other classes, I want to use an if condition with ActionPerformed method.
Something like(pseudo-code)
if(_selectListener.actionPerformed(ActionEvent)) { //i.e., if select Button is clicked,
//do something
}
Is this possible?
I want to call this method because I have to handle a situation in which a player should be able to choose something by clicking "select" button, or another "scroll" button, and I want to control it using something similar to a bunch of if statements like the one above.
If it is possible, what is the syntax for it? What is the argument ActionEvent?
Thank you!
The easiest and cleanest way is to add a dedicated, specific action listener to each button. That way, when the actionPerformed() method is called, you're sure that the associated button has been clicked, and don't need to test which button has been clicked:
selectButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// handle click on select button
}
});
scrollButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// handle click on scroll button
}
});
Another way is to use a common ActionListener, and use the getSource() method of ActionEvent to know which component triggered the event. Compare the result with each potential button to determine which is the one that has been clicked:
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == selectButton) {
// handle click on select button
}
else if (e.getSource() == scrollButton) {
// handle click on scroll button
}
}
What is the argument ActionEvent?
The answer is in the documentation. Read it.
no you cant call, if needs boolean expression/value, but this method returns void.