May 21, 2019
Hot Topics:

Manipulating User Data at the Model Layer in Swing

  • June 1, 2005
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Memory Management for Large Textual Data Models

Using the same approach for larger text components, such as JTextArea, can be beneficial not only when dealing with data manipulation, but also with memory management. The API that I overwrote to demonstrate this example is actually not from the model object itself, but from the component. However, it checks or consults the model before an operation so the principle is the same. Consider a main log window of a large GUI application, where there is a JTextArea into which all application-generated messages are posted. An application with a heavy operational load, running for an extensive period of time, can generate a lot of log information. If text area's data model object does not implement any memory management, JVM will run out of memory over time. The default API for the insert operation into the JTextArea is the append() method. Checking the size of the data in the model and decreasing or removing it periodically before appending will solve the memory hungry text area problem. The removal of data does not have to be total; it could be some percentage of the total number of characters. The text model would behave as a FIFO—first in, first out—queue.

For example, here is text area component that will remove the first 5000 characters if the total length of the data exceeds 10000 characters. And, it conveniently sets the CaretPosition to the end, so that if a scroll pane is used, it will automatically scroll to the end. Note that this object is also a singleton.

public class GlobalLogBean extends JTextArea {
   private static GlobalLogBean globalMB;
   private int DOC_MAX = 10000;

   private GlobalLogBean() {

public static GlobalLogBean getGlobalMBean()
   if (globalMB == null) {
      globalMB = new GlobalLogBean ();
   return globalMB;

public void append(String text)
      if (GlobalLogBean.this.getDocument().getLength() >= DOC_MAX)
         GlobalLogBean.this.getDocument().remove(0, 5000);
   catch(Exception e)

Filtering Data in Tables

Some Java APIs already make the concept of overwriting methods in API sub-classes for filtering purposes a requirement or useful extension to their functionalities. Consider a Java.io.FileFilter interface; it provides an accept method that has to be overwritten to filter a directory structure on the file system by files only.

For example:

public class FileOnlyFilter implements FileFilter {
   public FileOnlyFilter() {  }
   public boolean accept(File f) {
      if (f != null) {
         return f.isFile();
      else {
         return false;

This can be used in components such as file choosers, but it's not limited to them. For instance, if an application has a table (or other component) that needs to show a list of only files and not directories, FileOnlyFilter can be used in a model class for that component when browsing for files or creating the list.

// some where in code for DirectoryModel that extends
// AbstractTableModel
public void setDirectory(File dir) {
   if (dir != null) {
      directory = dir;
      File[] files = directory.listFiles(new FileOnlyFilter());
      children = new String[files.length];
      for (int i = 0; i < files.length; i++) {
         children[i] = files[i].getName();


The ability to modify data before it is put in the model object, is a very powerful concept. In this article, I've described several applications of this technique, in particular filtering and memory management. I believe that they are the most useful for a Swing developer and any GUI application can benefit from this method because the concept of overwriting key API can be applied to most of the model objects.

Download the code

You can download the source code used in this article here.

About the Author

Vlad Kofman is a Senior System Architect working on projects under government defense contracts. He has also been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.


Java Swing, 2nd Edition, by Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole. 2nd Edition. November 2002. ISBN: 0-596-00408-7

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date