Home > OS >  Setting font size for a print operation from a text component
Setting font size for a print operation from a text component

Time:02-20

I have the following program that puts a text string in a JTextArea and offers a print dialog for printing it:

public class PrintTest
{
  private static String fontName = Font.MONOSPACED; // "Verdana"
  private static FontUIResource defaultFixedWidthCellFont = new FontUIResource(fontName, Font.PLAIN, 16);
  public static FontUIResource getDefaultCellFont() { return defaultFixedWidthCellFont; }

  public static void main(String[] args)
  {
    JFrame frame = new JFrame("Print test");
    JTextArea textArea = new JTextArea();
    
    textArea.setEditable(false);
    textArea.setFont(getDefaultCellFont());
    textArea.setPreferredSize(new Dimension(400,200));

    textArea.setText("one and two and three");
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
    
    try { textArea.print(); }
    catch (PrinterException pe) { pe.printStackTrace(); }

  }

}

In the real application, the text area does not need to be edited, it does need to be a fixed width font of this size. I am willing to use a text component other than a JTextArea, as long as I can set it to a fixed width font size and put it in a JScrollPane. I do not want any text wrapping. After getting put on the screen, it does not change (it's a report).

The problem is that the text, when printed on the printer, is too large. I do not want to reduce the size of the text in the window. I do not want to put the text in a file and print the file. The text is one long string in my Java program, there's no need to make a file of it.

When I attempt to search solutions, I get lots of articles about printing from eclipse, using System.out.println, and an occasional article that involves using Graphics objects to render individual text strings. I was hoping for something simpler; the program does do the printing, I just need a different font.

I have tried putting the text into another JTextArea and giving it a derived font with a different size, but that didn't work -- I did not render that text area, and don't know if that would matter. Is that a way to get this done? I've seen references to off-screen buffers for graphics, but don't know how I'd go about telling my text component to render itself to an offscreen buffer.

Or is there a better way to get this done?

CodePudding user response:

Create a new JTextArea with the font you want and print that instead of the one on the screen

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.plaf.FontUIResource;

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        private static String fontName = Font.MONOSPACED; // "Verdana"
        private static FontUIResource defaultFixedWidthCellFont = new FontUIResource(fontName, Font.PLAIN, 32);
        private static FontUIResource defaultFixedWidthPrintFont = new FontUIResource(fontName, Font.PLAIN, 8);

        public static FontUIResource getDefaultCellFont() {
            return defaultFixedWidthCellFont;
        }

        public static FontUIResource getDefaultPrintFont() {
            return defaultFixedWidthPrintFont;
        }

        public TestPane() {
            setLayout(new BorderLayout());
            JTextArea textArea = new JTextArea();

            textArea.setEditable(false);
            textArea.setFont(getDefaultCellFont());

            textArea.setText("one and two and three");
            JScrollPane scrollPane = new JScrollPane(textArea);
            add(scrollPane);

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

            print.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JTextArea printTA = new JTextArea(textArea.getText());
                    printTA.setFont(getDefaultPrintFont());
                    try {
                        printTA.print();
                    } catch (PrinterException pe) {
                        pe.printStackTrace();
                    }
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 200);
        }
    }
}
  • Related