Table borders in j2html

406 Views Asked by At

I'm trying to use j2html to produce simple tables for displaying in Swing's TextPanes.

I want borders on the table, but I can't figure out how to add the border=1 attribute to the <table> tag (as far as I know, css styles don't work in TextPane).

attrs("border=1") throws an exception, and withData("border", "1") does nothing.

      return body(
            table(
                tbody(
                        each(this.DATA, (k,v) -> tr(
                                td(k.toString()), 
                                td(rawHtml(v.toLink()))
            ))))).toString();
1

There are 1 best solutions below

0
On

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.