Table on Browser Event : FlexTable « GWT « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Java » GWT » FlexTableScreenshots 
Table on Browser Event
 


package com.java2s.gwt.client;

import java.util.*;
import java.util.Set;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;


public class GWTClient implements EntryPoint, HistoryListener {

    private Table table;
    
    private void getData(){
        ArrayList values = new ArrayList();
        
        values.addnew Person ("A""H""E""6" ));
        values.addnew Person("B""G",  "E""19" ));
        values.addnew Person("C""F""A""17" ));
        values.addnew Person"D""E""A" "17"));
        
        table.setSourcenew SimpleDataSource( (Person[]) values.toArraynew Person[values.size()]) ) );
    }
    
    public void onModuleLoad() {
        table = new Tablenull, "myTable" );
        table.setStyleName("myTable");
        RootPanel.get().add(table);
        getData();
        
        table.addTableListenernew TableListener(){
            public void onCellClicked(SourcesTableEvents sender, 
                int row, int cell) {
                History.newItem""+row );
            }
        });
        History.addHistoryListenerthis );
    }
    
    public void onHistoryChanged(String historyToken){
        table.onCellClickedtable, Integer.parseInthistoryToken )0);
    }
    
}
 class Person {
    
    public String firstName;
    public String lastName;
    public String company;
    public String counts;
    
    public Person() {
    }
    
    public PersonString firstName, String lastName, String company, String counts ){
        this.firstName = firstName;
        this.lastName = lastName;
        this.company = company;
        this.counts = counts;
    }
    
}

class Table extends FlexTable implements TableListener {
    private String headerStyle;
    private String selectedStyle;
    private TableDataSource source;
    private int selectedRow;
    
    public Table(TableDataSource source, String stylePrefix) {
        super();
        this.setCellPadding(1);
        this.setCellSpacing(0);
        this.setWidth("100%");
        this.selectedStyle = stylePrefix + "-selected";
        this.headerStyle = stylePrefix + "-header";
        this.addTableListener(this);
        this.setSourcesource );
        
        sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONMOUSEOVER |Event.ONMOUSEOUT);         
    }

    public void onBrowserEvent(Event event) {
            Element td = getEventTargetCell(event);
            if (td == nullreturn;
            Element tr = DOM.getParent(td);
            switch (DOM.eventGetType(event)) {
                    case Event.ONMOUSEDOWN: {
                            DOM.setStyleAttribute(tr, "backgroundColor""#ffce00");
                            onRowClick(tr);
                            break;
                    }
                    case Event.ONMOUSEUP: {
                            DOM.setStyleAttribute(tr, "backgroundColor""#ffffff");
                            break;
                    }
                    case Event.ONMOUSEOVER: {
                            DOM.setStyleAttribute(tr, "backgroundColor""#ffce00");
                            onRowRollover(tr);
                            break;
                    }
                    case Event.ONMOUSEOUT: {
                            DOM.setStyleAttribute(tr, "backgroundColor","#ffffff");
                            break;
                }
        }
    
    }
    public void onRowClick(Element event){
           
    
    }
    public void onRowRollover(Element event){
           
    
    }

    public void onCellClicked(SourcesTableEvents sender, 
                int row, int cell) {
        this.getRowFormatter()
                .removeStyleName(selectedRow, selectedStyle);
        if ((source.getHeaderRow() == null|| (row > 0)) {
            this.getRowFormatter()
                .addStyleName(row, selectedStyle);
            selectedRow = row;
        }
    }
    
    public void setSourceTableDataSource source ){
        forint i = this.getRowCount(); i > 0; i-- ){
            this.removeRow);
        }
        ifsource == null ){
            return;
        }
        
        int row = 0;
        String[] headers = source.getHeaderRow();
        ifheaders != null ){
            forint i=0; i < headers.length; i++ ){
                this.setTextrow, i , headers[i] );
            }
            this.getRowFormatter().addStyleNamerow, headerStyle );
            row++;
        }
        int rows = source.getRowCount();
        forint i=; i <rows; i++ ){
            String[] values = source.getRow(i);
            forint col=0; col < values.length; col++ ){
                this.setTextrow, col, values[col] );
            }
            row++;
        }
        this.selectedRow = 0;
        this.source = source;
    }
}

 class SimpleDataSource implements TableDataSource {
    
    private String[] headers; 
    private String[][] data;
    
    public SimpleDataSource(Person[] people) {
        super();
        String[] headers = "First Name""Last Name""Company""Counts" };
        this.headers = headers;
        this.data = new String[people.length][4];
        forint i=0; i < people.length ; i++ ){
            Person p = people[i];
            this.data[i][0= p.firstName;
            this.data[i][1= p.lastName;
            this.data[i][2= p.company;
            this.data[i][3= p.counts;
        }
    }

    public int getRowCount() {
        return data.length;
    }

    public String[] getRow(int i) {
        return data[i];
    }

    public String[] getHeaderRow() {
        return headers;
    }
   
}


interface TableDataSource {
    
    public String[] getHeaderRow();
    
    public int getRowCount();
    
    public String[] getRow(int row);
    
}



           
         
  
GWT-TableonBrowserEvent.zip( 4 k)
Related examples in the same category
1.Use FlexTable
2.FlexTable with row style
3.Scroll FlexTable
4.Table Single Row Click Event
5.Table Mouse Over Event
6.extends FlexTable implements TableListener
7.FlexTable with components
8.Using FlexTable to layout buttons (Ext GWT)Using FlexTable to layout buttons (Ext GWT)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.