Why is a MenuItem not responding? - event-handling

There is a ContextMenu which has two options and when the second option (item2 in the code) is pressed with the right mousebutton I want it to print out some text so I know I did actually activate it. Up till now nothing happens when I click on the second mousebutton.
I haven't had much experience yet with Eventhandlers so my apologies if I made a noobish mistake.
private void maakContextMenu() {
menu = new ContextMenu();
MenuItem item = new MenuItem("Kleur Assen");
MenuItem item2 = new MenuItem("tweede optie");
final LissajousCanvas canvas = this;
item.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
new KiesKleur(canvas).show();
}
});
item2.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent t) {
System.out.println("in the loop");
if(t.getSource()==MouseButton.SECONDARY){
System.out.println("in too deep");
}
new KiesKleur(canvas).show();
}
});
menu.getItems().addAll(item, item2);
}

A MenuItem is not actually a Node, so it's not part of the scene graph in the way that Nodes are. So I'm not really sure if this is a bug or not; I think it probably only implements EventTarget so it can specifically generate ActionEvents. You'll have noticed there is no setOnMouseClicked(...) method available.
Here's a workaround. I'm not sure why it only works with MOUSE_PRESSED and not with MOUSE_CLICKED, but it's likely something to do with the default mouse event handling that generates the action events:
private void maakContextMenu() {
menu = new ContextMenu();
MenuItem item = new MenuItem("", new Label("Kleur Assen"));
Label menuItem2Label = new Label("tweede optie");
MenuItem item2 = new MenuItem("", menuItem2Label);
final LissajousCanvas canvas = this;
item.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
new KiesKleur(canvas).show();
}
});
menuItem2Label.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent t) {
System.out.println("in the loop");
if(t.getButton()==MouseButton.SECONDARY){
System.out.println("in too deep");
}
new KiesKleur(canvas).show();
}
});
menu.getItems().addAll(item, item2);
}

Related

Why do I get MouseDown events from a position outside of the control's boundary?

I have a Composite containing a number of Text controls. I have attached MouseListeners to each control.
What surprises me is that sometimes, when I click on a control, I get a MouseDown event from its neighbour. The Event position is outside of the control's boundary and I get no event from the other control which I thought I had clicked on.
What can cause this to happen?
SNIPPET
Run. Press Esc to close the MessageBox. Click in field BBBB. Press Esc to close MessageBox. Click in field AAAA. The event is generated from field BBBB.
public class Test
{
public class MyListener implements MouseListener, FocusListener
{
private boolean active;
#Override
public void focusGained(FocusEvent e)
{
System.out.println(e);
message((Text) e.widget, "FocusGained");
}
#Override
public void focusLost(FocusEvent e)
{
System.out.println(e);
}
#Override
public void mouseDoubleClick(MouseEvent e)
{
}
#Override
public void mouseDown(MouseEvent e)
{
System.out.println(e);
}
private void message(final Text t, final String m)
{
if (active == false)
{
active = true;
MessageBox mb = new MessageBox(t.getShell());
mb.setText(m);
mb.setMessage(t.getMessage() + "\n\n" + m);
mb.open();
active = false;
}
}
#Override
public void mouseUp(MouseEvent e)
{
System.out.println(e);
message((Text) e.widget, e.toString());
}
}
private MyListener listener = null;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
new Test(shell);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public Test(Composite parent)
{
listener = new MyListener();
create(parent);
}
private void create(Composite parent)
{
parent.setLayout(new GridLayout(2, true));
createText(parent, "AAAA");
createText(parent, "BBBB");
parent.layout(true);
}
private Text createText(Composite parent, String message)
{
Text t = new Text(parent, SWT.NONE);
t.setMessage(message);
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
t.setLayoutData(gd);
t.addFocusListener(listener);
t.addMouseListener(listener);
return t;
}
}
This is indeed strange. When traversing from B to A, the MouseUp event is marked as sent from field B.
If you replace the MessageBox with something non-interrupting i.e. System.out, the mouse event senders are the right ones.
To me, this seems more of a theoretical corner case. Decent applications would not interrupt the users field traversal with a modal window. However, if this is relevant for your, I'd report a bug to SWT.
I managed to get this working by de-coupling the pop-up window from the events. The message method now looks like this:
private void message(final Text t, final String m)
{
//NB active is declared as 'volatile'
if (active == false)
{
active = true;
t.getDisplay().asyncExec(new Runnable()
{
#Override
public void run()
{
MessageBox mb = new MessageBox(t.getShell());
mb.setText(m);
mb.setMessage(t.getMessage() + "\n\n" + m);
mb.open();
active = false;
}
});
}
else
{
System.out.println("Already active: " + t.getMessage());
}
}
This seems to give the events the opportunity to continue uninterrupted by the pop-up window. The pop-up will be activated a short while later. So far it works ok.

CellTable click swallowed

I've an combo box which is composed of a text field and a popup with a CellTable showing the suggestion items. The text field has a change handler that updates the CellTable's selection.
When typing a character and clicking an already selected suggestion, the first click is swallowed. The second click works and triggers the selection via the CellTable.addDomHandler(...).
Any idea why first click is swallowed?
Example code:
private static class SuggestFieldTextAndPopupSandbox extends SimplePanel {
private final TextField mText;
private CellTable<Handle<String>> mTable;
private SingleSelectionModel<Handle<String>> mTableSelection;
private SingleSelectionModel<Handle<String>> mSelection;
private ProvidesKey<Handle<String>> mKeyProvider = new SimpleKeyProvider<Handle<String>>();
private PopupPanel mPopup;
private List<Handle<String>> mData;
public SuggestFieldTextAndPopupSandbox() {
mData = Lists.newArrayList(new Handle<String>("AAA"), new Handle<String>("AAB"), new Handle<String>("ABB"));
mSelection = new SingleSelectionModel<Handle<String>>();
mText = new TextField();
mText.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent pEvent) {
mPopup.showRelativeTo(mText);
}
});
mText.addBlurHandler(new BlurHandler() {
#Override
public void onBlur(BlurEvent pEvent) {
mTableSelection.setSelected(startsWith(mText.getValue()), true);
}
});
mText.addChangeHandler(new ChangeHandler() {
#Override
public void onChange(ChangeEvent pEvent) {
mText.setText(mText.getText().toUpperCase());
}
});
mTable = new CellTable<Handle<String>>(0, GWT.<TableResources>create(TableResources.class));
mTable.setTableLayoutFixed(false);
mTableSelection = new SingleSelectionModel<Handle<String>>(mKeyProvider);
mTable.setSelectionModel(mTableSelection);
mTable.addDomHandler(new ClickHandler() {
#Override
public void onClick(final ClickEvent pEvent) {
Scheduler.get().scheduleFinally(new ScheduledCommand() {
#Override
public void execute() {
mSelection.setSelected(mTableSelection.getSelectedObject(), true);
mText.setFocus(true);
mPopup.hide();
}
});
}
}, ClickEvent.getType());
mTable.addColumn(new TextColumn<Handle<String>>() {
#Override
public String getValue(Handle<String> pObject) {
return pObject.get();
}
});
mTable.setRowData(mData);
mPopup = new PopupPanel();
mPopup.setAutoHideEnabled(true);
mPopup.setWidget(mTable);
mPopup.setWidth("200px");
mPopup.setHeight("200px");
VerticalPanel p = new VerticalPanel();
p.add(mText);
setWidget(p);
}
private Handle<String> startsWith(final String pValue) {
final String val = nullToEmpty(pValue).toLowerCase();
int i = 0;
for (Handle<String> item : mData) {
String value = item.get();
if (value != null && value.toLowerCase().startsWith(val)) {
return item;
}
i++;
}
return null;
}
}
I reproduced your issue and here is the problem:
when you click on the suggestions the following is happening:
The text field is loosing focus which causes the corresponding ChangeEvent to be dealt with followed by the BlurEvent.
The click causes the popup to get the focus now which is why it is swallowed.
If you remove the ChangeHandler and the BlurHandler of the text field the issue disappears. But I think I found another solution
Try replacing the DOM handler of the mTable with a selection handler relative to the mTableSelection as follows:
mTableSelection.addSelectionChangeHandler(new Handler(){
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Scheduler.get().scheduleFinally(new ScheduledCommand() {
#Override
public void execute() {
mSelection.setSelected(mTableSelection.getSelectedObject(), true);
mText.setFocus(true);
mPopup.hide();
}
});
}
});
Found a way how to properly solve this.
Skipping the blur handler when user hovers the suggestion list area seemed to fix that issue, at least from the tests that were done didn't see any more issues.
This was necessary because just before the user clicks a suggestion item, the text is blurred and it fires a selection change. This in turn cancels the selection made when user clicks an item.

Clear SuggestBox on blur in GWT

I have a SuggestionBox in GWT. Is there a way to clear it when it blurs (unless the user made a selection, in which case an action should happen)?
Add a BlurHandler:
suggestionBox.getValueBox().addBlurHandler(new BlurHandler() {
#Override
public void onBlur(BlurEvent event) {
// your code goes here
}
});
Try this one using ValueChangeHandler:
Note: ValueChange event has same behavior as Blue event but it is fired only if value is changed in SuggestBox.
class MyMultiWordSuggestOracle extends MultiWordSuggestOracle {
private Set<String> values = new HashSet<String>();
#Override
public void add(String value) {
super.add(value);
values.add(value);
}
#Override
public void clear(){
super.clear();
values.clear();
}
public boolean contains(String value) {
return values.contains(value);
}
}
You code:
final MyMultiWordSuggestOracle oracle = new MyMultiWordSuggestOracle();
oracle.add("A");
oracle.add("AB");
oracle.add("BCD");
oracle.add("BCDE");
final SuggestBox suggestionBox = new SuggestBox(oracle);
suggestionBox.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if (!oracle.contains(event.getValue())) {
suggestionBox.setValue("");
}
}
});

style for GXT menu button

I see GXT Menu can only be set on CellButtonBase subclasses' instances through setMenu() method.
I'd like to show an image instead of a button and show a menu when user clicks that image. unfortunately, Image is not a subclass of CellButtonBase and thus I can't attach a GXT Menu to it.
so how can I make TextButton (which seems to be my only choice here) look like an image if I have to use it?
There's no documentation or examples on this subject. I asked on Sencha GXT forum support, but got no response.
ok, I found a way to do this without TextButton. add an Image and call menu.show(...) in click handler.
private void createMenu() {
menu = new Menu();
Image menuButtonImage = new Image(Resources.INSTANCE.nav_preferences());
menuButtonImage.addStyleName(CSS.header_bar_icon());
menuButtonImage.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
menu.showAt(getAbsoluteLeft(), getAbsoluteTop() + MENU_OFFSET_FROM_IMAGE_TOP);
}
});
menu.addShowHandler(new ShowEvent.ShowHandler() {
#Override
public void onShow(ShowEvent event) {
highlight();
}
});
menu.addHideHandler(new HideEvent.HideHandler() {
#Override
public void onHide(HideEvent event) {
removeHighlight();
}
});
menu.setStyleName(CSS.menu());
add(menuButtonImage);
}
private void addUserSettings() {
MenuItem userSettingsItem = new MenuItem("User Settings");
userSettingsItem.addSelectionHandler(new SelectionHandler<Item>() {
#Override
public void onSelection(SelectionEvent<Item> event) {
_coreLayout.showUserSettingsPage();
}
});
userSettingsItem.setStyleName(CSS.menu_item());
menu.add(userSettingsItem);
}
private void highlight() {
addStyleName(CSS.header_bar_icon_box_selected());
}
private void removeHighlight() {
removeStyleName(CSS.header_bar_icon_box_selected());
}

Trying to Add More tabs to tab panel upon clicking on a button

I would like to add more tabs to the tab panel upon receiving a reponse from a servelet.. the problem is that It only adds the last one and not the the others see part of the code below. It seems like it is only adding the last panel "Time Reports" but not the other two
Thank you
btnLogin.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if(getLoginResult())
{
HorizontalPanel temp = new HorizontalPanel();
panel.add(temp, "Add Hours");
panel.add(temp, "Time Sheets");
panel.add(temp, "Time Reports");
}
}
});
RootPanel.get().add(panel);
}
private boolean getLoginResult() {
AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Object result) {
isAuthenticated = true;
}
public void onFailure(Throwable caught) {
Window.alert("Error when invoking the pageable data service :" + caught.getMessage());
isAuthenticated = false;
}
};
timesheetLoginServlet.isAuthenticated("1","rapidjava", callback);
return isAuthenticated;
}
}
You can add any widget to its parent only once. Change the temp to temp1, temp2 and temp3