Search code examples
javaprintingjtextarea

Sending JTextArea Component to the Printer


I have been working for 12 hours trying to get a simple jtextarea component's contents to be sent to the printer. This is insane. I looked over JavaDocs as was suggested in a previous code but frankly that code doesn't work and reads errors. I'm incredibly frustrated because it was pretty simple (~8 lines of code) to print the contents of a JTable but for some reason printing the contents of the jtextarea is totally different and much more complicated involving multiple classes and in some examples over 100 lines of code. I don't understand why the process of simply printing a Java component would be so convoluted.

Can anyone please tell me why the following code reads the error "Add argument to match print(Graphics)" and how to fix it so that I can send the textarea component to the printer and move on with my life.

JButton btnNewButton_7 = new JButton("Print");
    btnNewButton_7.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(ActionEvent arg0) {  

        try{
            boolean complete = textArea_2.print();
            //The above line reads the error "Add argument to match print(Graphics)"
            if(complete){
                JOptionPane.showMessageDialog(null,  "Printjob Finished", "Report",
                        JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null, "Printing", "Printer", JOptionPane.ERROR_MESSAGE);
                }
            }catch(PrinterException e){JOptionPane.showMessageDialog(null, e);
            }
        }

    });

Solution

  • You might want to take a look at StandardPrint. You can get rid of the lines that reference WindowUtilities

    From that class:

        JFrame jf = new JFrame("StandardPrint Test");
        final JTextArea area = new JTextArea();
        area.append("hello\n");
        for (int i = 0; i < 50; i++) {
            area.append("\n");
        }
        area.append("world\n");
        JScrollPane sp = new JScrollPane(area);
        jf.add(sp);
        JMenuBar bar = new JMenuBar();
        JMenu printMenu = new JMenu("Print");
        JMenuItem print = new JMenuItem("Print");
        printMenu.add(print);
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                StandardPrint sp = new StandardPrint(area);
                sp.setTitle("Hello World");
                sp.setPrintPageNumber(true);
                sp.setPageNumberVAlignment(BOTTOM);
                sp.setPageNumberHAlignment(CENTER);
                System.out.println(sp.getNumberOfPages());
                Image im1 = preview(300,300, sp, sp.getPageFormat(0), 0);
                Image im2 = preview(300,300, sp, sp.getPageFormat(1), 1);
                JLabel l = new JLabel(new ImageIcon(im1));
                JOptionPane.showMessageDialog(null, l);
                l = new JLabel(new ImageIcon(im2));
                JOptionPane.showMessageDialog(null, l);
            }
        };
        print.addActionListener(al);
        jf.setJMenuBar(bar);
        bar.add(fm);
        bar.add(printMenu);
        jf.setBounds(100,100,400,400);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }