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