java - HTML Table formatting using css not working in JTextPane -
i trying implement view html pane specific project. using jtextpane render html using content type "text/html". having tables in input html in order give these tables border, thought of using css styling, unfortunately did not work out.
if put border property part of table works not css styling.
here sample code created recreate issue. content1 not creates border table content2 creates it. want use content1 approach have plenty of html files tables. time, appreciated.
import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextpane; public class testtextpane { private static int x = 200; private static int y = 200; private static int w = 600; private static int h = 400; public static final string content1 = "<html>\r\n" + " <head>\r\n" + " <style type=\"text/css\">\r\n" + " <!--\r\n" + " table,th, td { border: 1px solid black }\r\n" + " body, p { font-family: courier; font-size: 14 }\r\n" + " -->\r\n" + " </style>\r\n" + " \r\n" + " </head>\r\n" + " <body>\r\n" + " <div align=\"left\">\r\n" + " <b>q: difference between , post method? </b>\r\n" + " </div>\r\n" + " <p>\r\n" + " a:\r\n" + " </p>\r\n" + " <table>\r\n" + " <tr>\r\n" + " <th width=\"50%\">\r\n" + " get\r\n" + " </th>\r\n" + " <th>\r\n" + " post\r\n" + " </th>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>\r\n" + " safe method (idempotent)\r\n" + " </td>\r\n" + " <td>\r\n" + " post non-idempotent method\r\n" + " </td>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>\r\n" + " can send limited data method , it’s sent in header \r\n" + " request url\r\n" + " </td>\r\n" + " <td>\r\n" + " can send large amount of data post because it’s part of \r\n" + " body.\r\n" + " </td>\r\n" + " </tr>\r\n" + " </table>\r\n" + " <br>\r\n" + " <br>\r\n" + " \r\n" + " </body>\r\n" + "</html>"; public static final string content2 = "<html>\r\n" + " <head>\r\n" + " <style type=\"text/css\">\r\n" + " <!--\r\n" + " body, p { font-family: courier; font-size: 14 }\r\n" + " -->\r\n" + " </style>\r\n" + " \r\n" + " </head>\r\n" + " <body>\r\n" + " <div align=\"left\">\r\n" + " <b>q: difference between , post method? </b>\r\n" + " </div>\r\n" + " <p>\r\n" + " a:\r\n" + " </p>\r\n" + " <table border=1>\r\n" + " <tr>\r\n" + " <th width=\"50%\">\r\n" + " get\r\n" + " </th>\r\n" + " <th>\r\n" + " post\r\n" + " </th>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>\r\n" + " safe method (idempotent)\r\n" + " </td>\r\n" + " <td>\r\n" + " post non-idempotent method\r\n" + " </td>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>\r\n" + " can send limited data method , it’s sent in header \r\n" + " request url\r\n" + " </td>\r\n" + " <td>\r\n" + " can send large amount of data post because it’s part of \r\n" + " body.\r\n" + " </td>\r\n" + " </tr>\r\n" + " </table>\r\n" + " <br>\r\n" + " <br>\r\n" + " \r\n" + " </body>\r\n" + "</html>"; /** * @param args */ public static void main(string[] args) { jframe frame = new jframe(); frame.setbounds(x, y, w, h); jtextpane pane = new jtextpane(); pane.setcontenttype("text/html"); pane.seteditable(false); jscrollpane scrollpane = new jscrollpane(pane); scrollpane.setbounds(x,y,w,h); frame.getcontentpane().add(scrollpane); pane.settext(content2); // change content here frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }
i removed comment notation style , added units size of font.

import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextpane; public class testtextpane { private static int x = 200; private static int y = 200; private static int w = 600; private static int h = 400; public static final string content1 = "<html>\r\n" + " <head>\r\n" + " <style type=\"text/css\">\r\n" + " table, th, td { border: 2px solid #ff0000; }\r\n" + // sure add unit font size, otherwise invalid " body, p { font-family: courier; font-size: 14px; }\r\n" + " </style>\r\n" + " \r\n" + " </head>\r\n" + " <body>\r\n" + " <div align=\"left\">\r\n" + " <b>q: difference between , post method? </b>\r\n" + " </div>\r\n" + " <p>\r\n" + " a:\r\n" + " </p>\r\n" + " <table>\r\n" + " <tr>\r\n" + " <th width=\"50%\">\r\n" + " get\r\n" + " </th>\r\n" + " <th>\r\n" + " post\r\n" + " </th>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>\r\n" + " safe method (idempotent)\r\n" + " </td>\r\n" + " <td>\r\n" + " post non-idempotent method\r\n" + " </td>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>\r\n" + " can send limited data method , it’s sent in header \r\n" + " request url\r\n" + " </td>\r\n" + " <td>\r\n" + " can send large amount of data post because it’s part of \r\n" + " body.\r\n" + " </td>\r\n" + " </tr>\r\n" + " </table>\r\n" + " <br>\r\n" + " <br>\r\n" + " \r\n" + " </body>\r\n" + "</html>"; public static void main(string[] args) { jframe frame = new jframe(); frame.setbounds(x, y, w, h); jtextpane pane = new jtextpane(); pane.setcontenttype("text/html"); pane.seteditable(false); jscrollpane scrollpane = new jscrollpane(pane); scrollpane.setbounds(x,y,w,h); frame.getcontentpane().add(scrollpane); pane.settext(content1); // change content here frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }
Comments
Post a Comment