Reputation: 3956
I want to show only top borders of a html table in JTextPane. Below code works fine in Java 1.7 but in Java 1.6 border does not appear. Is there a way to do it in Java 1.6?
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextPane;
public class textpanedemo{
public static void main(String[] args) {
String html = "<html><table><tr style=\"border-top:1px solid red\"><td>asd</td></tr></table></html>";
JTextPane jPane = new JTextPane();
jPane.setContentType("text/html");
jPane.setText(html);
JFrame frame = new JFrame("HtmlDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(jPane);
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 2
Views: 998
Reputation: 4755
Java 1.6xxx only supports HTML 3.2 and lower, and therefore table borders do not work in Java 1.6xxx.
I have created a small workaround that I hope will work for you:
String html = "<html><table><tr style=\"background-color:red;\"><td style=\"background-color:white;margin-top:1px\">asd</td></tr></table></html>";
Basically, I set the background of <tr>
to red, then set the background of <td>
to white, and gave it a bit of a margin to show some of the redness from the <tr>
. Unfortunately, the margin seems to show 1px of the red background on the bottom too, which I tried to fix but was unable to.
Here is the result running in Java 1.6:
I have also created a second workaround which is a bit more hacky than the last one, and shows only the top border:
String html = "<html><table style=\"background-color:red;padding-top:1px;\"><tr style=\"background-color:white;\"><td>asd</td></tr></table></html>";
The con of this workaround is that instead of creating new <tr>
elements to create new rows, you need to create new <table>
elements (with the same style) to create new rows. Here is an example of using two tables to create two rows, using this HTML:
String html = "<html><table style=\"background-color:red;padding-top:1px;\"><tr style=\"background-color:white;\"><td>asd</td></tr></table>"
+ "<table style=\"background-color:red;padding-top:1px;\"><tr style=\"background-color:white;\"><td>asd</td></tr></table></html>";
(Notice how there are two tables in that html, for two rows.)
Take your pick. They are both a bit hacky, but it's what you have to do to support old Java versions, I guess. :/
Upvotes: 1