JTable中的图像的HTML问题 [英] html issues with images in JTable

查看:176
本文介绍了JTable中的图像的HTML问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展了AbstractTableModel来创建一个自定义的TableModel。这样做的原因是将一个HashMap绑定到一个JTable中。

在我的TableModel中,在其中一行中,我返回了如下所示的html代码:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {$
case 0:
String sTest =< div style = \margin-left:100px; \>< img src ='+ new ImageIcon(Wds.class.getResource (/resources/video.png\")++'< / div>;
返回< html> + sTest + sTest +hello+< / html>;

默认值:
throw new IndexOutOfBoundsException();




$ b $ p
$ b现在的问题是HTML支持Java似乎很糟糕。



我需要使用margin-left,margin-top等来控制图片。问题是如果我使用< div样式>它会导致换行,所以之后的所有内容都会在下面一行。如果使用< span style>,则不会导致换行符,但页边距不能与< span style> ;(它应该);

我也尝试创建自定义的TableCellRenderer并在其中添加.css值并使用< div class>但换行问题仍然存在。 in < div style>消除换行和 margin-left 通常和< span style>一起工作,但是看起来Java对HTML的支持相当差。

有没有人对我如何解决这个问题有什么建议?

以下是完整的SSCCE代码:

 私人地图<串GT; list = new LinkedHashMap< String,String>(); 

class MyTableModel extends AbstractTableModel {

private String [] columnNames = {Column1};

public void addElement(String sElement,String sElement2){

list.put(sElement,sElement2);
fireTableRowsInserted(list.size(),list.size());


$ b @Override
public int getColumnCount(){
return columnNames.length;


@Override
public int getRowCount(){
return list.size();
}

@Override
public String getColumnName(int col){
return columnNames [col];

$ b @Override
public Object getValueAt(int rowIndex,int columnIndex){

switch(columnIndex){
case 1:
String sTest =< div style = \margin-left:100px; \>< img src ='+ new ImageIcon(Wds.class.getResource(/ resources / video。 png))+'< / div>;
返回< html> + sTest + sTest +hello+< / html>;

默认值:
throw new IndexOutOfBoundsException();




$ div class =h2_lin>解决方案

您应该在自定义

a> TableCellRenderer 例子。这个例子显示了一个 ListCellRenderer ,但是原理是一样的。



附录:我需要添加
$ b TableCellRenderer
的实现可以返回任何想要的组件 -even a JPanel 具有自己的布局,比如 StatusPanel 显示此处。您可能还需要自定义 TableCellEditor


I have extendeded AbstractTableModel to create a custom TableModel. The reason for doing this is to bind a hashmap to a JTable.

Within my TableModel, on one of the rows I am returning html code like this:

@Override
        public Object getValueAt(int rowIndex, int columnIndex) {

             switch (columnIndex) {
             case 0:    
                    String sTest = "<div style=\"margin-left:100px;\"><img src='" + new ImageIcon(Wds.class.getResource("/resources/video.png"))+ "'</div>";
                return "<html>" + sTest + sTest + "hello" + "</html>";              

             default:
                    throw new IndexOutOfBoundsException();
             }    
        }

The problem I have now is that the html support in Java seems to be pretty bad.

I need to control the images using "margin-left, margin-top" etc. The issue is that if I use "<div style>" it will lead to a linebreak, so everything afterwards will be one row below. If I use "<span style>" it doesn't lead to a linebreak, but margin doesn't work with "<span style>" (which it should);

I have also tried creating custom TableCellRenderer and add the .css values there and using "<div class>" but the issue with the linebreak remains.

Usually "display:inline" in "<div style>" eliminates the line-break and margin-left usually works with "<span style>", but it seems Java has pretty poor HTML support.

Does anybody have any suggestions on how I can solve this?

Here is full SSCCE code:

private Map<String, String> list = new LinkedHashMap<String,String>();

    class MyTableModel extends AbstractTableModel {

        private String[] columnNames = {"Column1"}; 

        public void addElement(String sElement, String sElement2) {         

            list.put(sElement, sElement2);
            fireTableRowsInserted(list.size(), list.size());

        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public int getRowCount() {
            return list.size();
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {

             switch (columnIndex) {
             case 1:    
                    String sTest = "<div style=\"margin-left:100px;\"><img src='" + new ImageIcon(Wds.class.getResource("/resources/video.png"))+ "'</div>";
                    return "<html>" + sTest + sTest + "hello" + "</html>";

             default:
                    throw new IndexOutOfBoundsException();
             }    
        }
    }

解决方案

You should handle the alignment in a custom TableCellRenderer, for example. The example shows a ListCellRenderer, but the principle is the same.

Addendum: I need to add several icons and position them differently within the cell.

Your implementation of TableCellRenderer can return any desired Component—even a JPanel with its own layout, such as the StatusPanel shown here. You may also need a custom TableCellEditor.

这篇关于JTable中的图像的HTML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆