-6

What is appearing in the text area.

And what is in the print.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • So what's the question? – Falla Coulibaly Mar 01 '17 at 06:48
  • It'd be easy to align the items in the table with the headings, and right align the amounts within their table cells if using HTML in a styled document like `JEditorPane`.. – Andrew Thompson Mar 01 '17 at 06:53
  • 1
    So, there's [`JTextArea#print`](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#print()), [`JTextArea#print(MessageFormat, MessageFormat)`](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#print(java.text.MessageFormat,%20java.text.MessageFormat)), [`JTextArea#print(lots of parameters)`](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#print(java.text.MessageFormat,%20java.text.MessageFormat,%20boolean,%20javax.print.PrintService,%20javax.print.attribute.PrintRequestAttributeSet,%20boolean)) – MadProgrammer Mar 01 '17 at 06:55
  • And if you really need to, can use [`JTextArea#getPrintable`](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#getPrintable(java.text.MessageFormat,%20java.text.MessageFormat)) or just use Jasper Reports – MadProgrammer Mar 01 '17 at 06:56
  • *"And what is in the print."* - Knowing "how" your printing it will make a HUGE difference in our ability to help you, I suspect that you just calling `paint(Graphics)` or `print(Graphics)` inside your own `Printable` which is going to cause no end of issues – MadProgrammer Mar 01 '17 at 06:57
  • So how to solve this? – Sanjeev Singh Mar 01 '17 at 07:04
  • PrinterJob pj = PrinterJob.getPrinterJob(); pj.setJobName(" Print Component "); pj.setPrintable (new Printable(){ public int print(Graphics pg, PageFormat pf, int pageNum){ if (pageNum > 0){ return Printable.NO_SUCH_PAGE; } Graphics2D g2 = (Graphics2D) pg; g2.translate(pf.getImageableX(), pf.getImageableY()); jTextArea1.paint(g2); return Printable.PAGE_EXISTS; } }); if (pj.printDialog() == false) return; try { pj.print(); } catch (PrinterException ex) { } – Sanjeev Singh Mar 01 '17 at 07:08
  • See the links listed above, start with one of those – MadProgrammer Mar 01 '17 at 07:08
  • `jTextArea1.print()` – MadProgrammer Mar 01 '17 at 07:18

1 Answers1

2

Many Swing components support printing out of the box

You could use something as simple as JTextArea.print to get started, for example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.util.StringJoiner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea ta;

        public TestPane() {
            setLayout(new BorderLayout());
            String[] lines = {
                "Idx     Met        MTU        State                Name           ",
                "---  ---------  ----------  ------------  --------------------------",
                "  1         50  4294967295  connected     Loopback Psudo-Interface 1",
                " 11         10        1500  connected     Local Area Connection     ",
                " 11          5        1500  disconnected  Local Area Connection 3   ",};
            StringJoiner joiner = new StringJoiner("\n");
            for (String line : lines) {
                joiner.add(line);
            }
            ta = new JTextArea(joiner.toString());
            ta.setBorder(new LineBorder(Color.RED));
            ta.setFont(new Font("Monospaced", Font.PLAIN, 13));
            ta.setWrapStyleWord(true);
            add(new JScrollPane(ta));

            JButton btn = new JButton("Print");
            add(btn, BorderLayout.SOUTH);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        ta.print();
                    } catch (PrinterException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

    }
}

Which can output...

enter image description here

You might also want to have a look at How can I print a custom paper size (cheques 8" x 4")? if you need print to non-standard page sizes

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks a lot:) but then how can i able to print heading? – Sanjeev Singh Mar 01 '17 at 18:00
  • Have even tried to read the API docs? There are at least three different print methods, maybe if you take 5 seconds you'd be surprised and might actually answer your own question - sorry, but that's exceedingly annoying – MadProgrammer Mar 01 '17 at 18:52
  • I am really sorry and i apologize. your answer was really helpful and again thank you so much for giving so much time. have a good day sir:) – Sanjeev Singh Mar 03 '17 at 07:37