July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating Interactive GUIs with Swing's MVC Architecture Part 2: Dynamic View for a Static Model

  • June 3, 2004
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Conclusion

The technique presented in this article relies on the separable architecture of Swing. The source code has a full listing of the custom model and filtering models as well as main the GUI code. It undoubtedly can be applied to any Swing object and imbedded into any graphical application that requires real-time manipulation of a presentation. Custom filters can do any number of tasks and be easily added later on in the development cycles, preserving existing functionality and introducing new features.

Download Source Code

Listing 1

class Moon
{
   public String name;
   public String planet;
   public int population;
   public String atmosphere;

   Moon(String name, String planet, String atmosphere, int population)
   {
      this.name = name;
      this.planet = planet;
      this.population = population;
      this.atmosphere = atmosphere;
   }

   public String toString()
   {
      return this.name;
   }
}

Listing 2

public void setModelData(Moon[] data) {
   for (int i = 0; i < data.length; i++) {
      Moon moon = data[i];
      Object row[] = new Object[] {moon.name, moon.planet,
                                   moon.atmosphere,
                                   String.valueOf(moon.population)};
      this.addRow(row);
      }
      fireTableDataChanged();
   }
Note: The fireTableDataChanged() method is one of methods that indicate to the visual components to sync with the model and therefore refresh the presentation.

Listing 3

public synchronized void reallocateIndexes() {

   int rowCount = model.getRowCount();

   indexList.clear();

   // Iterate through the model

   for (int z = 0; z < rowCount; z++) {
      String planet = (String) model.getValueAt(z, 0);
      if (filter.equals("All")) {
         indexList.add(new Integer(z));
      } else if (filter.equals(planet))
         indexList.add(new Integer(z));
      }
   }

Listing 4

public synchronized void setFilter(String filter) {

   if (model instanceof PlanetTableModel)
   ((PlanetTableModel) model).setFilter(filter);
   this.filter = filter;
   int rowCount = model.getRowCount();
   indexList.clear();

   // Iterate through the model
   for (int z = 0; z < rowCount; z++) {
   String planet = (String) model.getValueAt(z, 1);
   if (filter.equals("All")) {
         indexList.add(new Integer(z));
         } else if (filter.equals(planet))
            indexList.add(new Integer(z));
      }

      // Tell the component that the data have changed
      this.fireTableDataChanged();
   }

About the Author

Vlad Kofman is a 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 US government. His main interests are object-oriented programming methodologies and design patterns.

References

Core Swing Advanced Programming by Kim Topley ISBN: 0130832929. Publisher: Prentice Hall; December, 2000

Core Java 2, Volume II: Advanced Features (5th Edition) by Cay Horstmann and Gary Cornell. Publisher: Prentice Hall; December, 2001

A Swing Architecture Overview: http://java.sun.com/products/jfc/tsc/articles/architecture

Advanced MVC: http://www.ifi.unizh.ch/richter/Classes/oose2/03_mvc/02_mvc_java/02_mvc_java.html#4%20Models%20as%20Proxies

"Swing model filtering" by Mitch Goldstein: http://www-106.ibm.com/developerworks/java/library/j-filters/

Working with Models

Sorting and Otherwise Manipulating Data: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting





Page 3 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel