FlexTable with components : 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 
FlexTable with components
 

package com.java2s.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{
  private final Object[][] rowData = {
  new DeleteButton("Delete")"A""B""C""D"},
  new DeleteButton("Delete")"E""F",  "G""H"},
  new DeleteButton("Delete")"I""J""K""L"}
  };
  
  private final ComponentFlexTable table = new ComponentFlexTable(rowData);

  private void onCellClicked(SourcesTableEvents sender, final int row, final int cell){
     if (row == 0) {
       return;
     }   
     final Widget widget = table.getWidget(row, cell);
       
     if (widget instanceof Label == false) {
          return;
     
     final Label label = (Label)widget;
     final TextBox textBox = new TextBox();
     textBox.setText(table.getText(row, cell));
     table.setWidget(row, cell, textBox);
     DeferredCommand.addCommand(new Command() {
       public void execute() {
         textBox.setFocus(true);
         textBox.selectAll();
       }
     });            

                 
     textBox.addKeyboardListener(
         new KeyboardListenerAdapter() {
           public void onKeyPress(Widget sender, 
               char keyCode, int modifiers) {
             if (keyCode == KeyboardListener.KEY_ENTER) {
               label.setText(textBox.getText());  
               table.setWidget(row, cell, label);
             else if (keyCode ==
               KeyboardListener.KEY_ESCAPE) {
               table.setWidget(row, cell, label);
             }
           }
     })
     textBox.addFocusListener(new FocusListenerAdapter() {
       public void onLostFocus(Widget sender) {
         label.setText(textBox.getText());
         table.setWidget(row, cell, label);
       }
     });                
  }
  public void onModuleLoad() {
    table.addColumn("");    
    table.addColumn("Column 1");
    table.addColumn("Column 2");
    table.addColumn("Column 3");
    table.addColumn("Column 4");
    
    RootPanel.get().add(table);
    
    table.addTableListener(new TableListener() {
      public void onCellClicked(SourcesTableEvents sender, final int row, final int cell) {
        GWTClient.this.onCellClicked(sender, row, cell);
      }
    });
  }
  private class DeleteButton extends Button {
    public DeleteButton(String text) {
      super(text);
      addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
          table.removeRow(getThisRow());
          table.updateRowStyle();
        }
      });
    }
    private int getThisRow() {
      for (int i=1; i < table.getRowCount(); ++i) {
        if (table.getWidget(i, 0== this) {
          return i;
        }
      }
      return -1;
    }
  }
}

class ComponentFlexTable extends FlexTable {
  protected static final int HEADER_ROW = 0;

  private int rowIndex = 1;
  private Object[][] rowData = null;

  public ComponentFlexTable(Object[][] rowData) {
    insertRow(HEADER_ROW);
    getRowFormatter().addStyleName(HEADER_ROW,"ComponentFlexTable-Header");
    setCellSpacing(0);
    addStyleName("ComponentFlexTable");        
    this.rowData = rowData;
    createRows(0);
    updateRowStyle();
  }

  public void createRows(int rowIndex) {
    if (rowData == null)
      return;

    for (int row = rowIndex; row < rowData.length; row++) {
      addRow(rowData[row]);
    }
  }

  private void addRow(Object[] cellObjects) {
    for (int i = 0; i < cellObjects.length; i++) {
      addCell(rowIndex, i, cellObjects[i]);
    }
    rowIndex++;
  }

  public void addCell(int row, int cell, Object cellObject) {
    if (cellObject instanceof Widget)
      setWidget(row, cell, (WidgetcellObject);
    else
      setWidget(row, cell,new Label(cellObject.toString()));
    getCellFormatter().addStyleName(row, cell,"ComponentFlexTable-Cell");    
  }
  
  public void updateRowStyle() {
    HTMLTable.RowFormatter rf = getRowFormatter();
    for (int row = 1; row < getRowCount(); ++row) {
      if ((row % 2!= 0) {
        rf.removeStyleName(row,"ComponentFlexTable-EvenRow");
        rf.addStyleName(row,"ComponentFlexTable-OddRow");
      }else {
        rf.removeStyleName(row,"ComponentFlexTable-OddRow");
        rf.addStyleName(row,"ComponentFlexTable-EvenRow");
      }
    }
  }

  public void addColumn(Object columnHeading) {
    Widget widget = new Label(columnHeading.toString());
    int columnIndex = getColumnCount();
    widget.setWidth("100%");
    widget.addStyleName("ComponentFlexTable-ColumnLabel");

    setWidget(HEADER_ROW, columnIndex, widget);

    getCellFormatter().addStyleName(HEADER_ROW, columnIndex,"ComponentFlexTable-ColumnLabelCell");
  }

  public int getColumnCount() {
    return getCellCount(HEADER_ROW);
  }
}
/////////////
.ComponentFlexTable {
  border-top: thin solid #444444;
  border-left: thin solid #444444;
  border-right: thin solid #111111;
  border-bottom: thin solid #111111;
  background-color:  #505050;
}

.ComponentFlexTable-Header {
}

.ComponentFlexTable-OddRow {
  background-color: #cccccc;
}

.ComponentFlexTable-EvenRow {
  background-color:  #505050;
}


.ComponentFlexTable-ColumnLabel {
  color: white;
  padding: 3px;
}

.ComponentFlexTable-ColumnLabelCell {
  color: white;
  border-width: 0 0 0 1px;
  border-style: solid;
  border-color: white;
  margin: 0;
  padding: 0;
  text-align: center;
}

.ComponentFlexTable-Cell {
  border-width: 0px 0px 0px 1px;
  border-style: solid;
  color: white;
  border-color: white;
  padding: 5px;
}



           
         
  
GWT-componentFlexTable.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 on Browser Event
6.Table Mouse Over Event
7.extends FlexTable implements TableListener
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.