1. /*
  2. * @(#)GridBagConstraints.java 1.20 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. /**
  9. * The <code>GridBagConstraints</code> class specifies constraints
  10. * for components that are laid out using the
  11. * <code>GridBagLayout</code> class.
  12. *
  13. * @version 1.9, 06/16/97
  14. * @author Doug Stein
  15. * @see java.awt.GridBagLayout
  16. * @since JDK1.0
  17. */
  18. public class GridBagConstraints implements Cloneable, java.io.Serializable {
  19. /**
  20. * Specify that this component is the next-to-last component in its
  21. * column or row (<code>gridwidth</code>, <code>gridheight</code>),
  22. * or that this component be placed next to the previously added
  23. * component (<code>gridx</code>, <code>gridy</code>).
  24. * @see java.awt.GridBagConstraints#gridwidth
  25. * @see java.awt.GridBagConstraints#gridheight
  26. * @see java.awt.GridBagConstraints#gridx
  27. * @see java.awt.GridBagConstraints#gridy
  28. */
  29. public static final int RELATIVE = -1;
  30. /**
  31. * Specify that this component is the
  32. * last component in its column or row.
  33. */
  34. public static final int REMAINDER = 0;
  35. /**
  36. * Do not resize the component.
  37. */
  38. public static final int NONE = 0;
  39. /**
  40. * Resize the component both horizontally and vertically.
  41. */
  42. public static final int BOTH = 1;
  43. /**
  44. * Resize the component horizontally but not vertically.
  45. */
  46. public static final int HORIZONTAL = 2;
  47. /**
  48. * Resize the component vertically but not horizontally.
  49. */
  50. public static final int VERTICAL = 3;
  51. /**
  52. * Put the component in the center of its display area.
  53. */
  54. public static final int CENTER = 10;
  55. /**
  56. * Put the component at the top of its display area,
  57. * centered horizontally.
  58. */
  59. public static final int NORTH = 11;
  60. /**
  61. * Put the component at the top-right corner of its display area.
  62. */
  63. public static final int NORTHEAST = 12;
  64. /**
  65. * Put the component on the right side of its display area,
  66. * centered vertically.
  67. */
  68. public static final int EAST = 13;
  69. /**
  70. * Put the component at the bottom-right corner of its display area.
  71. */
  72. public static final int SOUTHEAST = 14;
  73. /**
  74. * Put the component at the bottom of its display area, centered
  75. * horizontally.
  76. */
  77. public static final int SOUTH = 15;
  78. /**
  79. * Put the component at the bottom-left corner of its display area.
  80. */
  81. public static final int SOUTHWEST = 16;
  82. /**
  83. * Put the component on the left side of its display area,
  84. * centered vertically.
  85. */
  86. public static final int WEST = 17;
  87. /**
  88. * Put the component at the top-left corner of its display area.
  89. */
  90. public static final int NORTHWEST = 18;
  91. /**
  92. * Specifies the cell at the left of the component's display area,
  93. * where the leftmost cell has <code>gridx=0</code>. The value
  94. * <code>RELATIVE</code> specifies that the component be placed just
  95. * to the right of the component that was added to the container just
  96. * before this component was added.
  97. * <p>
  98. * The default value is <code>RELATIVE</code>.
  99. * gridx should be a non-negative value.
  100. * @serial
  101. * @see clone()
  102. * @see java.awt.GridBagConstraints#gridy
  103. */
  104. public int gridx;
  105. /**
  106. * Specifies the cell at the top of the component's display area,
  107. * where the topmost cell has <code>gridy=0</code>. The value
  108. * <code>RELATIVE</code> specifies that the component be placed just
  109. * below the component that was added to the container just before
  110. * this component was added.
  111. * <p>
  112. * The default value is <code>RELATIVE</code>.
  113. * gridy should be a non-negative value.
  114. * @serial
  115. * @see clone()
  116. * @see java.awt.GridBagConstraints#gridx
  117. */
  118. public int gridy;
  119. /**
  120. * Specifies the number of cells in a row for the component's
  121. * display area.
  122. * <p>
  123. * Use <code>REMAINDER</code> to specify that the component be the
  124. * last one in its row. Use <code>RELATIVE</code> to specify that the
  125. * component be the next-to-last one in its row.
  126. * <p>
  127. * gridwidth should be non-negative and the default value is 1.
  128. * @serial
  129. * @see clone()
  130. * @see java.awt.GridBagConstraints#gridheight
  131. */
  132. public int gridwidth;
  133. /**
  134. * Specifies the number of cells in a column for the component's
  135. * display area.
  136. * <p>
  137. * Use <code>REMAINDER</code> to specify that the component be the
  138. * last one in its column. Use <code>RELATIVE</code> to specify that
  139. * the component be the next-to-last one in its column.
  140. * <p>
  141. * gridheight should be a non-negative value and the default value is 1.
  142. * @serial
  143. * @see clone()
  144. * @see java.awt.GridBagConstraints#gridwidth
  145. */
  146. public int gridheight;
  147. /**
  148. * Specifies how to distribute extra horizontal space.
  149. * <p>
  150. * The grid bag layout manager calculates the weight of a column to
  151. * be the maximum <code>weighty</code> of all the components in a
  152. * row. If the resulting layout is smaller horizontally than the area
  153. * it needs to fill, the extra space is distributed to each column in
  154. * proportion to its weight. A column that has a weight zero receives no
  155. * extra space.
  156. * <p>
  157. * If all the weights are zero, all the extra space appears between
  158. * the grids of the cell and the left and right edges.
  159. * <p>
  160. * The default value of this field is <code>0</code>.
  161. * weightx should be a non-negative value.
  162. * @serial
  163. * @see clone()
  164. * @see java.awt.GridBagConstraints#weighty
  165. */
  166. public double weightx;
  167. /**
  168. * Specifies how to distribute extra vertical space.
  169. * <p>
  170. * The grid bag layout manager calculates the weight of a row to be
  171. * the maximum <code>weightx</code> of all the components in a row.
  172. * If the resulting layout is smaller vertically than the area it
  173. * needs to fill, the extra space is distributed to each row in
  174. * proportion to its weight. A row that has a weight of zero receives no
  175. * extra space.
  176. * <p>
  177. * If all the weights are zero, all the extra space appears between
  178. * the grids of the cell and the top and bottom edges.
  179. * <p>
  180. * The default value of this field is <code>0</code>.
  181. * weighty should be a non-negative value.
  182. * @serial
  183. * @see clone()
  184. * @see java.awt.GridBagConstraints#weightx
  185. */
  186. public double weighty;
  187. /**
  188. * This field is used when the component is smaller than its display
  189. * area. It determines where, within the display area, to place the
  190. * component. Possible values are <code>CENTER</code>,
  191. * <code>NORTH</code>, <code>NORTHEAST</code>, <code>EAST</code>,
  192. * <code>SOUTHEAST</code>, <code>SOUTH</code>, <code>SOUTHWEST</code>,
  193. * <code>WEST</code>, and <code>NORTHWEST</code>.
  194. * The default value is <code>CENTER</code>.
  195. * @serial
  196. * @see clone()
  197. */
  198. public int anchor;
  199. /**
  200. * This field is used when the component's display area is larger
  201. * than the component's requested size. It determines whether to
  202. * resize the component, and if so, how.
  203. * <p>
  204. * The following values are valid for <code>fill</code>:
  205. * <p>
  206. * <ul>
  207. * <li>
  208. * <code>NONE</code>: Do not resize the component.
  209. * <li>
  210. * <code>HORIZONTAL</code>: Make the component wide enough to fill
  211. * its display area horizontally, but do not change its height.
  212. * <li>
  213. * <code>VERTICAL</code>: Make the component tall enough to fill its
  214. * display area vertically, but do not change its width.
  215. * <li>
  216. * <code>BOTH</code>: Make the component fill its display area
  217. * entirely.
  218. * </ul>
  219. * <p>
  220. * The default value is <code>NONE</code>.
  221. * @serial
  222. * @see clone()
  223. */
  224. public int fill;
  225. /**
  226. * This field specifies the external padding of the component, the
  227. * minimum amount of space between the component and the edges of its
  228. * display area.
  229. * <p>
  230. * The default value is <code>new Insets(0, 0, 0, 0)</code>.
  231. * @serial
  232. * @see clone()
  233. */
  234. public Insets insets;
  235. /**
  236. * This field specifies the internal padding of the component, how much
  237. * space to add to the minimum width of the component. The width of
  238. * the component is at least its minimum width plus
  239. * <code>(ipadx * 2)</code> pixels.
  240. * <p>
  241. * The default value is <code>0</code>.
  242. * @serial
  243. * @see clone()
  244. * @see java.awt.GridBagConstraints#ipady
  245. */
  246. public int ipadx;
  247. /**
  248. * This field specifies the internal padding, that is, how much
  249. * space to add to the minimum height of the component. The height of
  250. * the component is at least its minimum height plus
  251. * <code>(ipady * 2)</code> pixels.
  252. * <p>
  253. * The default value is 0.
  254. * @serial
  255. * @see clone()
  256. * @see java.awt.GridBagConstraints#ipadx
  257. */
  258. public int ipady;
  259. /**
  260. * Temporary place holder for the x coordinate.
  261. * @serial
  262. */
  263. int tempX;
  264. /**
  265. * Temporary place holder for the y coordinate.
  266. * @serial
  267. */
  268. int tempY;
  269. /**
  270. * Temporary place holder for the Width of the component.
  271. * @serial
  272. */
  273. int tempWidth;
  274. /**
  275. * Temporary place holder for the Height of the component.
  276. * @serial
  277. */
  278. int tempHeight;
  279. /**
  280. * The minimum width of the component. It is used to calculate
  281. * <code>ipady</code>, where the default will be 0.
  282. * @serial
  283. * @see ipady
  284. */
  285. int minWidth;
  286. /**
  287. * The minimum height of the component. It is used to calculate
  288. * <code>ipadx</code>, where the default will be 0.
  289. * @serial
  290. * @see ipadx
  291. */
  292. int minHeight;
  293. /*
  294. * JDK 1.1 serialVersionUID
  295. */
  296. private static final long serialVersionUID = -1000070633030801713L;
  297. /**
  298. * Creates a <code>GridBagConstraint</code> object with
  299. * all of its fields set to their default value.
  300. */
  301. public GridBagConstraints () {
  302. gridx = RELATIVE;
  303. gridy = RELATIVE;
  304. gridwidth = 1;
  305. gridheight = 1;
  306. weightx = 0;
  307. weighty = 0;
  308. anchor = CENTER;
  309. fill = NONE;
  310. insets = new Insets(0, 0, 0, 0);
  311. ipadx = 0;
  312. ipady = 0;
  313. }
  314. /**
  315. * Creates a <code>GridBagConstraints</code> object with
  316. * all of its fields set to the passed-in arguments.
  317. *
  318. * Note: Because the use of this constructor hinders readability
  319. * of source code, this constructor should only be used by
  320. * automatic source code generation tools.
  321. *
  322. * @param gridx The initial gridx value.
  323. * @param gridy The initial gridy value.
  324. * @param gridwidth The initial gridwidth value.
  325. * @param gridheight The initial gridheight value.
  326. * @param weightx The initial weightx value.
  327. * @param weighty The initial weighty value.
  328. * @param anchor The initial anchor value.
  329. * @param fill The initial fill value.
  330. * @param insets The initial insets value.
  331. * @param ipadx The initial ipadx value.
  332. * @param ipady The initial ipady value.
  333. *
  334. * @see java.awt.GridBagConstraints#gridx
  335. * @see java.awt.GridBagConstraints#gridy
  336. * @see java.awt.GridBagConstraints#gridwidth
  337. * @see java.awt.GridBagConstraints#gridheight
  338. * @see java.awt.GridBagConstraints#weightx
  339. * @see java.awt.GridBagConstraints#weighty
  340. * @see java.awt.GridBagConstraints#anchor
  341. * @see java.awt.GridBagConstraints#fill
  342. * @see java.awt.GridBagConstraints#insets
  343. * @see java.awt.GridBagConstraints#ipadx
  344. * @see java.awt.GridBagConstraints#ipady
  345. *
  346. * @since JDK1.2
  347. */
  348. public GridBagConstraints(int gridx, int gridy,
  349. int gridwidth, int gridheight,
  350. double weightx, double weighty,
  351. int anchor, int fill,
  352. Insets insets, int ipadx, int ipady) {
  353. this.gridx = gridx;
  354. this.gridy = gridy;
  355. this.gridwidth = gridwidth;
  356. this.gridheight = gridheight;
  357. this.fill = fill;
  358. this.ipadx = ipadx;
  359. this.ipady = ipady;
  360. this.insets = insets;
  361. this.anchor = anchor;
  362. this.weightx = weightx;
  363. this.weighty = weighty;
  364. }
  365. /**
  366. * Creates a copy of this grid bag constraint.
  367. * @return a copy of this grid bag constraint
  368. */
  369. public Object clone () {
  370. try {
  371. GridBagConstraints c = (GridBagConstraints)super.clone();
  372. c.insets = (Insets)insets.clone();
  373. return c;
  374. } catch (CloneNotSupportedException e) {
  375. // this shouldn't happen, since we are Cloneable
  376. throw new InternalError();
  377. }
  378. }
  379. }