Unfortunately, Swing supports HTML 3.2. What I am trying to achieve is this:
But I am unable too. No matter what I do there is no alignment between the text (sometext
) and the image.
I have tried all combinations of: align='middle'
align='top'
align='bottom'
inside <td>
tag and inside <img>
tag. I have tried with valign
as well.
<html>
<body>
<table border="1" >
<tr>
<td style='width:50%'>
<img src="https://www.w3schools.com/images/stickman.gif" align='middle' width="24" height="24">
sometext
</td>
<td style="width:50%">something</td>
</tr>
</table>
</body>
</html>
What I get are things like:
Is there a way to achieve it?
The Java example in case you want to test if it works in Swing:
public class HtmlExample {
public static void main(String[] args) {
//@formatter:off
SwingUtilities.invokeLater(() -> {
String prefix = "<html><body><table border='1'><tr>";
String suffix = "</tr></table><body><html>";
StringBuilder sb = new StringBuilder(prefix);
sb.append("<td style='width:50%'>");
sb.append("<img src=\"https://www.w3schools.com/images/stickman.gif\" align='top' width=\"24\" height=\"24\">");
sb.append("sometext");
sb.append("</td>");
sb.append("<td style='width:50%'>");
sb.append("something");
sb.append("</td>");
sb.append(suffix);
JLabel label = new JLabel(sb.toString());
JOptionPane.showMessageDialog(null, label);
});
}
}
I have tried a lot of things mentioned in HTML 3.2 Reference Specification but none of them give me what I want.
If I do this:
<html>
<body>
<table border="1" >
<tr style=''>
<td style='width:50%;'>
<div>
<img style="vertical-align:middle" src="https://www.w3schools.com/images/stickman.gif" width="24" height="24">
<span>Works.</span>
</div>
</td>
<td style="width:50%">
<div>
<span>Works.</span>
</div>
</td>
</tr>
</table>
</body>
</html>
I come close to it. But this will not work in HTML 3.2
EDIT: (After @Frakcool made a point of using a JTable
)
The reason I do not use a JTable
is that I use the text for a tooltip. As mentioned in this answer.