Vaadin wizard addon event fire twice - java

I am using Vaadin wizard addon and I have a problem with the following case:
When the user only presses forward/next step, there is no problem. However, if the user wants to go to the previous step, I am accidentally adding a Button click listener to the same event(That is my assumption. I have debugged the program and saw that if the user goes to the previous page, the event fires twice)
I have tried to remove the event listener before going to the next page, however, I could not find a method to remove all of the event listeners once. Also, I don't know where to remove them, since I could not find a function executed before the user is moved to the next page in Vaadin wizard.
I am following this example:
https://github.com/tehapo/WizardsForVaadin/tree/master/wizards-for-vaadin-demo/src/main/java/org/vaadin/teemu/wizards
Is there a method to remove all of the ClickListeners?
If it exists, where should I add that functionality?
Also, I am using ListDataProvider and NativeSelect components too.
NativeSelect has HasValue.ValueChangeListener<String> listener and in the default implementation, I Could not find a method such that I can use this:
NativeSelect<String> select = new NativeSelect<>("List");
select.addValueChangeListener(new HasValue.ValueChangeListener<String>() {
// some overwritten valuechange method
}
select.removeValueChangeListener(); // This does not exist
I am setting the click listener in the public Component getContent() {} method

In Vaadin 8 you need to use the Registration interface to remove Listeners.
When you add a Listener it will return the Registration:
final Registration registration = select.addValueChangeListener(this::doSomething);
And then to remove it:
registration.remove();

Related

About JButton listener with controller and view

I'm doing a Swing app in Java with a database. I got different JButton which delete, add or modified row in a JTable.
I'm stuck because I don't know where I can manage my action listener.
I have a class named DaoClef where I will execute an SQL statement, send my table model etc...
I got a view where all my button are set but I don't know what to do in my controller.
For now I got this in the view:
btnNew.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// check for selected row first
if (t.getSelectedRow() != -1) {
// remove selected row from the model
String sqlMaj = "DELETE FROM clefs WHERE IdClef = ?";
try (PreparedStatement pst = Connect.getConnection().prepareStatement(sqlMaj) )
{
pst.setInt(1, (int) t.getValueAt(t.getSelectedRow(), 0));
pst.executeUpdate();
t.addNotify();
}
catch (SQLException e)
{
}
}
}
});
But it's not good because the SQL statement need to be execute in a method in the controller.
Can you give me some clues?
From an MVC perspective, the issue here is that the Model and the View are mixed together. Since the Controller is supposed to act as an intermediary between the two, it has no role here.
The View, presenting the GUI (windows, buttons, text fields, etc.) to the user, should (ideally, more on this later) tell the Controller when a delete button has been pressed and its corresponding id. Then the Controller should pass this info to the Model which, in turn, will carry out the communication with the database using its connection and prepared statements.
Here's a good explanation on the MVC pattern:
Model-View-Controller: Does the user interact with the View or with the Controller?
Note (or maybe omit) that there's some controversy about different takes on the MVC pattern as shown in the different answers here.
There is also a good answer on the topic of MVC in Swing here:
Java and GUI - Where do ActionListeners belong according to MVC pattern?
Although the example given may be a little complex as the author tries to more strictly separate View and Controller. I'd go on and read the comments as well since they add further clarification.
I tried finding a simpler example but most didn't include action listeners. Here's a couple that do:
a) https://gist.github.com/Sammy30/7ebc606e7bb76cefac0f
b) http://www.fredosaurus.com/notes-java/GUI/structure/40mvc.html
The action listeners get added from the Controller unlike the first example linked. It's simpler but has the disadvantage of having Controller and View intertwined, which doesn't allow switching the latter easily (i.e., without modifying the Controller), which is one of the advantages of MVC.
The steps would be:
Preparation:
The View and Model get created (the Model may set its connection to the database now)
The Controller gets created with instances of the View and Model as properties
The Controller gets ready by setting action listeners in the View (or tells the View to set them, in which case the View should contact the Controller whenever an event happens)
Usage:
The user presses a button in the View
The Controller notices the button press through its action listener (or the View tells the Controller which button has been pressed)
The Controller decides what to do with the event. In this case, tell the Model that a record needs to be deleted
The Model receives the order and modifies the database, and returns a value with the updated rows count
The Model closes the ResultSet, Statement, and Connection

How to validate a textfield in pageablelistview in java wicket

i have added a pageable listview with two components,one is text field and other is a dropdown. Normal way of checking for validation is by adding a feedback panel.But when same method is used in grid its giving an error saying that feedback component with wicket:id="xy" has already been parsed.
is there any way to validate those components?
JAVA CODE HTML CODE
Somewhere in the code (e.g. your overridden Form#onSubmit() or Form#onError(), one of your Ajax behaviors, etc.) you add the component with wicket ID "FeedbackSubQuestions_" again, even though this component has already been added before. Something like the following:
public class MyPage extends WebPage {
public MyPage() {
this.add(new FeedbackPanel("FeedbackSubQuestions_"));
this.add(new Form<Void>("myForm"){
#Override
protected void onError() {
// This call the produce the error as the feedback component is already in the component hierarchy
MyPage.this.add(new FeedbackPanel("FeedbackSubQuestions_"));
}
});
...
Adding all the form components with validation and their ajax behaviors
...
}
}
In wicket, once you construct the component hierarchy, subsequent interactions with the page do not need to reinitialise any of it anymore. That is, once you add the feedback panel in order for it to display your feedback you do not need to add this panel again. If there are errors to be displayed (e.g. a form validation fails, or Component#error(String) is called manually) the feedback will pick those up automatically when the feedback panel is being rendered again (e.g. when you add it to your ajax response or the whole page is being re-rendered as part of the form submission process).

Eclipse e4 - Add a Closing Part handler

I'd like to implement an handler which is triggered every time I close a part of my Application Model.
How can I do?
For instance, every time I close such part I want to print a message thru the System Console.
Moreover is there any object (that could be injected) that provides the state of an MPart? (if it's open or close)
Thanks
You can use the addPartListener method of EPartService to add a listener for changes to the state of parts:
#Inject
EPartService partService;
...
partService.addPartListener(IPartListener instance);
Be sure to import the correct IPartListener - org.eclipse.e4.ui.workbench.modeling.IPartListener

SmartGWT: dynamically adding custom widget to VStack

I need some widgets that share the same code base, thus I introduced an abstract class providing these shared members and methods and created an implementation which adds additional functionality. but every time I wanted to add this to a VStack I got the following error:
java.lang.AssertionError: A widget that has an existing parent widget may not be added to the detach list
in order to verify if this is b/c of the class hierarchy I created the same in a method, but I'm still receiving the same error. the code below should describe what I want to achieve.
public class Test extends VStack {
// constructor that adds button and a clickhandler for the button
// that will call addComplex()
public void addComplex() {
HStack stack = new HStack();
stack.setHeight("22px");
stack.addMember(new IButton("remove"));
stack.addMember(new ListBox());
DynamicForm form = new DynamicForm();
form.setFields(new TextItem());
stack.addMember(form);
addMember(stack);
}
}
when I just call
addMember(new Label(""));
I get no error.
furthermore, considering that the stuff in the method addComplex is in a separate class, and I add a new instance in the constructor of this Test-class, no error is thrown. the error is only thrown when I want to add the HStack via a button click
why can't I add this HStack to my VStack?
Update
it was b/c of the ListBox, which is a GWT component, and no SmartGWT component. this was the detail I missed.
can anybody tell me: why has a GWT widget an existing parent, and a SmartGWT widget does not? or is the error message just bogus?
the problem was that ListBox is from GWT, not SmartGWT. replacing it with the appropriate SmartGWT widget it works like a charm.
I have found, as a general rule:
Thou shalt not mix GWT and SmartGWT if thou seeketh functionality beyond the basic displaying of thine widgets.
I ran into this same exception when attempting to hide/show a DynamicForm or a VLayout that contained some plain GWT widgets... The page loaded fine, but I ran into trouble when I tried to add some hide/show interaction functionality...

Why does my component not get focus?

I am running an applet, and am trying to focus on a component outside of the init method. I have tried using requestFocus and requestFocusInWindow. Both of these return true, as do isDisplayable, isVisible, and isFocusable, but calling hasFocus right after this returns false. The component just doesn't show up in the applet.
Here is the method called by the event listener (when the user clicks 'begin battle'), trying to focus on the component:
private void beginBattle(){
BattleSequence bs = new BattleSequence(..);
remove(pnlInit);
remove(pnlOOB);
remove(pnlBattle);
add(bs);
bs.setVisible(true);
bs.requestFocusInWindow(); //this returns 'true'
bs.setBounds(0, 0, bs.getSize().width, bs.getSize().height);
bs.init();
}
public class BattleSequence extends RavelSequence
public abstract class RavelSequence extends Applet implements KeyListener, FocusListener
Why is the focus request denied? If there is more code that is relevant, I can post it!
***EDIT:
Adding the mouse listener worked to get focus to the component, but I don't want to have the user need a mouse to control the game. I changed the declaration of RavelSequence to extend Container instead of Applet, and everything works peachy.
bs.hasFocus still returns false, but the key events are handled appropriately.
Try to call it on event dispatch thread.
Try adding a MouseListener to the component in question, and request focus in the mouseClicked() method.

Categories