Swing is a very powerful Java GUI toolkit and rank among the best GUI toolkits in the market. Despite its early negative reputation as being slow and buggy, Swing has come a long way since its inception a decade ago. Each iteration of Java incorporates noticeably improvement of Swing in term of performance, stability, power and ease of use. Swing also leverages on new hardware and software capabilities like Windows DirectX, graphics hardware acceleration. No doubt that Swing has a steep learning curve but developer who willing to spend time learning it will be rewarded with a toolkit that virtually capable of building any software imaginable, be it word processor, IDE. The future of Swing and Java2D which it built upon are promising and are the foundation of Java’s new scripting language, JavaFX.
Printing Text in JTextArea
Swing’s API has improved a great deal by making it simple to do mundane thing that once require the developer to implement. The printing facility is one area that Swing has made it easy for the developer. To print the content of text component that is subclass of JTextComponent (which defines the print method), one just need to call print method as follow. However, currently Swing does not provide a easy way to print selected text. Still with ‘creative problem solving’, there are ways to implement it just as easy. The following code is one way to implement it through using a dummy hidden text component. With some modification, the code can also be reused for JTextField, JEditorPane etc
Running the code
When code is run, it show the following,

User can either select portion of text or the whole text (no selection) and click on Print button. The print dialog will appear.
CODE <TestPrint.java>
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
public class TestPrint extends JFrame
{
JTextArea uiTxaDummy = new JTextArea(), // Hidden for printing only,
uiTxaActual = new JTextArea(15, 25) // Show on screen
{
// Ensure that same font is used if font change for uiTxaActual
public void setFont(Font f)
{
super.setFont(f);
uiTxaDummy.setFont(f);
}
};
{
// Set line wrap for dummy because if not, text may be truncated if
// wordwrap for uiTxaActual is not set.
// uiTxaActual on screen may or may not be wordwrapped but it doesn’t
// made sense to print if text truncated, hence dummy will be wordwrap.
uiTxaDummy.setLineWrap(true);
uiTxaDummy.setWrapStyleWord(true);
uiTxaActual.setFont(
new Font(uiTxaActual.getFont().getName(),
Font.BOLD, 15));
}
JScrollPane uiScpText = new JScrollPane(uiTxaActual);
public TestPrint()
{
super(”TestPrint”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton _uiButPrint = new JButton(”Print”);
_uiButPrint.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
print();
}
});
add(uiScpText);
add(_uiButPrint, BorderLayout.SOUTH);
pack();
}
// Printing text
public void print()
{
// Print all text
if (uiTxaActual.getSelectedText() == null)
{
uiTxaDummy.setText(uiTxaActual.getText());
}
else // Print selected text
{
uiTxaDummy.setText(uiTxaActual.getSelectedText());
}
try
{
// This will show the print dialog.
uiTxaDummy.print();
}
catch (PrinterException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
// Set focus so that selected text is highlighted.
uiTxaActual.requestFocusInWindow();
}
public static void main(String[] _args)
{
TestPrint _fra = new TestPrint();
_fra.setVisible(true);
}
}
Alternative Code <PrinteableTextArea.java> that extends JTextArea
Just instantiate PrintableTextArea and call print method, that’s it !
// Allow selection printing of text in JTextArea
public class PrintableTextArea extends JTextArea
{
private JTextArea uiTxaDummy = new JTextArea();
{
// Set line wrap for dummy because if not, text may be truncated if
// wordwrap for uiTxaActual is not set.
// uiTxaActual on screen may or may not be wordwrapped but it doesn’t
// made sense to print if text truncated, hence dummy will be wordwrap.
uiTxaDummy.setLineWrap(true);
uiTxaDummy.setWrapStyleWord(true);
}
public PrintableTextArea (int _rows, int _cols)
{
super(_rows, _cols);
}
public void setFont(Font _font)
{
super.setFont(_font);
// Note that when instantiate, the superclass will be ran first, and when run
// the superclass setFont will called subclass setFont, and hence variable in subclass will be null.
if (uiTxaDummy != null)
{
uiTxaDummy.setFont(_font);
}
}
@Override
// Note that one cannot even override a subclass method that has define Exception !
public boolean print()
{
boolean _isPrint = false;
// Print all text
if (getSelectedText() == null)
{
uiTxaDummy.setText(getText());
}
else // Print selected text
{
uiTxaDummy.setText(getSelectedText());
}
try
{
// This will show the print dialog.
_isPrint = uiTxaDummy.print();
}
catch (PrinterException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
// Set focus so that selected text is highlighted.
requestFocusInWindow();
return _isPrint;
}
}



Thenks to author
get an error on line 70 saying “cannot find symbol method print()”
Nick,
make sure you use Java’s 1.6. The method is added in this version.