// CollectionsTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class CollectionsTest extends JApplet { private JTextField inputTextField; private JButton iterateButton; private JTextArea originalTextArea; private JTextArea iterateTextArea; private JRadioButton [] typeRadioButtons; private Object[] collectionTypes; //array of Collection(s) private int currentCollectionType=0; public void init() { JLabel inputLabel; JButton clearButton; JButton sortButton; JButton reverseButton; JButton shuffleButton; ButtonGroup myButtonGroup; Container container = getContentPane(); container.setLayout( new FlowLayout() ); inputLabel = new JLabel( "Input a string" ); container.add( inputLabel ); //string entered is displayed in textarea and added to current collection inputTextField = new JTextField( "", 10 ); inputTextField.setFont( new Font( "SansSerif", Font.BOLD, 20) ); container.add( inputTextField ); inputTextField.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { String s = event.getActionCommand(); originalTextArea.append( s + "\n" ); //myTextField.setText( "" ); ((JTextField)(event.getSource())).setText( "" ); //add it to current collection: ((Collection)(collectionTypes[currentCollectionType])).add( s ); } } ); //iterate over current collection and display in textarea iterateButton = new JButton( "Iterate" ); iterateButton.setFont( new Font( "SansSerif", Font.BOLD, 20) ); container.add( iterateButton ); iterateButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { iterateTextArea.setText( "" ); //clear from previous Iterator iter = ((Collection)(collectionTypes[currentCollectionType])).iterator(); while (iter.hasNext()) iterateTextArea.append( iter.next() + "\n" ); //JOptionPane.showMessageDialog( null, "button pressed!" ); } } ); sortButton = new JButton( "Sort" ); sortButton.setFont( new Font( "SansSerif", Font.BOLD, 20) ); container.add( sortButton ); sortButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { if (collectionTypes[currentCollectionType] instanceof java.util.List) Collections.sort((java.util.List)(collectionTypes[currentCollectionType])); else JOptionPane.showMessageDialog( null, "sorry, only Lists can be sorted" ); } } ); shuffleButton = new JButton( "Shuffle" ); shuffleButton.setFont( new Font( "SansSerif", Font.BOLD, 20) ); container.add( shuffleButton ); shuffleButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { if (collectionTypes[currentCollectionType] instanceof java.util.List) Collections.shuffle((java.util.List)(collectionTypes[currentCollectionType])); else JOptionPane.showMessageDialog( null, "sorry, only Lists can be shuffled" ); } } ); reverseButton = new JButton( "Reverse" ); reverseButton.setFont( new Font( "SansSerif", Font.BOLD, 20) ); container.add( reverseButton ); reverseButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { if (collectionTypes[currentCollectionType] instanceof java.util.List) Collections.reverse((java.util.List)(collectionTypes[currentCollectionType])); else JOptionPane.showMessageDialog( null, "sorry, only Lists can be reversed" ); } } ); clearButton = new JButton( "Clear" ); clearButton.setFont( new Font( "SansSerif", Font.BOLD, 20) ); container.add( clearButton ); clearButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { iterateTextArea.setText( "" ); originalTextArea.setText( "" ); ((Collection)collectionTypes[currentCollectionType]).clear(); } } ); String [] collectionNames= {"ArrayList", "LinkedList", "HashSet", "LinkedHashSet", "TreeSet"}; typeRadioButtons = new JRadioButton[collectionNames.length]; TypeRadioButtonHandler typeRadioButtonHandler = new TypeRadioButtonHandler(); myButtonGroup = new ButtonGroup(); for (int i=0; i