Table Single Row Click 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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » GWT » FlexTable 




Table Single Row Click 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;
    
    /** Creates a new instance of Person */
    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-TableSingleRowClickEvent.zip( 4 k)
Related examples in the same category
1.Use FlexTable
2.FlexTable with row style
3.Scroll FlexTable
4.Table on Browser 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)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.