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