Adding a click handler to a panel? - gwt

We have the interface:
AbsolutePanel ap = ...;
ap.addHandler(EventHandler h, Type<EventHandler> type);
How do we use the above? It seems like the interface for click events has changed a few times in GWT, and older questions on this topic are out of date,
Thanks

This should work:
AbsolutePanel ap = new AbsolutePanel();
ap.addHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
}
}, ClickEvent.getType());

Related

Focuslost event on Jcombobox in netbeans

I am trying to have a bind a focuslost event on my combobox but it's not happening.
Here is my code-:
jComboBox1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e){
}
public void focusLost(FocusEvent e){
JOptionPane.showConfirmDialog(null,"focuslost");
}
});
I also tried this-:
JComboBox default editor has an internal class BasicComboBoxEditor$BorderlessTextField that is the component that gets and loses focus.
It can be accessed simply by-:
Component component = comboBox.getEditor().getEditorComponent();
if (component instanceof JTextField)
JTextField borderlesstextfield = (JTextField) borderless;
But i am getting error on this line-
JTextField borderlesstextfield = (JTextField) borderless;
I am new to netbeans. Kindly guide me.Thank you in advance.
I tested this(Adding the JComboBox inside a JPanel ). If there are more elements inside the panel the focuslost is triggered when pressing tab or clicking on another element.
Considering that you do not have any other elements or you want the focus lost event to trigger also when you click somewhere on the window:
Keep your focus listener as is and add the following after the auto-generated initComponents():
jPanel1.setFocusable(true);
jPanel1.setRequestFocusEnabled(true);
jPanel1.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {
jPanel1.requestFocusInWindow();
}
#Override
public void mouseReleased(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
});

GWT : Return VerticalPanel from a function in ClickHandler

I am trying to get a pop up when an image is clicked. below is the piece of code:
while(index<bookList.size()){
if(cellCount<4){
image[index] = new Image(imageList.get(index));
final int imageIndex = index;
table.setWidget(row, cellCount, new Image(bookList.get(index)));
table.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
VerticalPanel vPanel = new VerticalPanel();
vPanel = imageDetail.getPopup(popup, image[imageIndex]);
popup.setAnimationEnabled(true);
popup.setGlassEnabled(true);
popup.setAutoHideEnabled(false);
popup.setWidth("300px");
popup.setHeight("300px");
popup.add(vPanel);
popup.center();
}});
table.getFlexCellFormatter().setStyleName(row, cellCount, "ImageCell");
cellCount++;
index++;
}
Above code contain clickhandler which calls "getPopup" of imageDetail object. "getPopup" function returns a vertical Panel which is added to PopupPanel. ImageDetail class is as follows
public class ImageDetails extends Composite {
private Image closeButton;
VerticalPanel getPopup(final PopupPanel popup, Image image){
VerticalPanel vPanel = new VerticalPanel();
//Close Button
HorizontalPanel closePanel = new HorizontalPanel();
closeButton = new Image("/Images/closebutton.jpg");
closeButton.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
popup.hide();
}});
closePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
closePanel.add(closeButton);
vPanel.add(closePanel);
VerticalPanel imagePanel = new VerticalPanel();
imagePanel.add(image);
vPanel.add(imagePanel);
return vPanel;
}
My questions are as following :
1) Is it ok to return panels from a function? If no, then why? If yes, why above code gives "Uncaught exception escaped com.google.gwt.event.shared.UmbrellaException: 3 exceptions caught: null; null; null"?
2) When i try to add vertical panel to popup panel in "getPopup" function and get popup panel from there, code returning same error as mentioned in first question. What am I missing?
Am i missing any initWidget(w) function call?
Thank you

onMouseOver event on an image array is getting called only once in gwt

I have an image array attached to a DeckPanel. What I want is to have a pop-up on mouse hover on images which vanishes on mouseOut.
Code is as follows :
final PopupPanel popup = new PopupPanel();
for(int i=0; i<images.size();++i){
final int index = i;
image[i] = new Image(images.get(i));
image[i].addMouseOutHandler(new MouseOutHandler(){
#Override
public void onMouseOut(MouseOutEvent event) {
// TODO Auto-generated method stub
popup.hide();
}});
image[i].addMouseOverHandler(new MouseOverHandler(){
#Override
public void onMouseOver(MouseOverEvent event) {
// TODO Auto-generated method stub
Label label = new Label(images.get(index));
popup.add(label);
popup.showRelativeTo(image[index]);
}});
image[i].addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
Window.alert("image clicked");
}});
deck.add(image[i]);
}
Problem : onMouseOver() is getting only once. Could you guys please help me understand whats happening here and how tell me how to make it work for all images every time dat image is loaded?
onMouseOver you are repeatedly adding label to the popup.PopupPanel is a SimplePanel it can add only one widget.So onMouserOver first clear the PopupPanel
popup.clear();
Label label = new Label(images.get(index));
popup.add(label);
popup.showRelativeTo(image[index]);

Capturing mouse events on horizontal panel in GWT

Is there a way to capture mouse events on a horizontal panel in gwt ?
I am trying to capture mousedown or onclick events but m not able to get it working. This is what i have done so far
final HorizontalPanel container = new HorizontalPanel() {
#Override
public void sinkEvents(int eventBitsToAdd) {
// TODO Auto-generated method stub
super.sinkEvents(Event.ONCLICK);
}
#Override
public void onBrowserEvent(final Event event) {
// TODO Auto-generated method stub
super.onBrowserEvent(event);
if (DOM.eventGetType(event) == Event.ONCLICK) {
System.out.println("event type -->> " + event.getType());
}
/*if(Event.ONMOUSEDOWN == arg0.getTypeInt())
System.out.println("event type -->> " + arg0.getType());*/
}
};
I have no clue why this doesn't work.
any help would be appreciated.
Thanks
Instead of manually sinking and reading events, you could consider using Widget#addDomHandler():
HorizontalPanel container = new HorizontalPanel();
ClickHandler cHandler = new ClickHandler(){ /* ... */ };
MouseDownHandler mdHandler = new MouseDownHandler(){ /* ... */ };
container.addDomHandler(cHandler, ClickEvent.getType());
container.addDomHandler(mdHandler, MouseDownEvent.getType());
You can use FocusPanel. For example:
HorizontalPanel yourContainer = new HorizontalPanel();
FocusPanel wrapper = new FocusPanel();
wrapper.add(yourContainer );
wrapper.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// Handle the click
}
});

DialogBox in GWT isn't draggable or centred

I'm new to GWT programming. So far I have a DialogBox which is supposed to collect a login and a password, which can if required launch another DialogBox that allows someone to create a new account.
The first of these two DialogBoxes always appears at the top left of the browser screen, and can't be dragged, although part of the definition of a DialogBox is that it can be dragged. However, the second DialogBox can be dragged about the screen without any problem.
What I'd really like is for the first DialogBox to appear in the middle of the screen & be draggable, both of which I thought would happen automatically, but there's not.
So, what things can stop a DialogBox from being draggable? There is nothing on the RootPanel yet. Does that make a difference?
Code fragments available if they help, but perhaps this general outline is enough for some pointers.
Thanks
Neil
Use dialogBox.center() This will center your DialogBox in the middle of the screen. Normally a DialogBox is by default draggable.
Just tried it out and it doens't matter if your RootPanel is empty our not. When I just show the DialogBox on ModuleLoad it is draggable and it is centered. Probably the problem is situated somewhere else.
This is the example of google itself:
public class DialogBoxExample implements EntryPoint, ClickListener {
private static class MyDialog extends DialogBox {
public MyDialog() {
// Set the dialog box's caption.
setText("My First Dialog");
// DialogBox is a SimplePanel, so you have to set its widget property to
// whatever you want its contents to be.
Button ok = new Button("OK");
ok.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
MyDialog.this.hide();
}
});
setWidget(ok);
}
}
public void onModuleLoad() {
Button b = new Button("Click me");
b.addClickListener(this);
RootPanel.get().add(b);
}
public void onClick(Widget sender) {
// Instantiate the dialog box and show it.
new MyDialog().show();
}
}
Here more information about the DialogBox.
Without seeing any of your code it's hard to tell what's going wrong. The following code works for me (ignore the missing styling...):
public void onModuleLoad() {
FlowPanel login = new FlowPanel();
Button create = new Button("create");
login.add(new TextBox());
login.add(new TextBox());
login.add(create);
create.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
final DialogBox box = new DialogBox();
FlowPanel panel = new FlowPanel();
Button close = new Button("close");
close.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
box.hide();
}
});
panel.add(new Label("some content"));
panel.add(close);
box.setWidget(panel);
box.center();
}
});
DialogBox firstBox = new DialogBox(false, true);
firstBox.setWidget(login);
firstBox.center();
}
Both boxes are draggable and shown in the center of your browser window.
Looks like you're overriding this method in Widget:
public void fireEvent(GwtEvent<?> event) {
if (handlerManager != null) {
handlerManager.fireEvent(event);
}
}
In Widget, handlerManager refers to a private HandlerManager.
Either add super.fireEvent(event) to your method or as you have done rename it.
Well, with vast amounts of trial and error I have found the problem, which was just this: I had a method in an object I'd based on DialogBox called fireEvent, which looked like this:
public void fireEvent(GwtEvent<?> event)
{
handlerManager.fireEvent(event);
}
Then, when a button was clicked on the DialogBox, an event would be created and sent off to the handlerManager to be fired properly.
And it turns out that if I change it to this (LoginEvent is a custom-built event):
public void fireEvent(LoginEvent event)
{
handlerManager.fireEvent(event);
}
... or to this ....
public void fireAnEvent(GwtEvent<?> event)
{
handlerManager.fireEvent(event);
}
the DialogBox is draggable. However, if the method begins with the line
public void fireEvent(GwtEvent<?> event)
then the result is a DialogBox which can't be dragged.
I'm a bit unsettled by this, because I can't fathom a reason why my choice of name of a method should affect the draggability of a DialogBox, or why using a base class (GwtEvent) instead of a custom class that extends it should affect the draggability. And I suspect there are dozens of similar pitfalls for a naive novice like me.
(Expecting the DialogBox to centre itself was simply my mistake.)