GWT - flextable - 添加焦点侦听器

发布于 2024-11-17 16:50:06 字数 7861 浏览 3 评论 0原文

我正在开发一个可编辑的弹性表。单击某个单元格时,它会变成该特定单元格内的文本框。文本框包含单击的原始单元格的文本。由于“addFocustListener”函数已被弃用,我应该使用哪些函数将输入到文本框中的新文本复制到弹性表中。

以下是我的代码:

package testproject3.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusListenerAdapter;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ViewAndEditCompWidget 
{
    VerticalPanel vPanel=null;
    FlexTable table =null;



    public VerticalPanel getvPanel() 
    {
        table = new FlexTable();
        vPanel = new VerticalPanel();

        table.setText(0, 0, " Name");
        table.setText(0, 1, " Address aasdfasfasfdasfasfdasfasdfasfasdfasfsafasdfsadfsdfasfsdfasdfsafasdfasdfasdf");        

        table.setText(1, 0, " My Name");
        table.setText(1, 1, " 123456789  asfasdfasfdddddddddddddddddddddddddddd");

        makeTableEditable();
        vPanel.add(table);
        return vPanel;
    }
    public void makeTableEditable()
    {
        table.addClickHandler(new ClickHandler()
        {
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(ClickEvent event) 
            {
                final int cellIndex = table.getCellForEvent(event).getCellIndex();
                final int rowIndex =    table.getCellForEvent(event).getRowIndex();

                //Cell cell = myTable.getCellForEvent(event);
                //int receiverRowIndex = cell.getRowIndex(); 

                //make sure that it is not the header row
                if(rowIndex != 0)
                {
                    //make a new Textbox
                    final TextBox textBox = new TextBox();
                    textBox.setText(table.getText(rowIndex, cellIndex));
                    table.setWidget(rowIndex, cellIndex, textBox);

                    /*********************************************
                    DeferredComman is deprecated
                    *********************************************/
                    DeferredCommand.addCommand(new Command()
                    {
                        @Override
                        public void execute() 
                        {
                            textBox.setFocus(true);
                            textBox.selectAll();

                        }

                    }
                    );

                    /*********************************************
                    addFocusListener is also deprecated
                    *********************************************/
                    textBox.addFocusListener(new FocusListenerAdapter()
                    {
                        public void onLostFocus(Widget sender)
                        {
                            table.setText(rowIndex, cellIndex, textBox.getText());
                        }
                    }
                    );

                    //add a keyboard listener to the text box
                    //so that it reacts to enter and ESC key
                    textBox.addKeyPressHandler(new KeyPressHandler() 
                    {
                        @Override
                        public void onKeyPress(KeyPressEvent event) 
                        {
                            //if key pressed was ENTER, copy the text box's text into the table
                            if (event.getCharCode() == KeyCodes.KEY_ENTER) 
                            {
                                table.setText(rowIndex, cellIndex, table.getText(rowIndex, cellIndex));
                            }
                            else if (event.getCharCode() == KeyCodes.KEY_ESCAPE) 
                            {
                                table.setText(rowIndex, cellIndex, textBox.getText()+"");
                            }
                        }//onKeyPress
                    });//addKeyPressHandler
                }//if
            }

        });

    }

}

DeferredCommandaddFocustListener 已弃用。可以用什么来代替它们呢?

以下是我的新代码:

public class CellTableExample implements EntryPoint {

  /**
   * A simple data type that represents a contact.
   */
  private static class Contact {
    private final String address;
    private final Date birthday;
    private final String name;

    public Contact(String name, Date birthday, String address) {
      this.name = name;
      this.birthday = birthday;
      this.address = address;
    }
  }

  /**
   * The list of data to display.
   */
  @SuppressWarnings("deprecation")
  private static final List<Contact> CONTACTS = Arrays.asList(
      new Contact("John", new Date(80, 4, 12), "123 Abc Avenue"), 
      new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln fasfasdfasfdasdfasfasdfasfasdfasfasfasdfasdfasdf"), 
      new Contact("Tom", new Date(85, 3, 22), "33 Lance Lnasdfasfdasdfffffffffffffffffff"), 
      new Contact("Jack", new Date(85, 4, 22), "44 Lance Lnsddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"));

  public void onModuleLoad() {
    // Create a CellTable.
    final CellTable<Contact> table = new CellTable<Contact>();
    // Display 3 rows in one page
    table.setPageSize(3);

    // Add a text column to show the name.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.name;
      }
    };
    table.addColumn(nameColumn, "Name");

    // Add a date column to show the birthday.
    DateCell dateCell = new DateCell();
    Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) {
      @Override
      public Date getValue(Contact object) {
        return object.birthday;
      }
    };
    table.addColumn(dateColumn, "Birthday");

    // Add a text column to show the address.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.address;
      }
    };
    table.addColumn(addressColumn, "Address");

    // Associate an async data provider to the table
    // XXX: Use AsyncCallback in the method onRangeChanged
    // to actaully get the data from the server side
    AsyncDataProvider<Contact> provider = new AsyncDataProvider<Contact>() {
      @Override
      protected void onRangeChanged(HasData<Contact> display) {
        int start = display.getVisibleRange().getStart();
        int end = start + display.getVisibleRange().getLength();
        end = end >= CONTACTS.size() ? CONTACTS.size() : end;
        List<Contact> sub = CONTACTS.subList(start, end);
        updateRowData(start, sub);
      }
    };
    provider.addDataDisplay(table);
    provider.updateRowCount(CONTACTS.size(), true);

    SimplePager pager = new SimplePager();
    pager.setDisplay(table);

    VerticalPanel vp = new VerticalPanel();
    vp.add(table);
    vp.add(pager);

    // Add it to the root panel.
    RootPanel.get().add(vp);
  }
}

I'm working on a editable flextable. When a cell is clicked, it turns into a textbox within that particular cell. The textbox contains the text of the original cell clicked. Since the "addFocustListener" function is deprecated, which functions should I use to copy the new text typed into the textbox into the flextable.

Following is my code:

package testproject3.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusListenerAdapter;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ViewAndEditCompWidget 
{
    VerticalPanel vPanel=null;
    FlexTable table =null;



    public VerticalPanel getvPanel() 
    {
        table = new FlexTable();
        vPanel = new VerticalPanel();

        table.setText(0, 0, " Name");
        table.setText(0, 1, " Address aasdfasfasfdasfasfdasfasdfasfasdfasfsafasdfsadfsdfasfsdfasdfsafasdfasdfasdf");        

        table.setText(1, 0, " My Name");
        table.setText(1, 1, " 123456789  asfasdfasfdddddddddddddddddddddddddddd");

        makeTableEditable();
        vPanel.add(table);
        return vPanel;
    }
    public void makeTableEditable()
    {
        table.addClickHandler(new ClickHandler()
        {
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(ClickEvent event) 
            {
                final int cellIndex = table.getCellForEvent(event).getCellIndex();
                final int rowIndex =    table.getCellForEvent(event).getRowIndex();

                //Cell cell = myTable.getCellForEvent(event);
                //int receiverRowIndex = cell.getRowIndex(); 

                //make sure that it is not the header row
                if(rowIndex != 0)
                {
                    //make a new Textbox
                    final TextBox textBox = new TextBox();
                    textBox.setText(table.getText(rowIndex, cellIndex));
                    table.setWidget(rowIndex, cellIndex, textBox);

                    /*********************************************
                    DeferredComman is deprecated
                    *********************************************/
                    DeferredCommand.addCommand(new Command()
                    {
                        @Override
                        public void execute() 
                        {
                            textBox.setFocus(true);
                            textBox.selectAll();

                        }

                    }
                    );

                    /*********************************************
                    addFocusListener is also deprecated
                    *********************************************/
                    textBox.addFocusListener(new FocusListenerAdapter()
                    {
                        public void onLostFocus(Widget sender)
                        {
                            table.setText(rowIndex, cellIndex, textBox.getText());
                        }
                    }
                    );

                    //add a keyboard listener to the text box
                    //so that it reacts to enter and ESC key
                    textBox.addKeyPressHandler(new KeyPressHandler() 
                    {
                        @Override
                        public void onKeyPress(KeyPressEvent event) 
                        {
                            //if key pressed was ENTER, copy the text box's text into the table
                            if (event.getCharCode() == KeyCodes.KEY_ENTER) 
                            {
                                table.setText(rowIndex, cellIndex, table.getText(rowIndex, cellIndex));
                            }
                            else if (event.getCharCode() == KeyCodes.KEY_ESCAPE) 
                            {
                                table.setText(rowIndex, cellIndex, textBox.getText()+"");
                            }
                        }//onKeyPress
                    });//addKeyPressHandler
                }//if
            }

        });

    }

}

DeferredCommand and addFocustListener are deprecated. What can be used in their places?

Following is my new code:

public class CellTableExample implements EntryPoint {

  /**
   * A simple data type that represents a contact.
   */
  private static class Contact {
    private final String address;
    private final Date birthday;
    private final String name;

    public Contact(String name, Date birthday, String address) {
      this.name = name;
      this.birthday = birthday;
      this.address = address;
    }
  }

  /**
   * The list of data to display.
   */
  @SuppressWarnings("deprecation")
  private static final List<Contact> CONTACTS = Arrays.asList(
      new Contact("John", new Date(80, 4, 12), "123 Abc Avenue"), 
      new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln fasfasdfasfdasdfasfasdfasfasdfasfasfasdfasdfasdf"), 
      new Contact("Tom", new Date(85, 3, 22), "33 Lance Lnasdfasfdasdfffffffffffffffffff"), 
      new Contact("Jack", new Date(85, 4, 22), "44 Lance Lnsddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"));

  public void onModuleLoad() {
    // Create a CellTable.
    final CellTable<Contact> table = new CellTable<Contact>();
    // Display 3 rows in one page
    table.setPageSize(3);

    // Add a text column to show the name.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.name;
      }
    };
    table.addColumn(nameColumn, "Name");

    // Add a date column to show the birthday.
    DateCell dateCell = new DateCell();
    Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) {
      @Override
      public Date getValue(Contact object) {
        return object.birthday;
      }
    };
    table.addColumn(dateColumn, "Birthday");

    // Add a text column to show the address.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.address;
      }
    };
    table.addColumn(addressColumn, "Address");

    // Associate an async data provider to the table
    // XXX: Use AsyncCallback in the method onRangeChanged
    // to actaully get the data from the server side
    AsyncDataProvider<Contact> provider = new AsyncDataProvider<Contact>() {
      @Override
      protected void onRangeChanged(HasData<Contact> display) {
        int start = display.getVisibleRange().getStart();
        int end = start + display.getVisibleRange().getLength();
        end = end >= CONTACTS.size() ? CONTACTS.size() : end;
        List<Contact> sub = CONTACTS.subList(start, end);
        updateRowData(start, sub);
      }
    };
    provider.addDataDisplay(table);
    provider.updateRowCount(CONTACTS.size(), true);

    SimplePager pager = new SimplePager();
    pager.setDisplay(table);

    VerticalPanel vp = new VerticalPanel();
    vp.add(table);
    vp.add(pager);

    // Add it to the root panel.
    RootPanel.get().add(vp);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-11-24 16:50:06

自 1.6 起,监听器在 GWT 中已被弃用,您将不得不使用处理程序。你看到 addFocusHandler 了吗?

来自 gwt 网站 - http://code.google.com/webtoolkit/doc/ 1.6/ReleaseNotes_1_6.html

添加了事件处理程序来替换小部件、历史记录和各种其他类使用的旧事件侦听器。

ps - 另请参阅 http://gwt.google.com/samples/Showcase /Showcase.html#!CwCellTable,我认为这就是您想要实现的目标。

Listeners are deprecated in GWT since 1.6 you will have to use handlers instead. Do you see a addFocusHandler?

From the gwt site - http://code.google.com/webtoolkit/doc/1.6/ReleaseNotes_1_6.html

Event handlers have been added to replace the old event listeners used by Widgets, History, and various other classes.

ps - Also look at http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTable, I think that is what you are trying to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文