944,119 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3866
  • Java RSS
Aug 31st, 2005
0

A question on an error in a SpringLayout example program

Expand Post »
Hi again.I was just wondering about different layouts I can use with swing and I just came accross a sample program at http://java.sun.com/docs/books/tutor...ut/spring.html anyways there was this example program that I just couldn't find the error.Anyhelp would be welcomed.

The error it is giving is cannot find symbol variable springutilities
Java Syntax (Toggle Plain Text)
  1.  
  2. /*
  3.  * A 1.4 application that uses SpringLayout to create a forms-type layout.
  4.  * Other files required: SpringUtilities.java.
  5.  */
  6.  
  7. import javax.swing.*;
  8. import java.awt.*;
  9.  
  10. public class SpringForm {
  11. /**
  12. * Create the GUI and show it. For thread safety,
  13. * this method should be invoked from the
  14. * event-dispatching thread.
  15. */
  16. private static void createAndShowGUI() {
  17. String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};
  18. int numPairs = labels.length;
  19.  
  20. //Create and populate the panel.
  21. JPanel p = new JPanel(new SpringLayout());
  22. for (int i = 0; i < numPairs; i++) {
  23. JLabel l = new JLabel(labels[i], JLabel.TRAILING);
  24. p.add(l);
  25. JTextField textField = new JTextField(10);
  26. l.setLabelFor(textField);
  27. p.add(textField);
  28. }
  29.  
  30. //Lay out the panel.
  31. SpringUtilities.makeCompactGrid(p,
  32. numPairs, 2, //rows, cols
  33. 6, 6, //initX, initY
  34. 6, 6); //xPad, yPad
  35.  
  36. //Make sure we have nice window decorations.
  37. JFrame.setDefaultLookAndFeelDecorated(true);
  38.  
  39. //Create and set up the window.
  40. JFrame frame = new JFrame("SpringForm");
  41. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.  
  43. //Set up the content pane.
  44. p.setOpaque(true); //content panes must be opaque
  45. frame.setContentPane(p);
  46.  
  47. //Display the window.
  48. frame.pack();
  49. frame.setVisible(true);
  50. }
  51.  
  52. public static void main(String[] args) {
  53. //Schedule a job for the event-dispatching thread:
  54. //creating and showing this application's GUI.
  55. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  56. public void run() {
  57. createAndShowGUI();
  58. }
  59. });
  60. }
  61. }
Reputation Points: 11
Solved Threads: 0
Junior Poster
johnroach1985 is offline Offline
135 posts
since Apr 2004
Aug 31st, 2005
0

Re: A question on an error in a SpringLayout example program

SORRY OK I FOUND OUT MY MISTAKE!!!I just forgot to compile the other code they gave out which is below.You first have to compile this to compile the one above.Sorry...


The code is a following------>

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import javax.swing.SpringLayout;
  3. import java.awt.*;
  4.  
  5. /**
  6.  * A 1.4 file that provides utility methods for
  7.  * creating form- or grid-style layouts with SpringLayout.
  8.  * These utilities are used by several programs, such as
  9.  * SpringBox and SpringCompactGrid.
  10.  */
  11. public class SpringUtilities {
  12. /**
  13.   * A debugging utility that prints to stdout the component's
  14.   * minimum, preferred, and maximum sizes.
  15.   */
  16. public static void printSizes(Component c) {
  17. System.out.println("minimumSize = " + c.getMinimumSize());
  18. System.out.println("preferredSize = " + c.getPreferredSize());
  19. System.out.println("maximumSize = " + c.getMaximumSize());
  20. }
  21.  
  22. /**
  23.   * Aligns the first <code>rows</code> * <code>cols</code>
  24.   * components of <code>parent</code> in
  25.   * a grid. Each component is as big as the maximum
  26.   * preferred width and height of the components.
  27.   * The parent is made just big enough to fit them all.
  28.   *
  29.   * @param rows number of rows
  30.   * @param cols number of columns
  31.   * @param initialX x location to start the grid at
  32.   * @param initialY y location to start the grid at
  33.   * @param xPad x padding between cells
  34.   * @param yPad y padding between cells
  35.   */
  36. public static void makeGrid(Container parent,
  37. int rows, int cols,
  38. int initialX, int initialY,
  39. int xPad, int yPad) {
  40. SpringLayout layout;
  41. try {
  42. layout = (SpringLayout)parent.getLayout();
  43. } catch (ClassCastException exc) {
  44. System.err.println("The first argument to makeGrid must use SpringLayout.");
  45. return;
  46. }
  47.  
  48. Spring xPadSpring = Spring.constant(xPad);
  49. Spring yPadSpring = Spring.constant(yPad);
  50. Spring initialXSpring = Spring.constant(initialX);
  51. Spring initialYSpring = Spring.constant(initialY);
  52. int max = rows * cols;
  53.  
  54. //Calculate Springs that are the max of the width/height so that all
  55. //cells have the same size.
  56. Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
  57. getWidth();
  58. Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
  59. getWidth();
  60. for (int i = 1; i < max; i++) {
  61. SpringLayout.Constraints cons = layout.getConstraints(
  62. parent.getComponent(i));
  63.  
  64. maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
  65. maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
  66. }
  67.  
  68. //Apply the new width/height Spring. This forces all the
  69. //components to have the same size.
  70. for (int i = 0; i < max; i++) {
  71. SpringLayout.Constraints cons = layout.getConstraints(
  72. parent.getComponent(i));
  73.  
  74. cons.setWidth(maxWidthSpring);
  75. cons.setHeight(maxHeightSpring);
  76. }
  77.  
  78. //Then adjust the x/y constraints of all the cells so that they
  79. //are aligned in a grid.
  80. SpringLayout.Constraints lastCons = null;
  81. SpringLayout.Constraints lastRowCons = null;
  82. for (int i = 0; i < max; i++) {
  83. SpringLayout.Constraints cons = layout.getConstraints(
  84. parent.getComponent(i));
  85. if (i % cols == 0) { //start of new row
  86. lastRowCons = lastCons;
  87. cons.setX(initialXSpring);
  88. } else { //x position depends on previous component
  89. cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
  90. xPadSpring));
  91. }
  92.  
  93. if (i / cols == 0) { //first row
  94. cons.setY(initialYSpring);
  95. } else { //y position depends on previous row
  96. cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
  97. yPadSpring));
  98. }
  99. lastCons = cons;
  100. }
  101.  
  102. //Set the parent's size.
  103. SpringLayout.Constraints pCons = layout.getConstraints(parent);
  104. pCons.setConstraint(SpringLayout.SOUTH,
  105. Spring.sum(
  106. Spring.constant(yPad),
  107. lastCons.getConstraint(SpringLayout.SOUTH)));
  108. pCons.setConstraint(SpringLayout.EAST,
  109. Spring.sum(
  110. Spring.constant(xPad),
  111. lastCons.getConstraint(SpringLayout.EAST)));
  112. }
  113.  
  114. /* Used by makeCompactGrid. */
  115. private static SpringLayout.Constraints getConstraintsForCell(
  116. int row, int col,
  117. Container parent,
  118. int cols) {
  119. SpringLayout layout = (SpringLayout) parent.getLayout();
  120. Component c = parent.getComponent(row * cols + col);
  121. return layout.getConstraints(c);
  122. }
  123.  
  124. /**
  125.   * Aligns the first <code>rows</code> * <code>cols</code>
  126.   * components of <code>parent</code> in
  127.   * a grid. Each component in a column is as wide as the maximum
  128.   * preferred width of the components in that column;
  129.   * height is similarly determined for each row.
  130.   * The parent is made just big enough to fit them all.
  131.   *
  132.   * @param rows number of rows
  133.   * @param cols number of columns
  134.   * @param initialX x location to start the grid at
  135.   * @param initialY y location to start the grid at
  136.   * @param xPad x padding between cells
  137.   * @param yPad y padding between cells
  138.   */
  139. public static void makeCompactGrid(Container parent,
  140. int rows, int cols,
  141. int initialX, int initialY,
  142. int xPad, int yPad) {
  143. SpringLayout layout;
  144. try {
  145. layout = (SpringLayout)parent.getLayout();
  146. } catch (ClassCastException exc) {
  147. System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
  148. return;
  149. }
  150.  
  151. //Align all cells in each column and make them the same width.
  152. Spring x = Spring.constant(initialX);
  153. for (int c = 0; c < cols; c++) {
  154. Spring width = Spring.constant(0);
  155. for (int r = 0; r < rows; r++) {
  156. width = Spring.max(width,
  157. getConstraintsForCell(r, c, parent, cols).
  158. getWidth());
  159. }
  160. for (int r = 0; r < rows; r++) {
  161. SpringLayout.Constraints constraints =
  162. getConstraintsForCell(r, c, parent, cols);
  163. constraints.setX(x);
  164. constraints.setWidth(width);
  165. }
  166. x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
  167. }
  168.  
  169. //Align all cells in each row and make them the same height.
  170. Spring y = Spring.constant(initialY);
  171. for (int r = 0; r < rows; r++) {
  172. Spring height = Spring.constant(0);
  173. for (int c = 0; c < cols; c++) {
  174. height = Spring.max(height,
  175. getConstraintsForCell(r, c, parent, cols).
  176. getHeight());
  177. }
  178. for (int c = 0; c < cols; c++) {
  179. SpringLayout.Constraints constraints =
  180. getConstraintsForCell(r, c, parent, cols);
  181. constraints.setY(y);
  182. constraints.setHeight(height);
  183. }
  184. y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
  185. }
  186.  
  187. //Set the parent's size.
  188. SpringLayout.Constraints pCons = layout.getConstraints(parent);
  189. pCons.setConstraint(SpringLayout.SOUTH, y);
  190. pCons.setConstraint(SpringLayout.EAST, x);
  191. }
  192. }
Reputation Points: 11
Solved Threads: 0
Junior Poster
johnroach1985 is offline Offline
135 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: need an answer please
Next Thread in Java Forum Timeline: Trying to open explorer inside my java window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC