Java jTable添加带有base64图像的html img标签

时间:2019-01-31 15:01:04

标签: java base64 jtable

Java和此JTable的新功能。我想将图像添加到列中的单元格之一。我可以添加图片,但不显示图片,而只是显示损坏的图片链接的图片:

enter image description here

这是我用来填充这些图像的代码:

@SuppressWarnings("unused")
public void createAvatar(String b64image, String name, int row, int col) {      
    String html = String.format("<html><table><td><img alt=\"Avatar\" style=\"position: absolute; top: 0px; left: 0px;\" src=\"data:image/png;base64,%s\"/>%s</td></table></html>", b64image, name);
    table.setValueAt(html, row, col);
}
private void initialize() throws Exception {
...MORE CODE HERE...
try {
    for (int i = 0; i < b64s.size(); i++) {
       createAvatar(b64s.get(i), names.get(i), i, 8);
    }
} catch(Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

table = new JTable(model) {
   @Override
   public boolean isCellEditable (int row, int col)
   {
        return true;
   }

   public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        return null;
   }

   public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);

        if (c instanceof JComponent) {
             if(column == 4){
                JComponent jc = (JComponent) c;
                jc.setToolTipText(getValueAt(row, column).toString());
             }
        }

        return c;
   }
};

table.getModel().addTableModelListener(new TableModelListener() {
   @SuppressWarnings("static-access")
   public void tableChanged(TableModelEvent e) {
   ...MORE CODE HERE...
});

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

for (int i = 0; i < b64s.size(); i++) {
   createAvatar(b64s.get(i), names.get(i), i, 8);
}
}

如果我使用这样的硬编码驱动器路径:

@SuppressWarnings("unused")
public void createAvatar(String b64image, String name, int row, int col) {      
     String html = String.format("<html><table><td><img alt=\"Avatar\" style=\"position: absolute; top: 0px; left: 0px;\" src=\"%s\"/>%s</td></table></html>", "file:///C:/Users/xxxxxx/eclipse-workspacePhoton_xxxxx/xxxxx/resources/avatarB.png", name);
     table.setValueAt(html, row, col);
}

效果很好:

enter image description here

当我执行相同的操作但使用硬编码的base64字符串时:

String html = String.format("<html><table><td><img alt=\"Avatar\" style=\"position: absolute; top: 0px; left: 0px;\" src=\"data:image/png;base64,%s\"/>%s</td></table></html>", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUA....aZ9htT6WuJRFzV3epJcukNncYXzbPKIUjXzcWmUe+K7lQ3q/tn0/Gbk89e3sSussuZpu2D1Wj0P0mjaW2u8Si/mHPSBJpmUUM8x9EVbrT4j3P9QAAAABJRU5ErkJggg==", name);

它与非硬编码的字符串显示相同:

enter image description here

然后从代码中复制HTML变量:

<html><table><td><img alt="Avatar" style="position: absolute; top: 0px; left: 0px;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcE.....ussuZpu2D1Wj0P0mjaW2u8Si/mHPSBJpmUUM8x9EVbrT4j3P9QAAAABJRU5ErkJggg=="/>Bob</td></table></html>

<html><table><td><img alt="Avatar" style="position: absolute; top: 0px; left: 0px;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMjHxIGmVAAAEFUlEQVRIS82VYUxbVRTH33AbZoiZzk03BkQQp9Mw55yS6eIcg+BAqlLSOUzYCBoGGThHtuiHdTi3jKlBZWqUmAnrJjSUwVpZQAeidHTaQul7UGhf11H62s6q0Q9Dk2U73tve9/bu+kLIPvlP/mnSc87/13tPXx7zv5JVEBaNOv2rOE/waYcnlMn5fPdqtdo4Ur592e3BhDE+UMHxgpnlA/9yfABEs27hOvr0cG7hGOu6kk5G5i4AmOdwBV7n3IFQNFSAYZaHxve18E6ZBt6rfgPaWlr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pu2D1Wj0P0mjaW2u8Si/mHPSBJpmUUM8x9EVbrT4j3P9QAAAABJRU5ErkJggg=="/>Bob</td></table></html>

显示正常(表示base64图像正确且有效)

所以我不能在jTable的HTML内使用Base64版本吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试直接使用swing组件,而不是使用html。从您的问题来看,您似乎有一个代表用户头像的列,因此,假设数据的类型为Avatar

然后,您可以为该列类型设置自定义单元格渲染器,并使该渲染器返回JLabel。该标签既可以包含文本也可以包含图标,因此代码看起来像这样(简化):

JTable table = ...;

table.setDefaultRenderer( Avatar.class, new TableCellRenderer() {      
  @Override
  public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, 
                                 boolean hasFocus, int row, int column )
  {
    Avatar avatar = (Avatar)value;

    //you might want to think about caching the icon and maybe even the label.
    Icon icon = new ImageIcon(Base64.decode( avatar.getBase64Image() ));
    return new JLabel(avatar.getName(), icon, JLabel.LEFT);
  }
} );