next up previous
Next: Menus and Toolbars Up: Split panes Previous: Sample application with JSplitPane

Intercepting JSplitPane events

There may be situations when you need to know that the divider in a split pane has moved; however, if the online documentation for JSplitPane, you will notice that this class does not offer any method like addChangeListener().

A JSplitPane is really just a container for other components.

If you examine the JComponent class, you will notice a method called addAncestorListener(), which is used to notify a component when its owner has changed.

You can implement this code as follows:

class MyPanel
     extends  JPanel
implements AncestorListener
{
    // Add any special code for this component
    // Ancestor Listener support
    public void ancestorMoved( AncestorEvent event )
       {
           // Add code here to handle a split pane divider movement
         }

   // Not used
   public void ancestorAdded( AncestorEvent event )
     {
      }

   public void ancestorRemoved( AncestorEvent event )
   {
    }
}

The MyPanel class can then be added to one side of the split pane and will faithfully detect any changes to the divider.



Dave Marshall
4/14/1999