001package ca.bc.webarts;
002
003import java.awt.*;
004import java.awt.event.*;
005import javax.swing.*;
006
007
008/**
009 *  Description of the Class
010 *
011 * @author    tgutwin
012 */
013public class TooltipTextOfList
014{
015  /**  Description of the Field */
016  JList list;
017  /**  Description of the Field */
018  JTextField txtItem;
019  /**  Description of the Field */
020  DefaultListModel model;
021  /**  Description of the Field */
022  private JScrollPane scrollpane = null;
023
024
025  /**  Constructor for the TooltipTextOfList object */
026  public TooltipTextOfList()
027  {
028    JFrame frame = new JFrame( "Tooltip Text for List Item" );
029    String[] str_list = {"One", "Two", "Three", "Four"};
030    model = new DefaultListModel();
031    for ( int i = 0; i < str_list.length; i++ )
032    {
033      model.addElement( str_list[i] );
034    }
035    list =
036      new JList( model )
037      {
038        public String getToolTipText( MouseEvent e )
039        {
040          int index = locationToIndex( e.getPoint() );
041          if ( -1 < index )
042          {
043            String item = (String)getModel().getElementAt( index );
044            return item;
045          }
046          else
047          {
048            return null;
049          }
050        }
051      };
052    txtItem = new JTextField( 10 );
053    JButton button = new JButton( "Add" );
054    button.addActionListener( new MyAction() );
055    JPanel panel = new JPanel();
056    panel.add( txtItem );
057    panel.add( button );
058    panel.add( list );
059    frame.add( panel, BorderLayout.CENTER );
060    frame.setSize( 400, 400 );
061    frame.setVisible( true );
062    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
063  }
064
065
066  /**
067   *  The main program for the TooltipTextOfList class
068   *
069   * @param  args  The command line arguments
070   */
071  public static void main( String[] args )
072  {
073    TooltipTextOfList tt = new TooltipTextOfList();
074  }
075
076
077  /**
078   *  Description of the Class
079   *
080   * @author    tgutwin
081   */
082  public class MyAction extends MouseAdapter implements ActionListener
083  {
084    /**
085     *  Description of the Method
086     *
087     * @param  ae  Description of the Parameter
088     */
089    public void actionPerformed( ActionEvent ae )
090    {
091      String data = txtItem.getText();
092      if ( data.equals( "" ) )
093      {
094        JOptionPane.showMessageDialog( null, "Please enter text in the Text Box." );
095      }
096      else
097      {
098        model.addElement( data );
099        JOptionPane.showMessageDialog( null, "Item added successfully." );
100        txtItem.setText( "" );
101      }
102    }
103  }
104}