I’m trying to put an html table in an editable JTextPane. So you can edit tables in it like an excel sheet. When I try to do it, these weird col and colgroup boxes keep showing up. Also, the table doesn’t seem to work like they’re supposed to..
Here’s an example:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class htmlEditor2 extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
htmlEditor2 frame = new htmlEditor2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public htmlEditor2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
Foo f = new Foo();
f.setText("<html><body><table border=\"1\" width=\"985\" cellpadding=\"3\" cellspacing=\"0\" style=\"table-layout: fixed; border-collapse: collapse; border-width: 0px; border-color: #010101; \"><colgroup><col width=\"328\"></col> <col width=\"328\"></col> <col width=\"328\"></col> </colgroup><tr><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><font face=\"Arial\"><span style=\"font-size:8pt\">row 1</span></font></div></td><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><font face=\"Arial\"><span style=\"font-size:8pt\">row2</span></font></div></td><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><font face=\"Arial\"><span style=\"font-size:8pt\">row3</span></font></div></td></tr><tr><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><span style=\"font-size: 8pt;\"> </span></div></td><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><span style=\"font-size: 8pt;\"> </span></div></td><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><span style=\"font-size: 8pt;\"> </span></div></td></tr><tr><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><span style=\"font-size: 8pt;\"> </span></div></td><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><span style=\"font-size: 8pt;\"> </span></div></td><td align=\"left\" valign=\"top\" width=\"321\" style=\"border: solid #010101 1px; \"><div align=\"left\"><span style=\"font-size: 8pt;\"> </span></div></td></tr></table><div align=\"left\"> </div></body></html>");
contentPane.add(f);
}
class Foo extends JTextPane {
public Foo() {
super();
HTMLEditorKit kit = new HTMLEditorKit();
setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule(""); //in case I need to add a CSS
Document doc = kit.createDefaultDocument();
setDocument(doc);
}
}
}
I would paste a nicely formatted version of the html, but I’m not sure how to do it on here…
So yeah.. I just want to know how to get rid of those weird colgroup and col boxes in my table and how to make the table work normally!
UPDATE: So it turns out that the service that I’m retrieving the html tables from autoformats the html table to include the <colgroup> and <col> tags. So I have some follow up questions: Does JTextPane support current html versions? And of course, How do I get the JTextPane to not display those weird boxes?
My solution was to just use a String.replaceAll() and removed everything I didn’t want. I know it seems weird.. buuut. for now. That’s my solution. Thanks Reimeus for your answer. I will have to look into your solution when I have a chance.