I would like to set a CheckBox ReadOnly when my new window pop up.
Here is my code.
getForm().getField("foreign").addValueChangeListener(
new Property.ValueChangeListener(){
#Override
public void valueChange(Property.ValueChangeEvent event) {
changeValueForCurrency(event.getProperty().getValue());
}
}
);
Tried to let it happen by calling the changeValueForCurrency()
changeValueForCurrency(getForm().getField("foreign"));
Does not work.
My mate told me something about to fire it, but It doesn't has any methods like firing it.
Also, I tried to set this method below true;
setImmediate(true);
Still does not work
Did you try to do setImmediate() after adding the listener? i had this issue with a table.
Related
I am using Vaadin MessageBox, the problem that I have which is logic is that the code after MessageBox runs immediately and doesn't wait for your MessageBox answer. I tried to pack my code in a function which returns a boolean. But it seems impossible to do it with MessageBox(), here is a sample of what I want to do:
private boolean Delete(){
return true;
}
private boolean IsDone(){
MessageBox
.createQuestion()
.withCaption("Delete")
.withMessage("Are you sure?")
.withYesButton(()-> return Delete()) //here is where i have problem
.withNoButton(()-> {})
.open();
}
I tried also,
.withYesButton(()-> {return Delete();})
it seems to work like this but without return true;
.withYesButton(()-> {if(Delete){
return true; //doesn't work!
System.out.printLn("Works!"); // works :-?
}})
anyone know this situation or has any idea?
Actions that require user interaction are not "stopping" code execution. You need to organize your code so that it continues in handlers for yes and no buttons. So make sure that your "flow" stops to showing confirmation box and continues after yes or no has been answered.
private void IsDone(){
MessageBox
.createQuestion()
.withCaption("Delete")
.withMessage("Are you sure?")
.withYesButton(()-> onUserAnsweredYes())
.withNoButton(()-> onUserAnsweredNo())
.open();
}
The withYesButton method gets a Runnable that is gonna be executed when the user press the yes button. Let's see for a moment the implementation of Runnable:
public interface Runnable {
void run();
}
As you see, run returns void, so you cannot return any value, like true. Instead, you must provide the code you use to delete anything you want, like:
MessageBox
.createQuestion()
.withCaption("Delete")
.withMessage("Are you sure?")
.withYesButton(()-> codeToDeleteSomething())
.open();
I am using this code to create a JCheckBox
private final JCheckBox cbDisplayMessage = new JCheckBox("Display");
cbDisplayMessage.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if(e.getItemSelectable() == cbDisplayMessage) {
if(cbDisplayMessage.isSelected()) {
cbDisplayMessage.setSelected(false);
} else {
cbDisplayMessage.setSelected(true);
}
}
}
});
When I run this it causes an StackOverflow error on setSelected(true). Can't figure out what I am doing wrong. Any ideas appreciated....
You can try with ActionListener instead of ItemListener as shown below without causing StackOverflow error.
cbDisplayMessage.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (cbDisplayMessage.isSelected()) {
cbDisplayMessage.setSelected(false);
} else {
cbDisplayMessage.setSelected(true);
}
}
});
There is no need to check the source of the event again because you are sure that you have added this listener on the same object. This is required only if same listener is added for more components.
-- EDIT--
Now Your requirement is clear to me. If you want to toggle the state of the check box then there is no need to do it using listener because that's the default behavior of the check box.
Your listener is called every time the state changes, but you trigger a new state change from within that listener, so each state change results in that listener being called over and over again until your stack is full. Your setup has to be a bit more complicated to do something like that - if you want to change the state of the component you're listening to, you'll want to remove its listener(s), fire your programmatic state change, then re-add them.
my question is. Is possible to add a component like a button (button has a functionality that triggered when it is clicked) inside a list component?
This image explain better what I refer:
http://2.bp.blogspot.com/-HThpKcgDyRA/URI_FdpffMI/AAAAAAAAAUI/SficZAPXaCw/s1600/1.png
Yes but it requires some handcoding and it will only work for touch (since you won't be able to assign focus to it).
We normally recommend just using Component/Container hierarchies for these cases rather than dealing with lists but obviously this isn't always practical.
The key is to always use the list action listener to trigger events, nothing else. So when you are in the action handling code of the list you would want to know if it was triggered by your button...
If you are in the GUI builder this is pretty easy:
Button b = ((GenericListCellRenderer)list.getRenderer()).extractLastClickedComponent();
if(b != null && b == myButton) {
// your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}
The handcoded approach is pretty similar with one major caveat, you don't have the extractLastClickedComponent method. So assuming you have a component within the renderer just add an action listener to it. Within the action listener just set a flag e.g.:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
buttonWasClicked = true;
}
});
// within the list listener we do the exact same thing:
if(buttonWasClicked) {
// for next time...
buttonWasClicked = false;
// your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}
Yes!!! Try it, this is pretty easy:....
http://www.codenameone.com/how-do-i---create-a-list-of-items-the-easy-way.html
http://www.codenameone.com/how-do-i---create-a-list-of-items-the-hard-way-gui-builder-renderer.html
There are two listeners:
table.addListener(new ItemClickListener() {
public void itemClick(ItemClickEvent event) {
// fireEvent(...);
}
});
table.addListener(new Table.ValueChangeListener() {
public void valueChange(final ValueChangeEvent event) {
// do something
}
});
I want to fire an event from the first listener to call the second.
In one listener, I want to update the data source for the table by clicking on the row; in the second listener, I want to restore the cursor on the selected row.
How can I do it?
If you change a column value of a row, then second listener is fired. To do so, you can change the column then you can regain its value in the second listener.
You shouldn't be trying to manually trigger events. If you have code in the second listener that needs to be called, you should abstract it out into a third method.
It's difficult to understand exactly what you want, however. I would suggest adding more details to your question.
I'm trying to run some RPC calls when the user closes the window, refreshes it or clicks the back button but just for one single page. I found a post talking about handling but the solution is not working well, missing back button handler (not working) and always is for all page on the web, I can't find something for remove handler if you leave from page
Window.addWindowClosingHandler(new Window.ClosingHandler() {
#Override
public void onWindowClosing(ClosingEvent event) {
event.setMessage("You sure?");
}
});
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
// Execute code when window closes!
System.out.println("ble ! ");
}
});
Framework: GWT 2.4 with mvp4g.
Browsers: FF and Chrome.
Because i use mvp4g framework i found a solution there , you need to extends your presenter with CyclePresenter and override onLoad and onUnload methods. These methods fire when view is load/unload from DOM, i tested and work for all cases, f5, back button, close browser/tab, go other web and call others events. Now i cant put some code there.
You need to remove the handler when you leave the page and then re-add it when you enter the page again. You have the "add" side covered with the above code, you are missing the "remove" part. You do that by using the HandlerRegistration object that is returned from the add*Handler methods. When you want to remove the registered handler, you just call the HandlerRegistration.removeHandler() method:
HandlerRegistration windowClosingHandler = Window.addWindowClosingHandler(new ClosingHandler() {
#Override
public void onWindowClosing(ClosingEvent event) {
// Handle window closing
}
});
// From now on the CloseHandler will be fired
// ...
// Somewhere else:
windowClosingHandler.removeHandler();
// From now on the CloseHandler won't be fired