FileSelecter.java



package xmlshare;

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

/** select a file */
public class FileSelecter extends JPanel {

  /** JList object */
    protected JList list;

  /** JScrollPane object */
    protected JScrollPane pane;

  /** JButton object */
    protected JButton load;

  /** parent to process actionPerformed() */
    private ActionListener parent;

  /** constructor */
    public FileSelecter( String[] names,
                         ActionListener parent ) {
        this.parent = parent;
        setLayout( new BorderLayout() );
        list = new JList( names );
        list.setVisibleRowCount( 6 );
        pane = new JScrollPane();
        pane.getViewport().setView( list );
        add( pane, "Center" );
        load = new JButton( "Down Load" );
        add( load, "South" );

        MyAdapter adapter = new MyAdapter();
        load.addActionListener( adapter );
    }

  /** public methods to get selectied file name */
    public String getSelectedFileName() {

        String name = (String)list.getSelectedValue();
        return name;
    }

  /** inner class for event handling */
    class MyAdapter implements ActionListener {

         public void actionPerformed( ActionEvent evt ) {

             parent.actionPerformed( evt );
         }
    }

  /** start here (for test) */
    public static void main( String argv[] ) {

        FileSelecter test = new FileSelecter( argv, null );
        JFrame frame = new JFrame( "Selectable File List" );
        frame.getContentPane().setLayout( new BorderLayout() );
        frame.getContentPane().add( test, "Center" );
        frame.pack();
        frame.show();
    }
}