JThreadKit Home

JTKTextArea Demo - Instructions

Overview

The class JTKTextArea is a great tool for building thread-safe gui's. JTKTextArea is a JThreadKit Swing component that adds some great additional features to a JTextArea. The main feature highlighted in the demo is the ability to safely set or append text (from a helper Thread) without the need to create a Runnable Object and call SwingUtilities.invokeLater(). You simply create a JTKTextArea Object by passing a JTextArea to the constructor:

JTKTextArea textArea = new JTKTextArea(new JTextArea());

Now any Thread can call the setTextSafely() or appendSafely() methods. Note: JTKTextArea is not a subclass of Component. To add the wrapped JTextArea to a Container, simply do the following:

container.add(textArea.getTextArea());

Running the Demo

Once the demo appears, click the "Start" button. The JTKTextArea's text will now start changing approximately every second. The demo randomly calls setTextSafely() or appendSafely(). Notice the text area is cleared on a call to setTextSafely(), or the random text is appended when appendSafely() is called. A key feature to point out is the text area scrolling as text is appended beyond the visible area of the JScrollPane. This will not happen if you call a JTextArea's setText() or append() methods directly from a helper Thread. The JTKTextArea takes care of this issue.

You can stop and restart the demo if you wish, or simply close the window to end it.