0

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();
             }    
        }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Omid
  • 823
  • 1
  • 11
  • 31

1 Answers1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I can't do it like that, since I need to add several icons and position them differently within the cell. – Omid Apr 25 '13 at 14:34
  • If the HTML support in `DefaultTableCellRenderer` is inadequate, I don't see another choice; more above. – trashgod Apr 25 '13 at 17:01