如何在Vaadin中使表格可编辑

时间:2016-05-06 16:27:54

标签: java vaadin vaadin7

我有一个连接到SQL数据库并输出到表的Vaadin项目。我添加了“sqlGrid.setEditorEnabled(true);”并且无法编辑表格。如果我单击一个单元格或按Enter键它只是关闭并且不允许我输入数据。这是我的代码:

package com.example.testyui;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.servlet.annotation.WebServlet;

import com.evolt.data.connection.ConnectionHelper;
import com.txfb.bai.jtds.jdbc.JtdsConnection;
import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.util.sqlcontainer.SQLContainer;
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.query.FreeformQuery;
import com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate;
import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.renderers.NumberRenderer;
import com.vaadin.ui.renderers.ProgressBarRenderer;

//creates new layout and adds a grid to it
@SuppressWarnings("serial")
@Theme("valo")
@Push()
public class TestyUI extends UI {

    //set connection pool variable
    SimpleJDBCConnectionPool connectionPool;
    SQLContainer container;

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = TestyUI.class)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        //calls method from ConnectionHelper
        connectionPool = new ConnectionHelper().getConnectionPool();

        // declare new vertical layout
        final VerticalLayout layout = new VerticalLayout();



        // Declare grid variable
//      Grid grid = new Grid("Data Grid");

        //Sql Grid
        Grid sqlGrid = new Grid("Users from TXS9316135\\M12QA");
        layout.addComponent(sqlGrid); 
        sqlGrid.isImmediate();
        sqlGrid.isEnabled();
        sqlGrid.setVisible(true);
        sqlGrid.setSizeFull();
        sqlGrid.setEditorEnabled(true);
        sqlGrid.isEditorActive();



        // add layout to UI; add grid to layout;
        //layout.addComponent(grid);

        layout.setExpandRatio(sqlGrid, 1);
        layout.setSizeFull();


        //Static Populate
//      try {
//          ResultSet rs = ConnectionHelper.getConnection().createStatement().executeQuery("SELECT * FROM EVOLT.DBO.EVOLTTESTGROUPS;");
//      
//          ResultSetMetaData md = rs.getMetaData();
//          
//          for (int i = 1; i <= md.getColumnCount(); i++) {
//              sqlGrid.addColumn(md.getColumnName(i));
//          }
//          
//          while (rs.next()) {
//              String[] values = new String[md.getColumnCount()];
//              for (int i = 1; i <= md.getColumnCount(); i++) {
//                  values[i - 1] = rs.getString(i);
//              }
//              sqlGrid.addRow(values);
//          }
//      } catch (SQLException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }

        //SqlContainer Populate
        TableQuery tq = new TableQuery("ABC", "dbo", "ABCUSERCOM", connectionPool, new MSSQLGenerator());
//      FreeformQuery ff = new FreeformQuery("SELECT * FROM ABC.DBO.ABCUSERCOM;", connectionPool);
//      tq.setVersionColumn("UPDATETIMESTAMP");
        try {
            container = new SQLContainer(tq);
            sqlGrid.setContainerDataSource(container);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // adds layout and therefore grid to ui/layout
        this.setContent(layout);

        // TableQuery tq = new TableQuery("EVOLT", "dbo", "EVOLTTESTGROUPS",
        // connHelper.getConnectionPool(), new MSSQLGenerator());
        // tq.setVersionColumn("OPTLOCK");
        //
        // SQLContainer container = new SQLContainer(tq);

        ScheduledFuture<?> future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
            public void run() {
                access(new Runnable() {
                    public void run() {
                        container.refresh();
                    }
                });
            }
        }, 0, 500, TimeUnit.MILLISECONDS);

    }
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我已经使用MySQL数据库测试了您的代码,它允许编辑单元格。 这是我使用的init方法。 我唯一改变的是连接池创建。 我认为问题与您的连接池有关。 希望它有所帮助。

tableView:cellForRowAtIndexPath:

答案 1 :(得分:0)

原来,scheduleAtFixedRate(0)正在汇集以快速刷新网格(500毫秒),只需很少的时间来更新字段。

    ScheduledFuture<?> future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
        public void run() {
            access(new Runnable() {
                public void run() {
                    container.refresh();
                }
            });
        }
    }, 0, 50000, TimeUnit.MILLISECONDS);