HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"GUI Design Patterns"
Vol. 9, Issue 7, p. 48

	



Listing 1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * The ListChooser implements the "Add-and-Remove"
 * GUI design pattern.
 */
public class ListChooser extends JPanel
   implements ActionListener
{
   /** @serial  Original list. */
   private JList _originalList = null;

   /** @serial  Chosen list. */
   private JList _chosenList = null;

   /** @serial  Original label. */
   private JLabel _originalLabel = null;

   /** @serial  Chosen label. */
   private JLabel _chosenLabel = null;

   /** @serial  "Add" button. */
   private JButton _add = null;

   /** @serial  "Remove" button. */
   private JButton _remove = null;

   /** @serial  "Add All" button. */
   private JButton _addAll = null;

   /** @serial  "Remove All" button. */
   private JButton _removeAll = null; 

   /**
    * Constructor.
    * <p>
    * @param  original  original objects
    * @param  chosen    chosen objects
    */
   public ListChooser( Object[] original, Object[] chosen )
   {
      // Create Lists and ListModels.
      super();
      setFocusable( true );
      DefaultListModel originalModel = 
         new DefaultListModel();
      DefaultListModel chosenModel = 
         new DefaultListModel();
      for( int i = 0; ( i < original.length ); i++ )
         originalModel.addElement( original[ i ]);
      for( int i = 0; ( i < chosen.length ); i++ )
         chosenModel.addElement( chosen[ i ]);
      _originalList = new JList( originalModel );
      _chosenList = new JList( chosenModel );

      // Place lists.
      setLayout( new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.weighty = 1.0;   
      c.gridwidth = 1; 
      c.gridheight = 4;
      c.gridy = 1;
      c.gridx = 0;   
      JScrollPane pane = new JScrollPane( _originalList );
      _originalList.setVisibleRowCount( 7 );
      pane.setPreferredSize( new Dimension( 80, _originalList.
         getPreferredScrollableViewportSize().height ));
      add( pane, c );
      c.gridx = 2;
      pane = new JScrollPane( _chosenList );
      _chosenList.setVisibleRowCount( 7 );
      pane.setPreferredSize( new Dimension( 80, _chosenList.
         getPreferredScrollableViewportSize().height ));
      add( pane, c );
 
      // Place labels.
      _originalLabel = new JLabel( "Original:" );
      _chosenLabel = new JLabel( "Chosen:" );
      c.weighty = 0.0;
      c.gridheight = 1;
      c.gridy = 0;
      c.gridx = 0;
      add( _originalLabel, c );
      c.gridx = 2;
      add( _chosenLabel, c );

      // Place buttons.
      _add = new JButton( "Add" );
      _add.setMnemonic( 'A' ); 
      _addAll = new JButton( "Add All" );
      _remove = new JButton( "Remove" );
      _removeAll = new JButton( "Remove All" );
      c.weightx = 0.0;
      c.weighty = 1.0;
      c.gridx = 1; 
      c.gridy = 1;
      c.anchor = GridBagConstraints.SOUTH;
      add( _add, c );
      c.gridy = 3;
      add( _remove, c );
      c.anchor = GridBagConstraints.NORTH;
      c.gridy = 2;
      add( _addAll, c );
      c.gridy = 4;
      add( _removeAll, c );

      // Attach listeners.   
      _add.addActionListener( this );
      _addAll.addActionListener( this );
      _remove.addActionListener( this );
      _removeAll.addActionListener( this );
   }

   /**
    * Action event listener.
    * <p>
    * @param  e  ActionEvent
    */
   public void actionPerformed( ActionEvent e )
   {
      // Get models and command.
      DefaultListModel originalModel = 
         _originalList.getModel();
      DefaultListModel chosenModel = 
         _chosenList.getModel();
      String command = e.getActionCommand();

      // Add selected objects to the chosen list...
      if( command.equals( _add.getText()))
      {  Object[] values = 
            _originalList.getSelectedValues();
         for( int i = 0; ( i < values.length ); i++ )
         {  chosenModel.addElement( values[ i ]);
            originalModel.removeElement( values[ i ]);
         }
      }

      // ...or add all objects to the chosen list...
      else if( command.equals( _addAll.getText()))  
      {  Object[] values = originalModel.toArray();
         for( int i = 0; ( i < values.length ); i++ )
            chosenModel.addElement( values[ i ]);
         originalModel.clear();
      }

      // ...or remove selected objects to original list...
      else if( command.equals( _remove.getText()))
      {  Object[] values = 
            _chosenList.getSelectedValues();
         for( int i = 0; ( i < values.length ); i++ )
         {  originalModel.addElement( values[ i ]);
            chosenModel.removeElement( values[ i ]);
         }
      }

      // ...or remove all objects to the original list...
      else if( command.equals( _removeAll.getText()))
      {  Object[] values = chosenModel.toArray();
         for( int i = 0; ( i < values.length ); i++ )
            originalModel.addElement( values[ i ]);
         chosenModel.clear();
      }
   }
}

Listing 2

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
 * The TableChooser implements the "Add-and-Remove" GUI
 * design pattern and displays the chosen list as a table.
 */
public class TableChooser extends JPanel
   implements ActionListener
{
  /** @serial  Original table. */
   private JTable _originalTable = null;

   /** @serial  Chosen table. */
   private JTable _chosenTable = null;
      .
      .
      .
   /**
    * Constructor.
    * <p>
    * @param  original     original objects
    * @param  chosen       chosen objects
    * @param  columnNames  column names
    */
   public TableChooser( Object[][] original, 
      Object[][] chosen, Object[] columnNames )
   {
      // Create Tables and TableModels.
      super();
      setFocusable( true );
      DefaultTableModel originalModel = 
         new DefaultTableModel( original, columnNames );
      DefaultTableModel chosenModel = 
         new DefaultTableModel( chosen, columnNames );
      _originalTable = new JTable( originalModel );
      _originalTable.setTableHeader( null );
      _originalTable.setShowGrid( false );
      _chosenTable = new JTable( chosenModel );

      // Place tables in scroll panes of appropriate width.
      TableColumnModel tcm = 
         _originalTable.getColumnModel();
      TableColumn tc = tcm.getColumn( 0 );
      _originalWidth = tc.getPreferredWidth();
      tc.setMinWidth( _originalWidth );
      _originalTable.setPreferredScrollableViewportSize(
         new Dimension( _originalWidth, 
         10 * _originalTable.getRowHeight()));
      _chosenTable.setPreferredScrollableViewportSize(
         new Dimension( tcm.getTotalColumnWidth(),
         10 * _chosenTable.getRowHeight()));
      JScrollPane originalPane = 
         new JScrollPane( _originalTable );
      originalPane.getViewport().setBackground( Color.white );
      originalPane.setVerticalScrollBarPolicy(
         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
      JScrollPane chosenPane = 
         new JScrollPane( _chosenTable );
      chosenPane.getViewport().setBackground( Color.white );
      chosenPane.setVerticalScrollBarPolicy(
         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
         .
         .
         .
   }

   /**
    * Action event listener.
    * <p>
    * @param  e  ActionEvent
    */
   public void actionPerformed( ActionEvent e )
   {
      // Get models and command.
      DefaultTableModel originalModel = 
         _originalTable.getModel();
      DefaultTableModel chosenModel = 
         _chosenTable.getModel();
      String command = e.getActionCommand();

      // Add selected objects to the chosen table...
      if( command.equals( _add.getText()))
      {  int[] indices = _originalTable.getSelectedRows();
         Vector v = originalModel.getDataVector();
         for( int i = 0; ( i < indices.length ); i++ )
            chosenModel.addRow(
               ( Vector )v.elementAt( indices[ i ]));
         for( int i = indices.length - 1; ( i >= 0 ); i-- )
            originalModel.removeRow( indices[ i ]);
      }

      // ...or add all objects to the chosen table...
      else if( command.equals( _addAll.getText())) 
      {  Vector v = originalModel.getDataVector(); 
         for( int i = 0; ( i < v.size()); i++ )
            chosenModel.addRow(
               ( Vector )v.elementAt( i ));
         originalModel.setRowCount( 0 );
      }

      // ...or remove selected objects to original table...
      else if( command.equals( _remove.getText()))
      {  int[] indices = _chosenTable.getSelectedRows();
         Vector v = chosenModel.getDataVector();
         for( int i = 0; ( i < indices.length ); i++ )
            originalModel.addRow(
               ( Vector )v.elementAt( indices[ i ]));
         for( int i = indices.length - 1; ( i >= 0 ); i-- )
            chosenModel.removeRow( indices[ i ]);
      }

      // ...or remove all objects to the original table.
      else if( command.equals( _removeAll.getText()))
      {  Vector v = chosenModel.getDataVector();
         for( int i = 0; ( i < v.size()); i++ )
            originalModel.addRow(
               ( Vector )v.elementAt( i ));
         chosenModel.setRowCount( 0 );
      }
   } 
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.