html issues with images in JTable

我们两清 提交于 2019-12-25 03:37:00

问题


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();
             }    
        }
    }

回答1:


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.



来源:https://stackoverflow.com/questions/16209322/html-issues-with-images-in-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!