所以我认为这可能与StreamSource有关,但我无法完全找出问题所在。本质上,我有一个Vaadin网格,可以从SQL数据库中检索其数据。 “图像”列包含要上传的图像的BLOB值(成功的方法)。
我正在尝试做的是允许用户单击表中的按钮,并在popupview中显示该图像。
我的ViewExpenses类:
aGrid.addColumn(reciept -> "Receipt", new ButtonRenderer<>(clickEvent ->{
new ButtonRenderer<>(clickEvent ->{
Window window = new Window();
window.setModal(true);
window.addCloseShortcut(ShortcutAction.KeyCode.ESCAPE, null);
UI.getCurrent().addWindow(window);
Expenses anExpense = aGrid.getSelectionModel().getSelectedItems().stream().findFirst().get();
long anID = anExpense.getId();
System.out.println(anID);
StreamResource.StreamSource streamSource = (StreamResource.StreamSource)aController.getImage(anID);
System.out.println("4");
StreamResource streamResource = new StreamResource(streamSource,"");
Embedded embedded = new Embedded("",streamResource);
System.out.println("5");
Image anImage = new Image("Reciept", streamResource);
window.setContent(anImage);
anImage.setSizeFull();
window.setSizeFull();
})
);
我的后端DbController类方法用于获取图像:
public InputStream getImage(long anIndex){
InputStream binaryStream = null;
try{Statement stmt = null;
int intIndex = (int) anIndex;
//connect to database
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:mem:Users", "sa", "");
System.out.println("Connected database successfully...");
//insert data
System.out.println("Getting expenses from the database...");
stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * FROM EXPENSES");
Blob imageBlob = resultSet.getBlob(intIndex);
binaryStream = imageBlob.getBinaryStream(0, imageBlob.length());
}
catch (Exception e){
}
return binaryStream;
}
我已经为此抓了一段时间了,感谢您的帮助。
最佳答案
您永远不会显示PopupView
。然后,单击该按钮时,侦听器将初始化一个新的PopupView
,但不执行任何操作。
您可以:
单击按钮时,打开一个模式窗口:
Window window = new Window();
window.setModal(true);
window.addCloseShortcut(KeyCode.ESCAPE, null);
UI.getCurrent().addWindow(window);
Image anImage = ...
window.setContent(anImage);
anImage.setSizeFull();
window.setSizeFull();
使用呈现
ComponentRenderer
的PopupView
。grid.addColumn(x->new PopupView(...))
.setRenderer(new ComponentRenderer())
(请注意,使用组件渲染器会影响性能,而按钮渲染器会更亮)。