Path: blob/master/src/java.desktop/share/classes/javax/swing/Box.java
41153 views
/*1* Copyright (c) 1997, 2020, 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*/24package javax.swing;2526import java.awt.*;27import java.beans.JavaBean;28import java.beans.BeanProperty;29import java.beans.ConstructorProperties;30import javax.accessibility.*;3132/**33* A lightweight container34* that uses a BoxLayout object as its layout manager.35* Box provides several class methods36* that are useful for containers using BoxLayout --37* even non-Box containers.38*39* <p>40* The <code>Box</code> class can create several kinds41* of invisible components42* that affect layout:43* glue, struts, and rigid areas.44* If all the components your <code>Box</code> contains45* have a fixed size,46* you might want to use a glue component47* (returned by <code>createGlue</code>)48* to control the components' positions.49* If you need a fixed amount of space between two components,50* try using a strut51* (<code>createHorizontalStrut</code> or <code>createVerticalStrut</code>).52* If you need an invisible component53* that always takes up the same amount of space,54* get it by invoking <code>createRigidArea</code>.55* <p>56* If you are implementing a <code>BoxLayout</code> you57* can find further information and examples in58* <a59href="https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html">How to Use BoxLayout</a>,60* a section in <em>The Java Tutorial.</em>61* <p>62* <strong>Warning:</strong>63* Serialized objects of this class will not be compatible with64* future Swing releases. The current serialization support is65* appropriate for short term storage or RMI between applications running66* the same version of Swing. As of 1.4, support for long term storage67* of all JavaBeans68* has been added to the <code>java.beans</code> package.69* Please see {@link java.beans.XMLEncoder}.70*71* @see BoxLayout72*73* @author Timothy Prinzing74* @since 1.275*/76@JavaBean(defaultProperty = "accessibleContext")77@SuppressWarnings("serial")78public class Box extends JComponent implements Accessible {7980/**81* Creates a <code>Box</code> that displays its components82* along the specified axis.83*84* @param axis can be {@link BoxLayout#X_AXIS},85* {@link BoxLayout#Y_AXIS},86* {@link BoxLayout#LINE_AXIS} or87* {@link BoxLayout#PAGE_AXIS}.88* @throws AWTError if the <code>axis</code> is invalid89* @see #createHorizontalBox90* @see #createVerticalBox91*/92public Box(int axis) {93super();94super.setLayout(new BoxLayout(this, axis));95}9697/**98* Creates a <code>Box</code> that displays its components99* from left to right. If you want a <code>Box</code> that100* respects the component orientation you should create the101* <code>Box</code> using the constructor and pass in102* <code>BoxLayout.LINE_AXIS</code>, eg:103* <pre>104* Box lineBox = new Box(BoxLayout.LINE_AXIS);105* </pre>106*107* @return the box108*/109public static Box createHorizontalBox() {110return new Box(BoxLayout.X_AXIS);111}112113/**114* Creates a <code>Box</code> that displays its components115* from top to bottom. If you want a <code>Box</code> that116* respects the component orientation you should create the117* <code>Box</code> using the constructor and pass in118* <code>BoxLayout.PAGE_AXIS</code>, eg:119* <pre>120* Box lineBox = new Box(BoxLayout.PAGE_AXIS);121* </pre>122*123* @return the box124*/125public static Box createVerticalBox() {126return new Box(BoxLayout.Y_AXIS);127}128129/**130* Creates an invisible component that's always the specified size.131* <!-- WHEN WOULD YOU USE THIS AS OPPOSED TO A STRUT? -->132*133* @param d the dimensions of the invisible component134* @return the component135* @see #createGlue136* @see #createHorizontalStrut137* @see #createVerticalStrut138*/139public static Component createRigidArea(Dimension d) {140return new Filler(d, d, d);141}142143/**144* Creates an invisible, fixed-width component.145* In a horizontal box,146* you typically use this method147* to force a certain amount of space between two components.148* In a vertical box,149* you might use this method150* to force the box to be at least the specified width.151* The invisible component has no height152* unless excess space is available,153* in which case it takes its share of available space,154* just like any other component that has no maximum height.155*156* @param width the width of the invisible component, in pixels >= 0157* @return the component158* @see #createVerticalStrut159* @see #createGlue160* @see #createRigidArea161*/162public static Component createHorizontalStrut(int width) {163return new Filler(new Dimension(width,0), new Dimension(width,0),164new Dimension(width, Short.MAX_VALUE));165}166167/**168* Creates an invisible, fixed-height component.169* In a vertical box,170* you typically use this method171* to force a certain amount of space between two components.172* In a horizontal box,173* you might use this method174* to force the box to be at least the specified height.175* The invisible component has no width176* unless excess space is available,177* in which case it takes its share of available space,178* just like any other component that has no maximum width.179*180* @param height the height of the invisible component, in pixels >= 0181* @return the component182* @see #createHorizontalStrut183* @see #createGlue184* @see #createRigidArea185*/186public static Component createVerticalStrut(int height) {187return new Filler(new Dimension(0,height), new Dimension(0,height),188new Dimension(Short.MAX_VALUE, height));189}190191/**192* Creates an invisible "glue" component193* that can be useful in a Box194* whose visible components have a maximum width195* (for a horizontal box)196* or height (for a vertical box).197* You can think of the glue component198* as being a gooey substance199* that expands as much as necessary200* to fill the space between its neighboring components.201*202* <p>203*204* For example, suppose you have205* a horizontal box that contains two fixed-size components.206* If the box gets extra space,207* the fixed-size components won't become larger,208* so where does the extra space go?209* Without glue,210* the extra space goes to the right of the second component.211* If you put glue between the fixed-size components,212* then the extra space goes there.213* If you put glue before the first fixed-size component,214* the extra space goes there,215* and the fixed-size components are shoved against the right216* edge of the box.217* If you put glue before the first fixed-size component218* and after the second fixed-size component,219* the fixed-size components are centered in the box.220*221* <p>222*223* To use glue,224* call <code>Box.createGlue</code>225* and add the returned component to a container.226* The glue component has no minimum or preferred size,227* so it takes no space unless excess space is available.228* If excess space is available,229* then the glue component takes its share of available230* horizontal or vertical space,231* just like any other component that has no maximum width or height.232*233* @return the component234*/235public static Component createGlue() {236return new Filler(new Dimension(0,0), new Dimension(0,0),237new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));238}239240/**241* Creates a horizontal glue component.242*243* @return the component244*/245public static Component createHorizontalGlue() {246return new Filler(new Dimension(0,0), new Dimension(0,0),247new Dimension(Short.MAX_VALUE, 0));248}249250/**251* Creates a vertical glue component.252*253* @return the component254*/255public static Component createVerticalGlue() {256return new Filler(new Dimension(0,0), new Dimension(0,0),257new Dimension(0, Short.MAX_VALUE));258}259260/**261* Throws an AWTError, since a Box can use only a BoxLayout.262*263* @param l the layout manager to use264*/265public void setLayout(LayoutManager l) {266throw new AWTError("Illegal request");267}268269/**270* Paints this <code>Box</code>. If this <code>Box</code> has a UI this271* method invokes super's implementation, otherwise if this272* <code>Box</code> is opaque the <code>Graphics</code> is filled273* using the background.274*275* @param g the <code>Graphics</code> to paint to276* @throws NullPointerException if <code>g</code> is null277* @since 1.6278*/279protected void paintComponent(Graphics g) {280if (ui != null) {281// On the off chance some one created a UI, honor it282super.paintComponent(g);283} else if (isOpaque()) {284g.setColor(getBackground());285g.fillRect(0, 0, getWidth(), getHeight());286}287}288289290/**291* An implementation of a lightweight component that participates in292* layout but has no view.293* <p>294* <strong>Warning:</strong>295* Serialized objects of this class will not be compatible with296* future Swing releases. The current serialization support is297* appropriate for short term storage or RMI between applications running298* the same version of Swing. As of 1.4, support for long term storage299* of all JavaBeans300* has been added to the <code>java.beans</code> package.301* Please see {@link java.beans.XMLEncoder}.302*/303@SuppressWarnings("serial")304public static class Filler extends JComponent implements Accessible {305306/**307* Constructor to create shape with the given size ranges.308*309* @param min Minimum size310* @param pref Preferred size311* @param max Maximum size312*/313@ConstructorProperties({"minimumSize", "preferredSize", "maximumSize"})314public Filler(Dimension min, Dimension pref, Dimension max) {315setMinimumSize(min);316setPreferredSize(pref);317setMaximumSize(max);318setFocusable(false);319}320321/**322* Change the size requests for this shape. An invalidate() is323* propagated upward as a result so that layout will eventually324* happen with using the new sizes.325*326* @param min Value to return for getMinimumSize327* @param pref Value to return for getPreferredSize328* @param max Value to return for getMaximumSize329*/330public void changeShape(Dimension min, Dimension pref, Dimension max) {331setMinimumSize(min);332setPreferredSize(pref);333setMaximumSize(max);334revalidate();335}336337// ---- Component methods ------------------------------------------338339/**340* Paints this <code>Filler</code>. If this341* <code>Filler</code> has a UI this method invokes super's342* implementation, otherwise if this <code>Filler</code> is343* opaque the <code>Graphics</code> is filled using the344* background.345*346* @param g the <code>Graphics</code> to paint to347* @throws NullPointerException if <code>g</code> is null348* @since 1.6349*/350protected void paintComponent(Graphics g) {351if (ui != null) {352// On the off chance some one created a UI, honor it353super.paintComponent(g);354} else if (isOpaque()) {355g.setColor(getBackground());356g.fillRect(0, 0, getWidth(), getHeight());357}358}359360/////////////////361// Accessibility support for Box$Filler362////////////////363364/**365* Gets the AccessibleContext associated with this Box.Filler.366* For box fillers, the AccessibleContext takes the form of an367* AccessibleBoxFiller.368* A new AccessibleAWTBoxFiller instance is created if necessary.369*370* @return an AccessibleBoxFiller that serves as the371* AccessibleContext of this Box.Filler.372*/373public AccessibleContext getAccessibleContext() {374if (accessibleContext == null) {375accessibleContext = new AccessibleBoxFiller();376}377return accessibleContext;378}379380/**381* This class implements accessibility support for the382* <code>Box.Filler</code> class.383*/384@SuppressWarnings("serial")385protected class AccessibleBoxFiller extends AccessibleAWTComponent {386387/**388* Constructs an {@code AccessibleBoxFiller}.389*/390protected AccessibleBoxFiller() {}391392// AccessibleContext methods393//394/**395* Gets the role of this object.396*397* @return an instance of AccessibleRole describing the role of398* the object (AccessibleRole.FILLER)399* @see AccessibleRole400*/401public AccessibleRole getAccessibleRole() {402return AccessibleRole.FILLER;403}404}405}406407/////////////////408// Accessibility support for Box409////////////////410411/**412* Gets the AccessibleContext associated with this Box.413* For boxes, the AccessibleContext takes the form of an414* AccessibleBox.415* A new AccessibleAWTBox instance is created if necessary.416*417* @return an AccessibleBox that serves as the418* AccessibleContext of this Box419*/420@BeanProperty(bound = false)421public AccessibleContext getAccessibleContext() {422if (accessibleContext == null) {423accessibleContext = new AccessibleBox();424}425return accessibleContext;426}427428/**429* This class implements accessibility support for the430* <code>Box</code> class.431*/432@SuppressWarnings("serial")433protected class AccessibleBox extends AccessibleAWTContainer {434435/**436* Constructs an {@code AccessibleBox}.437*/438protected AccessibleBox() {}439440// AccessibleContext methods441//442/**443* Gets the role of this object.444*445* @return an instance of AccessibleRole describing the role of the446* object (AccessibleRole.FILLER)447* @see AccessibleRole448*/449public AccessibleRole getAccessibleRole() {450return AccessibleRole.FILLER;451}452} // inner class AccessibleBox453}454455456