[英]How to make table editable in Vaadin
I've got a Vaadin project that connects to a SQL database and outputs to a table. 我有一个Vaadin项目,该项目连接到SQL数据库并输出到表。 I've added "sqlGrid.setEditorEnabled(true);"
我添加了“ sqlGrid.setEditorEnabled(true);” and the table is not able to be edited.
并且表格无法编辑。 If I click in a cell or hit Enter it just closes out and does not allow me to enter data.
如果我单击一个单元格或按Enter键,它将关闭,并且不允许我输入数据。 Here is my code:
这是我的代码:
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);
}
}
Thanks for your help. 谢谢你的帮助。
I have tested your code with a MySQL database and it allows to edit cells. 我已经使用MySQL数据库测试了您的代码,它允许编辑单元格。 This is the init method that I have used.
这是我使用的初始化方法。 The only thing that I changed too is Connection pool creation.
我唯一更改的也是连接池的创建。 I think problem is related with your Connection pool.
我认为问题与您的连接池有关。 Hope it helps.
希望能帮助到你。
protected void init(VaadinRequest request) {
//calls method from ConnectionHelper
JDBCConnectionPool pool =null;
try {
pool = new SimpleJDBCConnectionPool(
"com.mysql.jdbc.Driver",
"jdbc:mysql://127.0.0.1:3306/dbname?autoReconnect=true", "user", "pwd", 2, 5);
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
// 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("tablename", pool);
// 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() {
@Override
public void run() {
access(new Runnable() {
@Override
public void run() {
container.refresh();
}
});
}
}, 0, 500, TimeUnit.MILLISECONDS);
}
Turned out that scheduleAtFixedRate(0) was pooling to refresh the grid too fast (500 milliseconds) allowing very little time to make an update to a field. 事实证明,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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.