Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JTextPane not printing components inside it

  1. #1
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    My Mood
    Thanked 25 Times in 25 Posts

    Exclamation JTextPane not printing components inside it

    As I couldn't find a way to do the printing of multiple items on one page, I decided to test out my idea. I found that I could have a table in a JTextPane.

    However, whenever I got to print, my table will vanish. It doesn't even need to go to the printer. Just bringing up the print dialog will make it vanish like tax money in D.C.

    (Also, I'm not really that familiar with JTextPane and it seems harder than JTextArea. Which part is controlling the font of my regular text by the way? I think it might be tableParagraph but it could also be the base font. I can try and fiddle with both and waste paper though if you can guess where the font is being set, please tell me.

    Setting the font with setFont() will vanish my table. )

    Somehow, I'm thinking that the solution might be JTextPane.setContentType() or setEditorKit() or both. (I"m not that familiar with either, but I fear that it's only being set to handle text, hence any graphics are being lost.)

    Here is my code:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.text.*;
    public class AddingTables {
        JTextPane textPane;
        private Object[][] rows;
        private Object[] colNames;
        public AddingTables(Object[][] rows, Object[] colNames) {
       // String[] text = {"This is the Church Visitation Thingy \n" , " \n", "Mike"};
            String[] text = {
                "\n" + "\n" + "\n" + "The Heading  \n",           //  0 - 89
                " \n",                                             // 90 - 91
                ""  // 92 - 321
            this.rows = rows;
            this.colNames = colNames;
            JTextField jtf = new JTextField();
            jtf.setText("April 15th, 2010");
            JTextArea jta = new JTextArea();
            jta.setText("We all need to fight Barack Obama.  He cannot win. " + 
             "Obama is our enemy.  He hates America.  He is a traitor.");
            textPane = new JTextPane();
            StyledDocument doc = textPane.getStyledDocument();
            createStyles(doc, rows, colNames);
            setContent(doc, text);
             StyledDocument document = (StyledDocument) textPane.getDocument();
            JTextField jtf2 = new JTextField("March 15th, 2014");
             JTextArea area = new JTextArea("We fought Barack Obama today.  He was beat up.");
             String lineString = "_________________________________________________________________________________________";
             JTextField jtf3 = new JTextField("April 7th, 2014");
             JTextArea jta2 = new JTextArea("Paul Adcock, the guy who will help save America, had his 25th Birthday today. \n" + "There was a big party.  We had lots of fun.");
             JTextField jtf4 = new JTextField("July 4th, 2014");
             JTextArea jta3 = new JTextArea("Barry tried to ruin our Independence Day today by using his pen and phone to push Amnesty. \n" + 
             "However, he didn't count on Texas and others refusing to implement it.  Also, we had a big protest at the border. \n" +
             "Barry woulda no doubt sent in his drones but the state militias came to our aide.  We've started the Convention of the States movement in earnest!");
             JTextField jtf5 = new JTextField("November 4th, 2014");
             JTextArea jta4 = new JTextArea("Today, we took the Senate.  Also, we got a lot of Tea Party guys in to replace the RINOs.");
           String newStuff = lineString + "\n" + jtf.getText() + "\n" + jta.getText() + "\n" + lineString + "\n" + jtf2.getText() + "\n" + area.getText() + "\n" + lineString + "\n" + jtf3.getText() + "\n" +
           jta2.getText() + "\n" + lineString + "\n" + jtf4.getText() + "\n" + jta3.getText() + "\n" + lineString + "\n" + jtf5.getText() + "\n" + jta4.getText() + "\n" + lineString + "\n";
           document.insertString(document.getLength(), newStuff, null);
           catch(BadLocationException ble)
           public JTextPane getTextPane()
           return textPane;
        private void createStyles(StyledDocument doc, Object[][] rows, Object[] colNames) {
            Style baseStyle = doc.addStyle("base", null);
            StyleConstants.setFontFamily(baseStyle, "Lucida Sans Unicode");
            StyleConstants.setFontSize(baseStyle, 18);
            StyleConstants.setFirstLineIndent(baseStyle, 20f);
            StyleConstants.setLeftIndent(baseStyle, 10f);
            Style style = doc.addStyle("bold", baseStyle);
            StyleConstants.setBold(style, true);
            style = doc.addStyle("italic", baseStyle);
            StyleConstants.setItalic(style, true);
            style = doc.addStyle("blue", baseStyle);
            StyleConstants.setForeground(style, Color.blue);
            style = doc.addStyle("underline", baseStyle);
            StyleConstants.setUnderline(style, true);
            style = doc.addStyle("green", baseStyle);
            StyleConstants.setForeground(style, Color.green.darker());
            StyleConstants.setUnderline(style, true);
            style = doc.addStyle("highlight", baseStyle);
            StyleConstants.setForeground(style, Color.yellow);
            StyleConstants.setBackground(style, Color.black);
            style = doc.addStyle("table", null);
            StyleConstants.setComponent(style, getTableComponent(rows, colNames));
            style = doc.addStyle("tableParagraph", null);
            StyleConstants.setLeftIndent(style, 12f);
            StyleConstants.setRightIndent(style, 12f);
            StyleConstants.setSpaceAbove(style, 15f);
            StyleConstants.setSpaceBelow(style, 15f);
        private void setContent(StyledDocument doc, String[] text) {
            try {
                doc.insertString(0,               text[0], doc.getStyle("base"));
                doc.insertString(doc.getLength(), text[1], doc.getStyle("table"));
                doc.insertString(doc.getLength(), text[2], doc.getStyle("base"));
            } catch(BadLocationException e) {
                System.out.printf("Bad location error: %s%n", e.getMessage());
        private void styleContent(StyledDocument doc) {
            Style style = doc.getStyle("base");
            doc.setLogicalStyle(0, style);
            style = doc.getStyle("underline");
            doc.setCharacterAttributes(22, 10, style, false);
            style = doc.getStyle("highlight");
            doc.setCharacterAttributes(62, 26, style, false);
            Style logicalStyle = doc.getLogicalStyle(0);
            style = doc.getStyle("tableParagraph");
            doc.setParagraphAttributes(90, 1, style, false);
            style = doc.getStyle("table");
            doc.setCharacterAttributes(90, 1, style, false);
            doc.setLogicalStyle(92, logicalStyle);
            style = doc.getStyle("blue");
            doc.setCharacterAttributes(118, 13, style, false);
            style = doc.getStyle("italic");
            doc.setCharacterAttributes(166, 18, style, false);
            style = doc.getStyle("green");
            doc.setCharacterAttributes(235, 9, style, false);
            doc.setCharacterAttributes(248, 9, style, false);
            style = doc.getStyle("bold");
            doc.setCharacterAttributes(263, 10, style, false);
            doc.setCharacterAttributes(278, 6, style, false);
        private JScrollPane getTableComponent(Object[][] rows, Object[] colNames) {
            JTable table = new JTable(new SpecialTableModel(rows, colNames));
            Dimension d = table.getPreferredSize();
            d.width = 300;
            return new JScrollPane(table);
        private JScrollPane getContent() {
            return new JScrollPane(textPane);
        protected class SpecialTableModel extends DefaultTableModel
        private Object[][] rows;
        private Object[] cNames;
        public SpecialTableModel(Object[][] rows, Object[] cNames)
        super(rows, cNames);
        this.rows = rows;
        this.cNames = cNames;
       // public int getColumnCount() {return cNames.length;}
       // public int getRowCount() { return rows.length;}
        public Object getValueAt(int row, int col)
        return String.valueOf(rows[row][col]);
        public static void main(String[] args) {
            System.setProperty("swing.aatext", "true");
            JFrame f = new JFrame();
            Object[][] rows = new Object[1][5];
            rows[0][0] = "Mongoose";
            rows[0][1] = "Penugin";
            rows[0][2] = "Mark Levin";
            rows[0][3] = "Rikki-tikki-tavi";
            rows[0][4] = "America";
            Object[] colNames = new Object[5];
            colNames[0] = "Name";
            colNames[1] = "Address";
            colNames[2] = "City";
            colNames[3] = "First Name";
            colNames[4] = "Last Name";
            final AddingTables at = new AddingTables(rows, colNames);
            JMenuBar jmb = new JMenuBar();
            JMenu file = new JMenu("File");
            JMenuItem print = new JMenuItem("Print");
            print.addActionListener(new java.awt.event.ActionListener()
            public void actionPerformed(java.awt.event.ActionEvent e)
            catch(java.awt.print.PrinterException pe)
           // at.getTextPane().setFont(new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, 12));
    Last edited by GoodbyeWorld; June 10th, 2014 at 12:49 PM.

  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    My Mood
    Thanked 25 Times in 25 Posts

    Default Re: JTextPane losing a table when printing

    I have looked on Google and can't find a solution to the issue. I think I may have found one solution to it that somebody posted, but, alas, the link is dead.

    I cannot find a solution online to this.

    What good is JTextPane if it won't print images, icons, or tables added to it but will only display them and will remove them when you try and print?!?

    I was trying to find a way around java's wasteful practice of printing every stinking component on a separate page. I had tried something earlier but the guy who showed the tutorial but left out something crucial in explaining how to do it and the people here haven't replied.

    So I tried JTextPane to try the workaround but now it's not only not printing the table, it's removing it!!!
    Last edited by GoodbyeWorld; June 10th, 2014 at 01:34 PM.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: JTextPane not printing components inside it

    Swing graphical components are meant to be used to create graphical user interfaces and are not optimized or even designed to be printed. I suppose it's possible - sometimes, for some components - with mixed results, depending on a lot of variables, but I'm sure there are other applications designed specifically for printing things that are better able to meet your requirements.

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    My Mood
    Thanked 25 Times in 25 Posts

    Default Re: JTextPane not printing components inside it

    JTextPane has a method to add components and icons and stuff and allows styles to create tables. It also has a print method. However, when it prints it

    1.) Removes the tables and non-text stuff from the JTextPane.
    2.) Doesn't print them either.

    Is there a way to fix that or to print two or more JTextComponents and a JTable on the same page? (Note: those DO have print methods, but they tend to print all on separate pages.)

Similar Threads

  1. adding data in table but the table is in another frame
    By brianskiee in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 13th, 2014, 03:31 PM
  2. Resizing an image without losing transparency
    By beansnbacon in forum AWT / Java Swing
    Replies: 1
    Last Post: July 31st, 2013, 07:59 AM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. JTextPane questions.
    By sunde in forum AWT / Java Swing
    Replies: 1
    Last Post: March 28th, 2011, 03:18 PM

Tags for this Thread