2012-02-16 65 views
0

我有一個EntryPoint類WebCrawlerOne.java,其中包含TabLayoutPanel,並在其中一個選項卡中調用CrawlerOne.java,其中包含CellTable。我嘗試了很多,但無法找到任何錯誤。我試圖搜索相關主題,但仍然在項目運行時,CellTable在TabLayoutPanel中不可見。CellTable在TabLayoutPanel中不可見

  1. 我在標準模式下使用GWT 2.2.4
  2. (使用DOCTYPE HTML)所指定的TabLayoutPanel在CSS作爲.gwt-TabLayoutPanel高度{ 高度:100%; }

    CrawlerOne.ui.xml看起來like-- G內。:HTMLpanel標籤

    Hello, 
    <g:Button styleName="{style.important}" ui:field="button" /> 
    <table cellspacing='0' cellpadding='0' style='width:100%;'> 
        <tr> 
         <td valign='top'> 
          <c:CellTable addStyleNames="{style.cellTable}" ui:field="table"/> 
         </td> 
        </tr> 
    </table>  
    
    <g:TextBox ui:field="box"></g:TextBox> 
    <g:Button ui:field="addSite"></g:Button> 
    

CrawlerOne.java看起來like--

public class CrawlerOne extends Composite implements HasText { 

interface CrawlerOneUiBinder extends UiBinder<Widget, CrawlerOne> { 
} 

@UiField 
CellTable<Contact> table; 

@UiField 
Button addSite; 

@UiField 
TextBox box; 

public CrawlerOne() { 
    Widget w = new Widget(); 
    w = onInitialize(); 
    initWidget(w);  
} 

@UiField 
Button button; 

@UiHandler("button") 
void onClick(ClickEvent e) { 
    Window.alert("Hello!"); 
} 

public void setText(String text) { 
    button.setText(text); 
} 

public String getText() { 
    return button.getText(); 
} 

/** 
    * A simple data type that represents a contact with a unique ID. 
    */ 
    private static class Contact { 
    private static int nextId = 0; 
    private final int id; 

    private String site; 
    private String document; 
    private String pending; 

    public Contact(String site, String document, String pending) { 
     nextId++; 
     this.id = nextId; 
     this.site = site; 
     this.document = document; 
     this.pending = pending; 
    } 

    public String getDocument() { 
     return document; 
    } 

    public int getId() { 
     return id; 
    } 

    public String getSite() { 
     return site; 
    } 

    public void setSite(String site) { 
     this.site = site; 
    } 

    public String getPending() { 
     return pending; 
    } 
    } 

    /** 
    * The list of data to display. 
    */ 
private static final List<Contact> CONTACTS = new LinkedList<Contact>(Arrays.asList(
     new Contact("www.google.com", "12", "123"), 
     new Contact("www.yahoo.com", "23", "124"), 
     new Contact("www.rediff.com", "24", "124"), 
     new Contact("www.gmail.com", "23", "124"), 
     new Contact("www.facebook.com", "23", "124"), 
     new Contact("www.flickr.com", "23", "124"), 
     new Contact("www.youtube.com", "45", "125"))); 

private static final ProvidesKey<Contact> KEY_PROVIDER = 
     new ProvidesKey<Contact>() { 
     @Override 
     public Object getKey(Contact object) { 
      return object.getId(); 
     } 
     }; 

    /** 
    * Initialize. 
    */ 
     public Widget onInitialize() { 
     table = new CellTable<Contact>(KEY_PROVIDER); 
     table.setWidth("100%", true); 
     table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); 

     initTableColumns(); 

     // Push the data into the widget. 
     table.setRowData(CONTACTS); 

     // Set table width. 
     table.setWidth("100%"); 

     box = new TextBox(); 
     box.setText("Enter New Site"); 

     addSite = new Button("Add Row"); 
     addSite.addClickHandler(new ClickHandler() { 

      @UiHandler("addSite") 
      public void onClick(ClickEvent event) { 
       // TODO Auto-generated method stub 
       try { 
        if(box.getValue()=="") { 
         Window.alert("Error: Invalid Site Value"); 
         box.setText("Enter New Site"); 
        } 
        else { 
         CONTACTS.add(new Contact(box.getValue(), "23", "127")); 
         table.setRowCount(CONTACTS.size(), true); 
         table.setRowData(CONTACTS); 
         table.redraw(); 
        } 
       }catch (Exception e) { 
        Window.alert("Exception Error: " + e); 
       } 
      } 
     }); 

    // Create the UiBinder. 
     CrawlerOneUiBinder uiBinder = GWT.create(CrawlerOneUiBinder.class); 
     Widget widget = uiBinder.createAndBindUi(this); 

     return widget; 
    } 

private void initTableColumns() { 

    //code to add columns 
}}//end of file 

和WebCrawlerOne.java看起來like--

public class WebCrawlerOne implements EntryPoint { 
    /** 
    * This is the entry point method. 
    */ 
    public void onModuleLoad() { 
     TabLayoutPanel menu = new TabLayoutPanel(10, Unit.EM); 

     menu.add(new CrawlerOne(), "Crawler"); 
     menu.add(new HTML("This is my page!"), "Page 2"); 
     menu.selectTab(0); 
     RootLayoutPanel.get().add(menu); 
    } 
    } 

輸出看起來like-- enter image description here

如果直接從入口點類中運行正在顯示的CellTable。任何幫助讚賞。謝謝!

回答

2

在我意識到簡單的答案之前,我寫了一點點,我認爲你真的需要,所以首先,簡單的答案,然後再考慮更多的事情。

您正在ui.xml文件中創建CellTable的新實例。這將取代onInitialize中已配置的CellTable實例,這意味着它沒有列,因此不可見。對uibinder.createAndBind的調用將通過並創建所有帶有@UiField註釋的小部件,除非您將它們標記爲已提供。有兩個選項:

  • 將其標記爲規定:

    @UiField(provided = true) 
    CellTable<Contact> table; 
    
  • 不要配置uibinder.createAndBind被稱爲直到後

    //... 
    Widget widget = uiBinder.createAndBindUi(this); 
    table.setWidth("100%", true); 
    table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); 
    
    initTableColumns(); 
    
    // Push the data into the widget. 
    table.setRowData(CONTACTS); 
    
    // Set table width. 
    table.setWidth("100%"); 
    

第一種選擇可能是更好的表,所以你可以在繪製任何東西前完全配置它。也就是說,我還沒有測試過(沒有提供足夠的代碼來輕鬆測試)。


各種LayoutPanels(通常,實現東西ProvidesResize和RequiresResize)大小自己的孩子,無論基於何種特定的規則,每個人都有。

這些只能調整他們的直接孩子 - 如果孩子不需要調整大小(如HTMLPanel),他們就不會在那裏做任何佈局工作,而是期望該容器調整孩子的大小,但它看起來合適。

因此,HtmlPanel和您在ui.xml文件中繪製的html有可能以允許TabLayoutPanel將佈局信息正確傳遞到CellTable的方式獲得。如果你不想使用佈局的東西,那麼這是一件好事 - 你需要以其他方式調整CellTable的大小。

最後一個想法:使用FireBug或類似的東西來確保dom被設置爲你所期望的。在那裏你會看到celltable存在,但沒有任何內容。

+0

非常感謝! @UiField(provided = true)缺失。由於其他錯誤,我早先刪除了它,然後忘了放回去。非常感謝! :) – Prince 2012-02-17 01:58:18