【问题标题】:PopupView from Vaadin Grid ButtonRender来自 Vaadin Grid ButtonRender 的 PopupView
【发布时间】:2018-07-10 20:31:34
【问题描述】:

所以我认为这可能与 StreamSource 有关,但我无法完全找到问题所在。本质上,我有一个从 SQL 数据库中检索数据的 Vaadin Grid。 “图像”列保存上传的图像的 BLOB 值(成功方法)。

我想要做的是允许用户单击表格中的按钮并在弹出视图中显示该图像。

我的 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;
    }

我一直在为这个问题摸不着头脑,如果有任何帮助,我将不胜感激。

【问题讨论】:

    标签: java image blob vaadin


    【解决方案1】:

    您永远不会显示PopupView。然后,当单击按钮时,侦听器会初始化一个新的PopupView,但不会对其执行任何操作。

    您可以:

    1. 点击按钮时打开一个模态窗口:

      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();
      
    2. 使用呈现PopupViewComponentRenderer

      grid.addColumn(x->new PopupView(...))
        .setRenderer(new ComponentRenderer())
      

    (注意使用组件渲染器会影响性能,按钮渲染器更轻)。

    【讨论】:

    • 很好,所以我选择了第一个选项,100% 没有意识到你可以用 vaadin 做到这一点。我已经修改了上面的代码以反映。但是,我现在从 handleConnectorRequest 类中得到一个空指针异常。我怀疑这是来自我加载图像的方式。有什么想法吗?
    • 也许...如果没有堆栈跟踪,很难判断它在哪里失败,顺便说一句,这会导致另一个问题,因为您的代码已经更改,现在出现了另一个问题。
    • 与图像未显示相同的问题 ;) 因此是 DBController 代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多