Path: blob/master/src/java.desktop/share/classes/java/awt/BorderLayout.java
41152 views
/*1* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.io.Serial;2829/**30* A border layout lays out a container, arranging and resizing31* its components to fit in five regions:32* north, south, east, west, and center.33* Each region may contain no more than one component, and34* is identified by a corresponding constant:35* {@code NORTH}, {@code SOUTH}, {@code EAST},36* {@code WEST}, and {@code CENTER}. When adding a37* component to a container with a border layout, use one of these38* five constants, for example:39* <pre>40* Panel p = new Panel();41* p.setLayout(new BorderLayout());42* p.add(new Button("Okay"), BorderLayout.SOUTH);43* </pre>44* As a convenience, {@code BorderLayout} interprets the45* absence of a string specification the same as the constant46* {@code CENTER}:47* <pre>48* Panel p2 = new Panel();49* p2.setLayout(new BorderLayout());50* p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);51* </pre>52* <p>53* In addition, {@code BorderLayout} supports the relative54* positioning constants, {@code PAGE_START}, {@code PAGE_END},55* {@code LINE_START}, and {@code LINE_END}.56* In a container whose {@code ComponentOrientation} is set to57* {@code ComponentOrientation.LEFT_TO_RIGHT}, these constants map to58* {@code NORTH}, {@code SOUTH}, {@code WEST}, and59* {@code EAST}, respectively.60* <p>61* For compatibility with previous releases, {@code BorderLayout}62* also includes the relative positioning constants {@code BEFORE_FIRST_LINE},63* {@code AFTER_LAST_LINE}, {@code BEFORE_LINE_BEGINS} and64* {@code AFTER_LINE_ENDS}. These are equivalent to65* {@code PAGE_START}, {@code PAGE_END}, {@code LINE_START}66* and {@code LINE_END} respectively. For67* consistency with the relative positioning constants used by other68* components, the latter constants are preferred.69* <p>70* Mixing both absolute and relative positioning constants can lead to71* unpredictable results. If72* you use both types, the relative constants will take precedence.73* For example, if you add components using both the {@code NORTH}74* and {@code PAGE_START} constants in a container whose75* orientation is {@code LEFT_TO_RIGHT}, only the76* {@code PAGE_START} will be laid out.77* <p>78* NOTE: Currently,79* {@code BorderLayout} does not support vertical80* orientations. The {@code isVertical} setting on the container's81* {@code ComponentOrientation} is not respected.82* <p>83* The components are laid out according to their84* preferred sizes and the constraints of the container's size.85* The {@code NORTH} and {@code SOUTH} components may86* be stretched horizontally; the {@code EAST} and87* {@code WEST} components may be stretched vertically;88* the {@code CENTER} component may stretch both horizontally89* and vertically to fill any space left over.90* <p>91* Here is an example of five buttons in an applet laid out using92* the {@code BorderLayout} layout manager:93* <p>94* <img src="doc-files/BorderLayout-1.gif" alt="Diagram of an applet95* demonstrating BorderLayout. Each section of the BorderLayout contains a96* Button corresponding to its position in the layout, one of: North, West,97* Center, East, or South." style="margin: 7px 10px;">98* <p>99* The code for this applet is as follows:100*101* <hr><blockquote><pre>102* import java.awt.*;103* import java.applet.Applet;104*105* public class buttonDir extends Applet {106* public void init() {107* setLayout(new BorderLayout());108* add(new Button("North"), BorderLayout.NORTH);109* add(new Button("South"), BorderLayout.SOUTH);110* add(new Button("East"), BorderLayout.EAST);111* add(new Button("West"), BorderLayout.WEST);112* add(new Button("Center"), BorderLayout.CENTER);113* }114* }115* </pre></blockquote><hr>116*117* @author Arthur van Hoff118* @see java.awt.Container#add(String, Component)119* @see java.awt.ComponentOrientation120* @since 1.0121*/122public class BorderLayout implements LayoutManager2,123java.io.Serializable {124/**125* Constructs a border layout with the horizontal gaps126* between components.127* The horizontal gap is specified by {@code hgap}.128*129* @see #getHgap()130* @see #setHgap(int)131*132* @serial133*/134int hgap;135136/**137* Constructs a border layout with the vertical gaps138* between components.139* The vertical gap is specified by {@code vgap}.140*141* @see #getVgap()142* @see #setVgap(int)143* @serial144*/145int vgap;146147/**148* Constant to specify components location to be the149* north portion of the border layout.150* @serial151* @see #getChild(String, boolean)152* @see #addLayoutComponent153* @see #getLayoutAlignmentX154* @see #getLayoutAlignmentY155* @see #removeLayoutComponent156*/157Component north;158/**159* Constant to specify components location to be the160* west portion of the border layout.161* @serial162* @see #getChild(String, boolean)163* @see #addLayoutComponent164* @see #getLayoutAlignmentX165* @see #getLayoutAlignmentY166* @see #removeLayoutComponent167*/168Component west;169/**170* Constant to specify components location to be the171* east portion of the border layout.172* @serial173* @see #getChild(String, boolean)174* @see #addLayoutComponent175* @see #getLayoutAlignmentX176* @see #getLayoutAlignmentY177* @see #removeLayoutComponent178*/179Component east;180/**181* Constant to specify components location to be the182* south portion of the border layout.183* @serial184* @see #getChild(String, boolean)185* @see #addLayoutComponent186* @see #getLayoutAlignmentX187* @see #getLayoutAlignmentY188* @see #removeLayoutComponent189*/190Component south;191/**192* Constant to specify components location to be the193* center portion of the border layout.194* @serial195* @see #getChild(String, boolean)196* @see #addLayoutComponent197* @see #getLayoutAlignmentX198* @see #getLayoutAlignmentY199* @see #removeLayoutComponent200*/201Component center;202203/**204*205* A relative positioning constant, that can be used instead of206* north, south, east, west or center.207* mixing the two types of constants can lead to unpredictable results. If208* you use both types, the relative constants will take precedence.209* For example, if you add components using both the {@code NORTH}210* and {@code BEFORE_FIRST_LINE} constants in a container whose211* orientation is {@code LEFT_TO_RIGHT}, only the212* {@code BEFORE_FIRST_LINE} will be laid out.213* This will be the same for lastLine, firstItem, lastItem.214* @serial215*/216Component firstLine;217/**218* A relative positioning constant, that can be used instead of219* north, south, east, west or center.220* Please read Description for firstLine.221* @serial222*/223Component lastLine;224/**225* A relative positioning constant, that can be used instead of226* north, south, east, west or center.227* Please read Description for firstLine.228* @serial229*/230Component firstItem;231/**232* A relative positioning constant, that can be used instead of233* north, south, east, west or center.234* Please read Description for firstLine.235* @serial236*/237Component lastItem;238239/**240* The north layout constraint (top of container).241*/242public static final String NORTH = "North";243244/**245* The south layout constraint (bottom of container).246*/247public static final String SOUTH = "South";248249/**250* The east layout constraint (right side of container).251*/252public static final String EAST = "East";253254/**255* The west layout constraint (left side of container).256*/257public static final String WEST = "West";258259/**260* The center layout constraint (middle of container).261*/262public static final String CENTER = "Center";263264/**265* Synonym for PAGE_START. Exists for compatibility with previous266* versions. PAGE_START is preferred.267*268* @see #PAGE_START269* @since 1.2270*/271public static final String BEFORE_FIRST_LINE = "First";272273/**274* Synonym for PAGE_END. Exists for compatibility with previous275* versions. PAGE_END is preferred.276*277* @see #PAGE_END278* @since 1.2279*/280public static final String AFTER_LAST_LINE = "Last";281282/**283* Synonym for LINE_START. Exists for compatibility with previous284* versions. LINE_START is preferred.285*286* @see #LINE_START287* @since 1.2288*/289public static final String BEFORE_LINE_BEGINS = "Before";290291/**292* Synonym for LINE_END. Exists for compatibility with previous293* versions. LINE_END is preferred.294*295* @see #LINE_END296* @since 1.2297*/298public static final String AFTER_LINE_ENDS = "After";299300/**301* The component comes before the first line of the layout's content.302* For Western, left-to-right and top-to-bottom orientations, this is303* equivalent to NORTH.304*305* @see java.awt.Component#getComponentOrientation306* @since 1.4307*/308public static final String PAGE_START = BEFORE_FIRST_LINE;309310/**311* The component comes after the last line of the layout's content.312* For Western, left-to-right and top-to-bottom orientations, this is313* equivalent to SOUTH.314*315* @see java.awt.Component#getComponentOrientation316* @since 1.4317*/318public static final String PAGE_END = AFTER_LAST_LINE;319320/**321* The component goes at the beginning of the line direction for the322* layout. For Western, left-to-right and top-to-bottom orientations,323* this is equivalent to WEST.324*325* @see java.awt.Component#getComponentOrientation326* @since 1.4327*/328public static final String LINE_START = BEFORE_LINE_BEGINS;329330/**331* The component goes at the end of the line direction for the332* layout. For Western, left-to-right and top-to-bottom orientations,333* this is equivalent to EAST.334*335* @see java.awt.Component#getComponentOrientation336* @since 1.4337*/338public static final String LINE_END = AFTER_LINE_ENDS;339340/**341* Use serialVersionUID from JDK 1.1 for interoperability.342*/343@Serial344private static final long serialVersionUID = -8658291919501921765L;345346/**347* Constructs a new border layout with348* no gaps between components.349*/350public BorderLayout() {351this(0, 0);352}353354/**355* Constructs a border layout with the specified gaps356* between components.357* The horizontal gap is specified by {@code hgap}358* and the vertical gap is specified by {@code vgap}.359* @param hgap the horizontal gap.360* @param vgap the vertical gap.361*/362public BorderLayout(int hgap, int vgap) {363this.hgap = hgap;364this.vgap = vgap;365}366367/**368* Returns the horizontal gap between components.369*370* @return the horizontal gap between components371* @since 1.1372*/373public int getHgap() {374return hgap;375}376377/**378* Sets the horizontal gap between components.379*380* @param hgap the horizontal gap between components381* @since 1.1382*/383public void setHgap(int hgap) {384this.hgap = hgap;385}386387/**388* Returns the vertical gap between components.389*390* @return the vertical gap between components391* @since 1.1392*/393public int getVgap() {394return vgap;395}396397/**398* Sets the vertical gap between components.399*400* @param vgap the vertical gap between components401* @since 1.1402*/403public void setVgap(int vgap) {404this.vgap = vgap;405}406407/**408* Adds the specified component to the layout, using the specified409* constraint object. For border layouts, the constraint must be410* one of the following constants: {@code NORTH},411* {@code SOUTH}, {@code EAST},412* {@code WEST}, or {@code CENTER}.413* <p>414* Most applications do not call this method directly. This method415* is called when a component is added to a container using the416* {@code Container.add} method with the same argument types.417* @param comp the component to be added.418* @param constraints an object that specifies how and where419* the component is added to the layout.420* @see java.awt.Container#add(java.awt.Component, java.lang.Object)421* @exception IllegalArgumentException if the constraint object is not422* a string, or if it not one of the five specified constants.423* @since 1.1424*/425public void addLayoutComponent(Component comp, Object constraints) {426synchronized (comp.getTreeLock()) {427if ((constraints == null) || (constraints instanceof String)) {428addLayoutComponent((String)constraints, comp);429} else {430throw new IllegalArgumentException("cannot add to layout: constraint must be a string (or null)");431}432}433}434435/**436* @deprecated replaced by {@code addLayoutComponent(Component, Object)}.437*/438@Deprecated439public void addLayoutComponent(String name, Component comp) {440synchronized (comp.getTreeLock()) {441/* Special case: treat null the same as "Center". */442if (name == null) {443name = "Center";444}445446/* Assign the component to one of the known regions of the layout.447*/448if ("Center".equals(name)) {449center = comp;450} else if ("North".equals(name)) {451north = comp;452} else if ("South".equals(name)) {453south = comp;454} else if ("East".equals(name)) {455east = comp;456} else if ("West".equals(name)) {457west = comp;458} else if (BEFORE_FIRST_LINE.equals(name)) {459firstLine = comp;460} else if (AFTER_LAST_LINE.equals(name)) {461lastLine = comp;462} else if (BEFORE_LINE_BEGINS.equals(name)) {463firstItem = comp;464} else if (AFTER_LINE_ENDS.equals(name)) {465lastItem = comp;466} else {467throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);468}469}470}471472/**473* Removes the specified component from this border layout. This474* method is called when a container calls its {@code remove} or475* {@code removeAll} methods. Most applications do not call this476* method directly.477* @param comp the component to be removed.478* @see java.awt.Container#remove(java.awt.Component)479* @see java.awt.Container#removeAll()480*/481public void removeLayoutComponent(Component comp) {482synchronized (comp.getTreeLock()) {483if (comp == center) {484center = null;485} else if (comp == north) {486north = null;487} else if (comp == south) {488south = null;489} else if (comp == east) {490east = null;491} else if (comp == west) {492west = null;493}494if (comp == firstLine) {495firstLine = null;496} else if (comp == lastLine) {497lastLine = null;498} else if (comp == firstItem) {499firstItem = null;500} else if (comp == lastItem) {501lastItem = null;502}503}504}505506/**507* Gets the component that was added using the given constraint508*509* @param constraints the desired constraint, one of {@code CENTER},510* {@code NORTH}, {@code SOUTH},511* {@code WEST}, {@code EAST},512* {@code PAGE_START}, {@code PAGE_END},513* {@code LINE_START}, {@code LINE_END}514* @return the component at the given location, or {@code null} if515* the location is empty516* @exception IllegalArgumentException if the constraint object is517* not one of the nine specified constants518* @see #addLayoutComponent(java.awt.Component, java.lang.Object)519* @since 1.5520*/521public Component getLayoutComponent(Object constraints) {522if (CENTER.equals(constraints)) {523return center;524} else if (NORTH.equals(constraints)) {525return north;526} else if (SOUTH.equals(constraints)) {527return south;528} else if (WEST.equals(constraints)) {529return west;530} else if (EAST.equals(constraints)) {531return east;532} else if (PAGE_START.equals(constraints)) {533return firstLine;534} else if (PAGE_END.equals(constraints)) {535return lastLine;536} else if (LINE_START.equals(constraints)) {537return firstItem;538} else if (LINE_END.equals(constraints)) {539return lastItem;540} else {541throw new IllegalArgumentException("cannot get component: unknown constraint: " + constraints);542}543}544545546/**547* Returns the component that corresponds to the given constraint location548* based on the target {@code Container}'s component orientation.549* Components added with the relative constraints {@code PAGE_START},550* {@code PAGE_END}, {@code LINE_START}, and {@code LINE_END}551* take precedence over components added with the explicit constraints552* {@code NORTH}, {@code SOUTH}, {@code WEST}, and {@code EAST}.553* The {@code Container}'s component orientation is used to determine the location of components554* added with {@code LINE_START} and {@code LINE_END}.555*556* @param constraints the desired absolute position, one of {@code CENTER},557* {@code NORTH}, {@code SOUTH},558* {@code EAST}, {@code WEST}559* @param target the {@code Container} used to obtain560* the constraint location based on the target561* {@code Container}'s component orientation.562* @return the component at the given location, or {@code null} if563* the location is empty564* @exception IllegalArgumentException if the constraint object is565* not one of the five specified constants566* @exception NullPointerException if the target parameter is null567* @see #addLayoutComponent(java.awt.Component, java.lang.Object)568* @since 1.5569*/570public Component getLayoutComponent(Container target, Object constraints) {571boolean ltr = target.getComponentOrientation().isLeftToRight();572Component result = null;573574if (NORTH.equals(constraints)) {575result = (firstLine != null) ? firstLine : north;576} else if (SOUTH.equals(constraints)) {577result = (lastLine != null) ? lastLine : south;578} else if (WEST.equals(constraints)) {579result = ltr ? firstItem : lastItem;580if (result == null) {581result = west;582}583} else if (EAST.equals(constraints)) {584result = ltr ? lastItem : firstItem;585if (result == null) {586result = east;587}588} else if (CENTER.equals(constraints)) {589result = center;590} else {591throw new IllegalArgumentException("cannot get component: invalid constraint: " + constraints);592}593594return result;595}596597598/**599* Gets the constraints for the specified component600*601* @param comp the component to be queried602* @return the constraint for the specified component,603* or null if component is null or is not present604* in this layout605* @see #addLayoutComponent(java.awt.Component, java.lang.Object)606* @since 1.5607*/608public Object getConstraints(Component comp) {609//fix for 6242148 : API method java.awt.BorderLayout.getConstraints(null) should return null610if (comp == null){611return null;612}613if (comp == center) {614return CENTER;615} else if (comp == north) {616return NORTH;617} else if (comp == south) {618return SOUTH;619} else if (comp == west) {620return WEST;621} else if (comp == east) {622return EAST;623} else if (comp == firstLine) {624return PAGE_START;625} else if (comp == lastLine) {626return PAGE_END;627} else if (comp == firstItem) {628return LINE_START;629} else if (comp == lastItem) {630return LINE_END;631}632return null;633}634635/**636* Determines the minimum size of the {@code target} container637* using this layout manager.638* <p>639* This method is called when a container calls its640* {@code getMinimumSize} method. Most applications do not call641* this method directly.642* @param target the container in which to do the layout.643* @return the minimum dimensions needed to lay out the subcomponents644* of the specified container.645* @see java.awt.Container646* @see java.awt.BorderLayout#preferredLayoutSize647* @see java.awt.Container#getMinimumSize()648*/649public Dimension minimumLayoutSize(Container target) {650synchronized (target.getTreeLock()) {651Dimension dim = new Dimension(0, 0);652653boolean ltr = target.getComponentOrientation().isLeftToRight();654Component c = null;655656if ((c=getChild(EAST,ltr)) != null) {657Dimension d = c.getMinimumSize();658dim.width += d.width + hgap;659dim.height = Math.max(d.height, dim.height);660}661if ((c=getChild(WEST,ltr)) != null) {662Dimension d = c.getMinimumSize();663dim.width += d.width + hgap;664dim.height = Math.max(d.height, dim.height);665}666if ((c=getChild(CENTER,ltr)) != null) {667Dimension d = c.getMinimumSize();668dim.width += d.width;669dim.height = Math.max(d.height, dim.height);670}671if ((c=getChild(NORTH,ltr)) != null) {672Dimension d = c.getMinimumSize();673dim.width = Math.max(d.width, dim.width);674dim.height += d.height + vgap;675}676if ((c=getChild(SOUTH,ltr)) != null) {677Dimension d = c.getMinimumSize();678dim.width = Math.max(d.width, dim.width);679dim.height += d.height + vgap;680}681682Insets insets = target.getInsets();683dim.width += insets.left + insets.right;684dim.height += insets.top + insets.bottom;685686return dim;687}688}689690/**691* Determines the preferred size of the {@code target}692* container using this layout manager, based on the components693* in the container.694* <p>695* Most applications do not call this method directly. This method696* is called when a container calls its {@code getPreferredSize}697* method.698* @param target the container in which to do the layout.699* @return the preferred dimensions to lay out the subcomponents700* of the specified container.701* @see java.awt.Container702* @see java.awt.BorderLayout#minimumLayoutSize703* @see java.awt.Container#getPreferredSize()704*/705public Dimension preferredLayoutSize(Container target) {706synchronized (target.getTreeLock()) {707Dimension dim = new Dimension(0, 0);708709boolean ltr = target.getComponentOrientation().isLeftToRight();710Component c = null;711712if ((c=getChild(EAST,ltr)) != null) {713Dimension d = c.getPreferredSize();714dim.width += d.width + hgap;715dim.height = Math.max(d.height, dim.height);716}717if ((c=getChild(WEST,ltr)) != null) {718Dimension d = c.getPreferredSize();719dim.width += d.width + hgap;720dim.height = Math.max(d.height, dim.height);721}722if ((c=getChild(CENTER,ltr)) != null) {723Dimension d = c.getPreferredSize();724dim.width += d.width;725dim.height = Math.max(d.height, dim.height);726}727if ((c=getChild(NORTH,ltr)) != null) {728Dimension d = c.getPreferredSize();729dim.width = Math.max(d.width, dim.width);730dim.height += d.height + vgap;731}732if ((c=getChild(SOUTH,ltr)) != null) {733Dimension d = c.getPreferredSize();734dim.width = Math.max(d.width, dim.width);735dim.height += d.height + vgap;736}737738Insets insets = target.getInsets();739dim.width += insets.left + insets.right;740dim.height += insets.top + insets.bottom;741742return dim;743}744}745746/**747* Returns the maximum dimensions for this layout given the components748* in the specified target container.749* @param target the component which needs to be laid out750* @see Container751* @see #minimumLayoutSize752* @see #preferredLayoutSize753*/754public Dimension maximumLayoutSize(Container target) {755return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);756}757758/**759* Returns the alignment along the x axis. This specifies how760* the component would like to be aligned relative to other761* components. The value should be a number between 0 and 1762* where 0 represents alignment along the origin, 1 is aligned763* the furthest away from the origin, 0.5 is centered, etc.764*/765public float getLayoutAlignmentX(Container parent) {766return 0.5f;767}768769/**770* Returns the alignment along the y axis. This specifies how771* the component would like to be aligned relative to other772* components. The value should be a number between 0 and 1773* where 0 represents alignment along the origin, 1 is aligned774* the furthest away from the origin, 0.5 is centered, etc.775*/776public float getLayoutAlignmentY(Container parent) {777return 0.5f;778}779780/**781* Invalidates the layout, indicating that if the layout manager782* has cached information it should be discarded.783*/784public void invalidateLayout(Container target) {785}786787/**788* Lays out the container argument using this border layout.789* <p>790* This method actually reshapes the components in the specified791* container in order to satisfy the constraints of this792* {@code BorderLayout} object. The {@code NORTH}793* and {@code SOUTH} components, if any, are placed at794* the top and bottom of the container, respectively. The795* {@code WEST} and {@code EAST} components are796* then placed on the left and right, respectively. Finally,797* the {@code CENTER} object is placed in any remaining798* space in the middle.799* <p>800* Most applications do not call this method directly. This method801* is called when a container calls its {@code doLayout} method.802* @param target the container in which to do the layout.803* @see java.awt.Container804* @see java.awt.Container#doLayout()805*/806public void layoutContainer(Container target) {807synchronized (target.getTreeLock()) {808Insets insets = target.getInsets();809int top = insets.top;810int bottom = target.height - insets.bottom;811int left = insets.left;812int right = target.width - insets.right;813814boolean ltr = target.getComponentOrientation().isLeftToRight();815Component c = null;816817if ((c=getChild(NORTH,ltr)) != null) {818c.setSize(right - left, c.height);819Dimension d = c.getPreferredSize();820c.setBounds(left, top, right - left, d.height);821top += d.height + vgap;822}823if ((c=getChild(SOUTH,ltr)) != null) {824c.setSize(right - left, c.height);825Dimension d = c.getPreferredSize();826c.setBounds(left, bottom - d.height, right - left, d.height);827bottom -= d.height + vgap;828}829if ((c=getChild(EAST,ltr)) != null) {830c.setSize(c.width, bottom - top);831Dimension d = c.getPreferredSize();832c.setBounds(right - d.width, top, d.width, bottom - top);833right -= d.width + hgap;834}835if ((c=getChild(WEST,ltr)) != null) {836c.setSize(c.width, bottom - top);837Dimension d = c.getPreferredSize();838c.setBounds(left, top, d.width, bottom - top);839left += d.width + hgap;840}841if ((c=getChild(CENTER,ltr)) != null) {842c.setBounds(left, top, right - left, bottom - top);843}844}845}846847/**848* Get the component that corresponds to the given constraint location849*850* @param key The desired absolute position,851* either NORTH, SOUTH, EAST, or WEST.852* @param ltr Is the component line direction left-to-right?853*/854private Component getChild(String key, boolean ltr) {855Component result = null;856857if (key == NORTH) {858result = (firstLine != null) ? firstLine : north;859}860else if (key == SOUTH) {861result = (lastLine != null) ? lastLine : south;862}863else if (key == WEST) {864result = ltr ? firstItem : lastItem;865if (result == null) {866result = west;867}868}869else if (key == EAST) {870result = ltr ? lastItem : firstItem;871if (result == null) {872result = east;873}874}875else if (key == CENTER) {876result = center;877}878if (result != null && !result.visible) {879result = null;880}881return result;882}883884/**885* Returns a string representation of the state of this border layout.886* @return a string representation of this border layout.887*/888public String toString() {889return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";890}891}892893894