MyIF interface development example - g

By Nicholas Duchon - March 16, 2012, Musings

UML from jGrasp:

Display:

Code:

// File: MyIFg
// Author: Nicholas Duchon
// Date: Feb 19, 2006
// Part g of the interface demonstrations
// Creating the 4-int record class
// added buttons, scrolling text area, button panel, border layout

// NOW - add ArrayList to project
// adding the natural ordering: Comparable interface

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.Random;

import java.util.ArrayList;
import java.util.Collections;

public class MyIFg extends JApplet {
   static Random rn = new Random();
  
   ArrayList <MyRecord> arr = new ArrayList <MyRecord> (100);
  
   MyRecord mr;
   int index = 0;
   private JButton db;
   private DisplayArrayButton dabL = new DisplayArrayButton();
   private JButton nb;
   private NewArrayButton nabL = new NewArrayButton();
   private JButton sb;
   private ShuffleArrayButton sabL = new ShuffleArrayButton();
   private JButton sortOneB;
   private SortOneArrayButton sortOneBL = new SortOneArrayButton();
  
   JScrollPane spTextIO; // scrolling pane for text IO
   JPanel textIObp; // panel for text io control buttons
   JTextArea ioArea;

  
   public void init () {
      setLayout (new BorderLayout ());
      for (int i = 0; i < 100; i++)
         arr.add ( new MyRecord() );
      db = new JButton ("Display Array");
      db.addActionListener (dabL);
      nb = new JButton ("New Array");
      nb.addActionListener (nabL);
      sb = new JButton ("Shuffle Array");
      sb.addActionListener (sabL);
      sortOneB = new JButton ("Sort on one");
      sortOneB.addActionListener (sortOneBL);
     
      ioArea = new JTextArea (10,10);
      ioArea.setEditable (true);
      spTextIO = new JScrollPane (ioArea); // TextArea always get auto scrolls
      textIObp = new JPanel (new GridLayout (0, 4)); // button panel
      textIObp.add(db);
      textIObp.add(nb);
      textIObp.add(sb);
      textIObp.add(sortOneB);

      add (spTextIO, BorderLayout.CENTER);
      add (textIObp, BorderLayout.SOUTH);
     
   } // end method init
  
   public void display () {
      ioArea.setText("");
      for (int i = 0; i < arr.size(); i++)
         ioArea.append(arr.get(i) + "\n");
   } // end method display
  
   class DisplayArrayButton implements ActionListener {
      public void actionPerformed (ActionEvent e) {
         display();
      } // end actionPerformed - required by interface
   } // end SortButtonOne

   class NewArrayButton implements ActionListener {
      public void actionPerformed (ActionEvent e) {
         arr.clear();
         index = 0;
         for (int i = 0; i < 100; i++)
            arr.add ( new MyRecord() );
         display();
      } // end actionPerformed - required by interface
   } // end SortButtonOne

   class ShuffleArrayButton implements ActionListener {
      public void actionPerformed (ActionEvent e) {
         Collections.shuffle(arr);
         display();
      } // end actionPerformed - required by interface
   } // end SortButtonOne

   class SortOneArrayButton implements ActionListener {
      public void actionPerformed (ActionEvent e) {
         Collections.sort (arr);
         display();
      } // end actionPerformed - required by interface
   } // end SortButtonOne

   class MyRecord implements Comparable {
      int one, two = 0, three = 0, four = 0;
      public MyRecord () {
         one = index++;
         two = rn.nextInt(100);
         three = rn.nextInt(100);
         four = rn.nextInt(100);
      } // end no parameter constructor
     
      public int compareTo (Object mr) {
         return one - ((MyRecord) mr).one;
      } // end method compareTo required by Comparable interface
     
      public String toString () {
         return "\t" + one + "\t" + two + "\t" + three + "\t" + four;
      } // end method toString
   } // end class MyRecord

} // end class MyIFc