One way of doing this is by adding attr("border", "1")
to the table, like so:
import static j2html.TagCreator.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
public class Main {
private static void createAndShowGUI() {
final JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setEditable(false);
pane.setText(html(
body(
table(
tr(
th("Header 1"),
th("Header 2")
),
tr(
td("Data 1"),
td("Data 2")
)
).attr("border", "1")
)
).render());
final JFrame frame = new JFrame("TextPane HTML table border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(pane));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(Main::createAndShowGUI);
}
}
I haven't tried CSS on the JTextPane
though.
Another way of doing this (which needs a bit more code, but has prettier result) is:
import static j2html.TagCreator.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
public class Main2 {
private static void createAndShowGUI() {
final JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setEditable(false);
pane.setText(html(
body(
table(
tr(
th("Header 1").attr("bgcolor", "#ffffff"),
th("Header 2").attr("bgcolor", "#ffffff")
),
tr(
td("Data 1").attr("bgcolor", "#ffffff"),
td("Data 2").attr("bgcolor", "#ffffff")
)
).attr("cellspacing", "3").attr("bgcolor", "#000000")
)
).render());
final JFrame frame = new JFrame("TextPane HTML table border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(pane));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(Main2::createAndShowGUI);
}
}
Both cases are not using CSS.