Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

Custom Search
Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 9/9 



Previous Page Previous Page (8/9)
Subpages: 1. JTable  
2.
Stocks Table: part I - Basic JTable example 
3.
Stocks Table: part II - Custom renderers 
4. Stocks Table: part III - Data formatting 
5. Stocks Table: part IV - Sorting columns 
6.
Stocks Table: part V - JDBC
7.
Stocks Table: part VI - Column addition and removal
8.
Expense report application
9.
JavaBeans property editor

18.9  A JavaBeans property editor

Now that we're familiar with the table API we can complete the JavaBeans container introduced in the chapter 4 and give it the capability to edit the properties of JavaBeans. This dramatically increases the possible uses of our simple container and makes it a quite powerful tool for studying JavaBeans.

Figure 18.8 BeanContainer JavaBeans property editor using JTables as editing forms.

<<file figure18-8.gif>>

The Code: BeanContainer.java

see \Chapter18\8

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.beans.*;

import java.lang.reflect.*;

import java.util.*;

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.event.*;

import dl.*;

public class BeanContainer extends JFrame implements FocusListener

{

  protected Hashtable m_editors = new Hashtable();

  // Unchanged code from section 4.7

  protected JMenuBar createMenuBar() {

    // Unchanged code from section 4.7

    JMenu mEdit = new JMenu("Edit");

    mItem = new JMenuItem("Delete");

    lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        if (m_activeBean == null)

          return;

        Object obj = m_editors.get(m_activeBean);

        if (obj != null) {

          BeanEditor editor = (BeanEditor)obj;

          editor.dispose();

          m_editors.remove(m_activeBean);

        }

        getContentPane().remove(m_activeBean);

        m_activeBean = null;

        validate();

        repaint();

      }

    };

    mItem.addActionListener(lst);

    mEdit.add(mItem);

    mItem = new JMenuItem("Properties...");

    lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        if (m_activeBean == null)

          return;

        Object obj = m_editors.get(m_activeBean);

        if (obj != null) {

          BeanEditor editor = (BeanEditor)obj;

          editor.setVisible(true);

          editor.toFront();

        }

        else {

          BeanEditor editor = new BeanEditor(m_activeBean);

          m_editors.put(m_activeBean, editor);

        }

      }

    };

    mItem.addActionListener(lst);

    mEdit.add(mItem);

    menuBar.add(mEdit);

    // Unchanged code from section 4.7

    return menuBar;

  }

  // Unchanged code from section 4.7

}

class BeanEditor extends JFrame implements PropertyChangeListener

{

  protected Component m_bean;

  protected JTable m_table;

  protected PropertyTableData m_data;

  public BeanEditor(Component bean) {

    m_bean = bean;

    m_bean.addPropertyChangeListener(this);

    Point pt = m_bean.getLocationOnScreen();

    setBounds(pt.x+50, pt.y+10, 400, 300);

    getContentPane().setLayout(new BorderLayout());

    m_data = new PropertyTableData(m_bean);

    m_table = new JTable(m_data);

    JScrollPane ps = new JScrollPane();

    ps.getViewport().add(m_table);

    getContentPane().add(ps, BorderLayout.CENTER);

    setDefaultCloseOperation(HIDE_ON_CLOSE);

    setVisible(true);

  }

  public void propertyChange(PropertyChangeEvent evt) {

    m_data.setProperty(evt.getPropertyName(), evt.getNewValue());

  }

  class PropertyTableData extends AbstractTableModel

  {

    protected String[][] m_properties;

    protected int m_numProps = 0;

    protected Vector m_v;

    public PropertyTableData(Component bean) {

      try {

        BeanInfo info = Introspector.getBeanInfo(

          m_bean.getClass());

        BeanDescriptor descr = info.getBeanDescriptor();

        setTitle("Editing "+descr.getName());

        PropertyDescriptor[] props = info.getPropertyDescriptors();

        m_numProps = props.length;

        m_v = new Vector(m_numProps);

        for (int k=0; k<m_numProps; k++) {

          String name = props[k].getDisplayName();

          boolean added = false;

          for (int i=0; i<m_v.size(); i++) {

            String str = ((PropertyDescriptor)m_v.elementAt(i)).

              getDisplayName();

            if (name.compareToIgnoreCase(str) < 0) {

              m_v.insertElementAt(props[k], i);

              added = true;

              break;

            }

          }

          if (!added)

            m_v.addElement(props[k]);

        }

        m_properties = new String[m_numProps][2];

        for (int k=0; k<m_numProps; k++) {

          PropertyDescriptor prop =

            (PropertyDescriptor)m_v.elementAt(k);

          m_properties[k][0] = prop.getDisplayName();

          Method mRead = prop.getReadMethod();

          if (mRead != null &&

           mRead.getParameterTypes().length == 0) {

            Object value = mRead.invoke(m_bean, null);

            m_properties[k][1] = objToString(value);

          }

          else

            m_properties[k][1] = "error";

        }

      }

      catch (Exception ex) {

        ex.printStackTrace();

        JOptionPane.showMessageDialog(

          BeanEditor.this, "Error: "+ex.toString(),

          "Warning", JOptionPane.WARNING_MESSAGE);

      }

    }

    public void setProperty(String name, Object value) {

      for (int k=0; k<m_numProps; k++)

        if (name.equals(m_properties[k][0])) {

          m_properties[k][1] = objToString(value);

          m_table.tableChanged(new TableModelEvent(this, k));

          m_table.repaint();

          break;

        }

    }

    public int getRowCount() { return m_numProps; }

    public int getColumnCount() { return 2; }

    public String getColumnName(int nCol) {

      return nCol==0 ? "Property" : "Value";

    }

    public boolean isCellEditable(int nRow, int nCol) {

       return (nCol==1);

    }

    public Object getValueAt(int nRow, int nCol) {

      if (nRow < 0 || nRow>=getRowCount())

        return "";

      switch (nCol) {

        case 0: return m_properties[nRow][0];

        case 1: return m_properties[nRow][1];

      }

      return "";

    }

    public void setValueAt(Object value, int nRow, int nCol) {

      if (nRow < 0 || nRow>=getRowCount())

        return;

      String str = value.toString();

      PropertyDescriptor prop = (PropertyDescriptor)m_v.

        elementAt(nRow);

      Class cls = prop.getPropertyType();

      Object obj = stringToObj(str, cls);

      if (obj==null)

        return;        // can't process

      Method mWrite = prop.getWriteMethod();

      if (mWrite == null || mWrite.getParameterTypes().length != 1)

        return;

      try {

        mWrite.invoke(m_bean, new Object[]{ obj });

        m_bean.getParent().doLayout();

        m_bean.getParent().repaint();

        m_bean.repaint();

      }

      catch (Exception ex) {

        ex.printStackTrace();

        JOptionPane.showMessageDialog(

          BeanEditor.this, "Error: "+ex.toString(),

          "Warning", JOptionPane.WARNING_MESSAGE);

      }

      m_properties[nRow][1] = str;

    }

    public String objToString(Object value) {

      if (value==null)

        return "null";

      if (value instanceof Dimension) {

        Dimension dim = (Dimension)value;

        return ""+dim.width+","+dim.height;

      }

      else if (value instanceof Insets) {

        Insets ins = (Insets)value;

        return ""+ins.left+","+ins.top+","+ins.right+","+ins.bottom;

      }

      else if (value instanceof Rectangle) {

        Rectangle rc = (Rectangle)value;

        return ""+rc.x+","+rc.y+","+rc.width+","+rc.height;

      }

      else if (value instanceof Color) {

        Color col = (Color)value;

        return ""+col.getRed()+","+col.getGreen()+","+col.getBlue();

      }

      return value.toString();

    }

    public Object stringToObj(String str, Class cls) {

      try {

        if (str==null)

          return null;

        String name = cls.getName();

        if (name.equals("java.lang.String"))

          return str;

        else if (name.equals("int"))

          return new Integer(str);

        else if (name.equals("long"))

          return new Long(str);

        else if (name.equals("float"))

          return new Float(str);

        else if (name.equals("double"))

          return new Double(str);

        else if (name.equals("boolean"))

          return new Boolean(str);

        else if (name.equals("java.awt.Dimension")) {

          int[] i = strToInts(str);

          return new Dimension(i[0], i[1]);

        }

        else if (name.equals("java.awt.Insets")) {

          int[] i = strToInts(str);

          return new Insets(i[0], i[1], i[2], i[3]);

        }

        else if (name.equals("java.awt.Rectangle")) {

          int[] i = strToInts(str);

          return new Rectangle(i[0], i[1], i[2], i[3]);

        }

        else if (name.equals("java.awt.Color")) {

          int[] i = strToInts(str);

          return new Color(i[0], i[1], i[2]);

        }

        return null;    // not supported

      }

      catch(Exception ex) { return null; }

    }

    public int[] strToInts(String str) throws Exception {

      int[] i = new int[4];

      StringTokenizer tokenizer = new StringTokenizer(str, ",");

      for (int k=0; k<i.length &&

       tokenizer.hasMoreTokens(); k++)

        i[k] = Integer.parseInt(tokenizer.nextToken());

      return i;

    }

  }

}

Understanding the Code

Class BeanContainer

This class (formerly BeanContainer from section 4.7) has received a new collection, Hashtable m_editors, added as an instance variable. This Hashtable holds references to BeanEditor frames (used to edit beans, see below) as values and the corresponding Components being edited as keys.

A new menu item titled "Properties..." is added to the "Edit" menu. This item is used to create a new editor for the selected bean or activate an existing one (if any). The attached ActionListener looks for an existing BeanEditor corresponding to the currently selected m_activeBean component in the m_editors collection. If such an editor is found it is made visible and brought to the front. Otherwise, a new instance of BeanEditor is created to edit the currently active m_activeBean component, and is added to the m_editors collection.

The ActionListener attached to menu item "Delete," which removes the currently active component, receives additional functionality. The added code looks for an existing BeanEditor corresponding to the currently selected m_activeBean component in the m_editors collection. If such an editor is found it is disposed and its' reference is removed from the hashtable.

Class BeanEditor

This class extends JFrame and implements the PropertyChangeListener interface.  BeanEditor is used to display and edit the properties exposed by a given JavaBean. Three instance variables are declared:

Component m_bean: JavaBean component to be edited.

JTable m_table: table component to display a bean's properties.

PropertyTableData m_data: table model for m_table.

The BeanEditor constructor takes a reference to the JavaBean component to be edited, and stores it in instance variable m_bean. The initial location of the editor frame is selected depending on the location of the component being edited.

The table component, m_table, is created and added to a JScrollPane to provide scrolling capabilities. Note that we do not add a WindowListener to this frame. Instead we use the HIDE_ON_CLOSE default close operation (see chapter 3):

        setDefaultCloseOperation(HIDE_ON_CLOSE);

        setVisible(true);

Upon closing, this frame will be hidden but not disposed. Its' reference will still be present in the m_editors collection, and this frame will be re-activated if the user chooses to see the properties of the associated bean again.

Note that an instance of the BeanEditor class is added as a PropertyChangeListener to the corresponding bean being edited. The propertyChange() method is invoked if the bean has changed it's state during editing and a PropertyChangeEvent has been fired. This method simply triggers a call to the setProperty() method of the table model.

Class BeanEditor.PropertyTableData

PropertyTableData extends AbstractTableModel and provides the table model for each bean editor. Three instance variables are declared:

String[][] m_properties: an array of data displayed in the table.

int m_numProps: number of a bean properties (corresponds to the number of rows in the table).

Vector m_v: collection of PropertyDescriptor objects sorted in alphabetical order.

The constructor of the PropertyTableData class takes a given bean instance and retrieves it's properties. First it uses the Introspector.getBeanInfo() method to get a BeanInfo instance:

      BeanInfo info = Introspector.getBeanInfo(

        m_bean.getClass());

      BeanDescriptor descr = info.getBeanDescriptor();

      setTitle("Editing "+descr.getName());

      PropertyDescriptor[] props = info.getPropertyDescriptors();

      m_numProps = props.length;

This provides us with all available information about a bean (see chapter 2). We determine the bean's name and use it as the editor frame's title (note that this is an inner class, so setTitle() refers to the parent BeanEditor instance). We then extract an array of PropertyDescriptors which will provide us with the actual information about a bean's properties.

Bean properties are sorted by name in alphabetical order. The name of each property is determined with the getDisplayName() method. The sorted PropertyDescriptors are stored in our m_v Vector collection. Then we can create the 2-dimensional array, m_properties, which holds data to be displayed in the table. This array has m_numProps rows and 2 columns (for property name and value). To determine a property's value we need to obtain a reference to its getXX() method with getReadMethod() and make a call using the reflection API. We can call only getXX() methods without parameters (since we don't know anything about these parameters). Note that our objToString() helper method is invoked to translate a property's value into a display string (see below).

The setProperty() method searches for the given name in the 0-th column of the m_properties array. If such a property is found this method sets it's new value and updates the table component.

Several other simple methods included in this class have already been presented in previous examples and need not be explained again here. However, note that the isCellEditable() method returns true only for cells in the second column (property names, obviously, cannot be changed).

The setValueAt() method deserves additional explanation because it not only saves the modified data in the table model, but it also sends these modifications to the bean component itself. To do this we obtain a PropertyDescriptor instance stored in the m_v Vector collection. The modified property value is always a String, so first we need to convert it into its proper object type using our stringToObj() helper method (if we can do this, see below). If the conversion succeeds (i.e. the result is not null), we can continue.

To modify a bean value we determine the reference to it's setXX()method (corresponding to a certain property) and invoke it. Note that an anonymous array containing one element is used as parameter (these constructions are typical when dealing with the reflection API). Then the bean component and it's container (which can also be affected by changes in such properties as size and color) are refreshed to reflect the bean's new property value. Finally, if the above procedures were successful, we store the new value in the m_properties data array.

The objToString() helper method converts a given Object into a String suitable for editing. In many cases the toString() method returns a long string starting with the class name. This is not very appropriate for editable data values. So for several classes we provide our own conversion into a string of comma-delimited numbers. For instance a Dimension object is converted into a "width, height" form, Color is converted into "red, green, blue" form, etc. If no special implementation is provided, an object's toString() string is returned.

The stringToObj() helper method converts a given String into an Object of the given Class. The class's name is analyzed and a conversion method is chosen to build the correct type of object based on this name. The simplest case is the String class: we don't need to do any conversion at all in this case. For the primitive data types such as int or boolean we return the corresponding encapsulating (wrapper class) objects. For the several classes which receive special treatment in the objToString() method (such as a Dimension or Color object), we parse the comma-delimited string of numbers and construct the proper object. For all other classes (or if a parsing exception occurs) we return null to indicate that we cannot perform the required conversion.

Running the Code

Figure 18.9 shows the BeanContainer container and two editing frames displaying the properties of Clock and JButton components. This application provides a simple but powerful tool for investigating Swing and AWT components as well as custom JavaBeans. We can see all exposed properties and modify many of them. If a component's properties change as a result of user interaction, our component properly notifies its' listeners and we see an automatic editor table update. Try serializing a modified component and restoring it from its' file. Note how the previously modified properties are saved as expected. 

It is natural to imagine using this example as a base for constructing a custom Swing IDE (Interface Development Environment). BeanContainer, combined with the custom resize edge components developed in chapters 15 and 16, provides a fairly powerful base to work from.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy