GWT. How to handle two similar actions with one event? - java

I've encountered next problem. I've got list of products and each product has button to open its QuickLook PopUp. I've created special event for this button, registered it in EventBus and trigger them. Everything seems good, but when I click one button, popup is shown not just for this item, but for all items, that are in list (I mean numbers of that "show"). This happens cause I have one Event-class for all these buttons, but can I somehow separate them one from another?
I just want to set some ID or something like that to every button and check this condition while triggering or (this would be even better) trigger only event, that I really need.

You don't need an EventBus for this. You can create a simple ClickHandler and attach it to your buttons. When you create a ClickHandler, you pass a product ID (or whatever you use to differentiate your products) to a method that shows your popup.

You can add productId to your Custom Event as a property. Event handler will check it and show only required product info.

Related

How can I create a popup menu that allows user to choose a certain elements from a list on Java?

I am writing a Java program to record some events and their status, i.e., done or not. I have implemented the recording and calculations, but I also want to have a button that will open another window that will list the events with a checkbox corresponding to each of them, so I can pick which ones' statuses I will change to done. I have only used Swing libraries so far.
I want this window to contain one event per line, with a checkbox next to the event. Each checkbox should determine whether the status of the even should be changed or not. I had trouble even making such a window open and list the items, let alone put the checkbox.
Note: The design is not too important. This won't be a published project, I will mostly use it for personal work. I can do with buttons instead of checkboxes, where any click changes the status of the event next to it.
Thank you in advance for your valuable responses.

vaadin UI call from callback

i am trying to build a 2-step process using vaadin. I created a view that holds my 2 main components (extends Div) which are displayed alternately.
to change from one of the main components to the other i have a ComboBox that i use for searching and therefore implemented a CallbackDataProvider, like seen below.
ComboBox<ResponsePartSearchResult> cmbPartSearch = new ComboBox<>();
CallbackDataProvider<ResponsePartSearchResult, String> provider = new CallbackDataProvider<ResponsePartSearchResult, String>(
query -> {
// fetch callback
}, query -> {
// retrieve count
});
The user is supposed to search for parts, get a list, and should be able to click on one of the results. this result is then displayed in the ComboBox. In another text field he has to add more info and click on a Button, in order to proceed. The Button's ClickListener retrieves the value from the ComboBox and the second TextField and sends it to a private method:
ResponsePartSearchResult value = cmbPartSearch.getValue();
value.q = txtPartSerial.getValue();
proceedToStepTwo(value)
BUT! If the user pastes a valid ID, the result coming back from the backend contains a certain flag, showing that this part was retrieved by its ID and with all required additional info. In this case the user is supposed to be directly "redirected" to the second main component, whithout the need to click the Button.
In this case I call the method proceedToStepTwo within the "fetch callback".
For the first case everything is running smoothly so far. For the second case, however, the 2nd main component and all it's UI elements are shown, but in order to click on something, whether it is a button or a checkbox, i need a double click.
It's a very strange behaviour without any error message. When I click on a Button (I added a sysout to a ClickListener) on that 2nd main component my RequestLoggingFilter shows a few of the following lines:
After request [uri=/?v-r=uidl&v-uiId=0;client=0:0:0:0:0:0:0:1;session= ...]
but not the content of the sysout. It only shows the content (and a few of the Logging lines above) when i doubleclick.
This shows that the Button reacts, but not entirely.
When I just let the fetch callback fill the ComboBox and TextFields and click the Button manually, it works again. But I want to get rid of the separate manual step.
I assume when I'm inside of that callback method, I need to call proceedToStepTwo differently, but how? Any ideas or experiences? Any help would be appreciated.
Thanks!
EDIT
When i debug and set a breakpoint into the method proceedToStepTwo() the UI will behave expectedly. However when i remove the breakpoint again and put a Thread.sleep(1000) in there, nothing changes and i cannot properly click the UI elements.

How to automatically fulfill task on activity start?

I have a code that requires two edittexts, this information is used to display direction on a map. How can I submit this data automatically on activity start, without the need of a button.
Side question, is there a way to hide these edittexts? To prevent further manipulation by users.
EditTexts can be either disabled using editText.setEnabled(false); or hidden using editText.setVisibility(View.INVISIBLE);.
If you want to execute code on an activity start just write it into the onCreate(...) method.

Instanceof, Enum Or Multiple Listeners for JButtons

On my swing GUI I have lines of data and a number of buttons, the user selects a number of items and then then selects a button.
Each button applies a different rule to the data and so different functions need to be called for every button, I'm using an MVC design pattern and my question is such, How should I handle the different needs of every button?
Create a class 'MyButton' which extends JButton then give this some sort of Enum, I can then create 1 action listener and then check which button has been pressed in the ActionListener by inspecting the Enum.
Similar to above but with a different class for each button then using instanceof to determine which has been pressed.
Implement a separate ActionListener for each button
Other?
Which is the best method to use if any? Any advice would be greatly received!
Implement a separate listener for each button.
First because it's the usual solution. Second, between there's no reason to extend JButton just to do something else when it's clicked. That's the role of the ActionListener. Swing components are designed to be used as is, and you should generally not extend them.
It's MVC: you separate the logic (in Actions) and the view (the button).
There is no need to use an enum or to subclass JButton. What you can do to keep things clean when you have dozens of buttons, is a factory class to create Action instances.
If I get your question correctly, you mean to say, you have a data in line items and every line items have a button, which when pressed invokes a rule pertaining to the line item.
If so, then
If you take the 2nd approach, you need to code inside your action listener every time a new line item added in future.
Third approach will also have same implication as above
First approach sounds quite good. You can have a Factory which may have a hashmap keyed with the enum variables and the respective rule. Inside the action listener get the rule from the factory and invoke it.
This way you get a proper separation of concerns and your action listener will act as a controller, having no knowledge of rules and data items.

How to avoid infinite update loops in Swing?

I have a JPanel with a set of items (for example combo boxes and text fields). Some action listeners are implemented on those items to register user updates.
If the user selects a value in a JComboBox (for example), the action listener captures the event. The corresponding underlying bean method is called and the panel is refreshed. Changing can have an impact on other fields displayed in the pane.
The problem is that when the panel is refreshed, all listeners are triggered, and they call for a refresh themselves. This leads to an infinite loop.
How can I avoid this? I can't get rid of the listeners, because I need to capture user updates, but I don't want these to fire when I am only refreshing the panel content.
One option is to have a central boolean value or some indicator that each listener can check to prevent the chaining of events.
Another option is to not refresh the field if the value does not change. That way each component is updated at most once per refresh.
I can't get rid of the listeners, because I need to capture user updates, but I don't want these to fire when I am only refreshing the pane content
Then remove the listeners, refresh the pane content and then restore the listeners. This way the listeners only fire when a user change is made.
I think that if your problem is in combobox it just points to a bug. Really, if user changes the value of the combobox, that somehow triggers refresh of the pane the value of the combo box should not be changed second time! So if it is onValueChanged() (or something like this) it should not be called at all when pane is being refreshed.
But if for some reason it happens you can verify whether the old and new values are the same and exit the listener.
If this still does not help I'd suggest you some non-standard solution: try to investigate the stack trace into the listener. Can you identify whether the listener was called as a direct reaction to user's action or after the pane refresh? In this case you can create utility method and put it in the beginning of all relevant listeners.
My applications also suffered from this problem, and solution with the flag, that I should check in every listener and enable/disable in code, feels not very good for me. I always forgot to set this flag to true/false in necessary places.
That is why I decide to implement another solution.
I just subclass all default swing components that I am using often, and implemented custom ValueChanged event that I fire after mouse/keyboard/clipboard/etc events. Now I am always know, that if ValueChanged event is fired, it means, that value was issued by user, not by code. Event handling in this way much more cleaner. This solution solves my problem.

Categories

Resources