Java/GWT/PopupPanel

Материал из Java эксперт
Перейти к: навигация, поиск

Change style for PopupPanel

   <source lang="java">

package com.jexp.gwt.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.google.gwt.user.client.ui.PopupPanel; import com.google.gwt.user.client.ui.HTML; public class GWTClient implements EntryPoint {

 public void onModuleLoad() {
   Button b = new Button("Click me", new ClickListener() {
     public void onClick(Widget sender) {
       MyPopup p = new MyPopup();
       int left = sender.getAbsoluteLeft() + 10;
       int top = sender.getAbsoluteTop() + 10;
       p.setPopupPosition(left, top);
       p.show();
     }
   });
   RootPanel.get().add(b);
 }

} class MyPopup extends PopupPanel {

 public MyPopup() {
   super(true);
   HTML contents = new HTML("Click anywhere outside this popup to make it disappear.");
   contents.setWidth("128px");
   setWidget(contents);
   setStyleName("popups-Popup");
 }

}

      </source>
   
  
 
  



extends PopupPanel to create Tooltip

   <source lang="java">

package com.jexp.gwt.client; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import com.google.gwt.core.client.*; public class GWTClient implements EntryPoint{

 public void onModuleLoad() {
   Label label = new Label();
   label.setText("label with tooltip");
   
   
   
   label.addMouseListener(new MouseListenerAdapter() {
     public void onMouseEnter(Widget sender) {
        ToolTip tip = new ToolTip("this is a tooltip.",0,0);
     }
     public void onMouseLeave(Widget sender) {
     }
     public void onMouseDown(Widget sender, int x, int y) {
     }
     public void onMouseUp(Widget sender, int x, int y) {
     }
   });
   RootPanel.get().add(label);
 }

}

class ToolTip extends PopupPanel{
 final int VISIBLE_DELAY = 2000;
 
 Timer removeDelay;
 
 public ToolTip(String message, int x, int y){
     super(true);
     this.setPopupPosition(x, y);
     this.add(new Label(message));
     removeDelay = new Timer(){
     public void run() {
         ToolTip.this.setVisible(false);
       ToolTip.this.hide();
     }        
     };
     removeDelay.schedule(VISIBLE_DELAY);
     this.addPopupListener(new PopupListener(){
     public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
       removeDelay.cancel();
     }
     });
     this.setStyleName("toolTip");
     this.show();
   }
 
 public boolean onEventPreview(Event event){
     int type = DOM.eventGetType(event);
     switch(type){
        case Event.ONMOUSEDOWN:
        case Event.ONCLICK:{
          this.hide();
          return true;
        }
     }
     return false;
 }

}


      </source>
   
  
 
  



Set position of PopupPanel

   <source lang="java">

package com.jexp.gwt.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.google.gwt.user.client.ui.PopupPanel; import com.google.gwt.user.client.ui.HTML; public class GWTClient implements EntryPoint {

 public void onModuleLoad() {
   Button b = new Button("Click me", new ClickListener() {
     public void onClick(Widget sender) {
       MyPopup p = new MyPopup();
       int left = sender.getAbsoluteLeft() + 10;
       int top = sender.getAbsoluteTop() + 10;
       p.setPopupPosition(left, top);
       p.show();
     }
   });
   RootPanel.get().add(b);
 }

} class MyPopup extends PopupPanel {

 public MyPopup() {
   super(true);
   HTML contents = new HTML("Click anywhere outside this popup to make it disappear.");
   contents.setWidth("128px");
   setWidget(contents);
   setStyleName("popups-Popup");
 }

}


      </source>