Source of GridFiller.java


  1: import java.awt.GridLayout;
  2: import javax.swing.JFrame;
  3: import javax.swing.JTextField;

  5: /**
  6:  * A program to explain why the GridLayout in class was behaving so "badly".
  7:  * This program animates adding fields to two windows.  Each window uses a
  8:  * GridLayout, but one tells the GridLayout how many rows and columns to use,
  9:  * while the other just tells it how many columns to use.
 10:  *
 11:  * Turns out, if you say how many rows to use, then the GridLayout IGNORES how
 12:  * many columns you said to use!  If you really want to say how many columns to
 13:  * use, you need to tell the GridLayout to use zero (0) rows.
 14:  *
 15:  * @author Mark Young (A00000000)
 16:  */
 17: public class GridFiller {

 19:     /**
 20:      * @param args the command line arguments
 21:      */
 22:     public static void main(String[] args) {
 23:         printIntroduction();
 24:         animateWindow(new GridLayout(5, 3));
 25:         continueExplanation();
 26:         animateWindow(new GridLayout(0, 3));
 27:         sayHowToQuit();
 28:     }
 29:     
 30:     /**
 31:      * Print the first part of this program's explanation.
 32:      */
 33:     private static void printIntroduction() {
 34:         System.out.println("\n"
 35:                 + "GridLayouts ARE WEIRD.\n\n"
 36:                 + "If you say how many rows (non-zero) you want, "
 37:                 + "then that's how many rows you get. \n"
 38:                 + "It ignores the number of columns you want!\n\n"
 39:                 + "Watch how the GridLayout fills in the fields "
 40:                 + "when we tell it to use five rows and\nthree columns.\n\n"
 41:                 + "It starts with one column, "
 42:                 + "and only adds a second column "
 43:                 + "when the number of fields\ngets up to six.\n\n"
 44:                 + "The third column is only added "
 45:                 + "when we get to eleven fields. "
 46:                 + "A fourth column is\nadded when we get to sixteen fields, "
 47:                 + "and a fifth when we get to twenty-one.\n\n"
 48:                 + "But whenever it adds a column, "
 49:                 + "all the fields get moved "
 50:                 + "so it looks like they've\nbeen filled in "
 51:                 + "row by row.");
 52:     }
 53:     
 54:     /**
 55:      * Print the second part of this program's explanation.
 56:      */
 57:     private static void continueExplanation() {
 58:         System.out.println("\n"
 59:                 + "If you really want the number of columns you say, "
 60:                 + "then you have to set the number\nof rows to zero!\n\n"
 61:                 + "Watch how the number of rows grows "
 62:                 + "as we add fields to this next window.");
 63:     }

 65:     /**
 66:      * Explain how to end the program.
 67:      */
 68:     private static void sayHowToQuit() {
 69:         System.out.println("\n"
 70:                 + "Close either window to end this program.\n"
 71:                 + "...");
 72:     }

 74:     /**
 75:      * Animate adding 25 fields to a window using the given GridLayout.
 76:      * 
 77:      * @param gridLayout the GridLayout to use for this window
 78:      */
 79:     private static void animateWindow(GridLayout gridLayout) {
 80:         // include dimensions of grid layout in window title
 81:         String rowByCol = gridLayout.getRows() + "x" + gridLayout.getColumns();
 82:         JFrame win = new JFrame("Filling in a " + rowByCol + " GridLayout");
 83:         
 84:         // make the program end when EITHER window is closed
 85:         win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 86:         
 87:         // make the window a fair size and put it in the middle of the screen
 88:         win.setSize(600, 400);
 89:         win.setLocationRelativeTo(null);
 90:         
 91:         // apply the layout to this window
 92:         win.setLayout(gridLayout);
 93:         
 94:         // add the 25 fields
 95:         for (int i = 1; i <= 25; ++i) {
 96:             // add the field
 97:             win.add(new JTextField("Field #" + i));

 99:             // refresh the window
100:             win.setVisible(true);
101:             
102:             // wait for one second before adding the next field
103:             pause();
104:         }
105:     }

107:     /**
108:      * Wait for one second....
109:      */
110:     private static void pause() {
111:         try {
112:             Thread.sleep(1000);
113:         } catch (InterruptedException e) {
114:             
115:         }
116:     }

118: }