Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/Box.java
38829 views
/*1* Copyright (c) 1997, 2013, 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*/242526package javax.swing;2728import java.awt.*;29import java.awt.event.*;30import java.beans.ConstructorProperties;31import java.util.Locale;32import java.io.Serializable;33import javax.accessibility.*;3435/**36* A lightweight container37* that uses a BoxLayout object as its layout manager.38* Box provides several class methods39* that are useful for containers using BoxLayout --40* even non-Box containers.41*42* <p>43* The <code>Box</code> class can create several kinds44* of invisible components45* that affect layout:46* glue, struts, and rigid areas.47* If all the components your <code>Box</code> contains48* have a fixed size,49* you might want to use a glue component50* (returned by <code>createGlue</code>)51* to control the components' positions.52* If you need a fixed amount of space between two components,53* try using a strut54* (<code>createHorizontalStrut</code> or <code>createVerticalStrut</code>).55* If you need an invisible component56* that always takes up the same amount of space,57* get it by invoking <code>createRigidArea</code>.58* <p>59* If you are implementing a <code>BoxLayout</code> you60* can find further information and examples in61* <a62href="https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html">How to Use BoxLayout</a>,63* a section in <em>The Java Tutorial.</em>64* <p>65* <strong>Warning:</strong>66* Serialized objects of this class will not be compatible with67* future Swing releases. The current serialization support is68* appropriate for short term storage or RMI between applications running69* the same version of Swing. As of 1.4, support for long term storage70* of all JavaBeans™71* has been added to the <code>java.beans</code> package.72* Please see {@link java.beans.XMLEncoder}.73*74* @see BoxLayout75*76* @author Timothy Prinzing77*/78@SuppressWarnings("serial")79public class Box extends JComponent implements Accessible {8081/**82* Creates a <code>Box</code> that displays its components83* along the the specified axis.84*85* @param axis can be {@link BoxLayout#X_AXIS},86* {@link BoxLayout#Y_AXIS},87* {@link BoxLayout#LINE_AXIS} or88* {@link BoxLayout#PAGE_AXIS}.89* @throws AWTError if the <code>axis</code> is invalid90* @see #createHorizontalBox91* @see #createVerticalBox92*/93public Box(int axis) {94super();95super.setLayout(new BoxLayout(this, axis));96}9798/**99* Creates a <code>Box</code> that displays its components100* from left to right. If you want a <code>Box</code> that101* respects the component orientation you should create the102* <code>Box</code> using the constructor and pass in103* <code>BoxLayout.LINE_AXIS</code>, eg:104* <pre>105* Box lineBox = new Box(BoxLayout.LINE_AXIS);106* </pre>107*108* @return the box109*/110public static Box createHorizontalBox() {111return new Box(BoxLayout.X_AXIS);112}113114/**115* Creates a <code>Box</code> that displays its components116* from top to bottom. If you want a <code>Box</code> that117* respects the component orientation you should create the118* <code>Box</code> using the constructor and pass in119* <code>BoxLayout.PAGE_AXIS</code>, eg:120* <pre>121* Box lineBox = new Box(BoxLayout.PAGE_AXIS);122* </pre>123*124* @return the box125*/126public static Box createVerticalBox() {127return new Box(BoxLayout.Y_AXIS);128}129130/**131* Creates an invisible component that's always the specified size.132* <!-- WHEN WOULD YOU USE THIS AS OPPOSED TO A STRUT? -->133*134* @param d the dimensions of the invisible component135* @return the component136* @see #createGlue137* @see #createHorizontalStrut138* @see #createVerticalStrut139*/140public static Component createRigidArea(Dimension d) {141return new Filler(d, d, d);142}143144/**145* Creates an invisible, fixed-width component.146* In a horizontal box,147* you typically use this method148* to force a certain amount of space between two components.149* In a vertical box,150* you might use this method151* to force the box to be at least the specified width.152* The invisible component has no height153* unless excess space is available,154* in which case it takes its share of available space,155* just like any other component that has no maximum height.156*157* @param width the width of the invisible component, in pixels >= 0158* @return the component159* @see #createVerticalStrut160* @see #createGlue161* @see #createRigidArea162*/163public static Component createHorizontalStrut(int width) {164return new Filler(new Dimension(width,0), new Dimension(width,0),165new Dimension(width, Short.MAX_VALUE));166}167168/**169* Creates an invisible, fixed-height component.170* In a vertical box,171* you typically use this method172* to force a certain amount of space between two components.173* In a horizontal box,174* you might use this method175* to force the box to be at least the specified height.176* The invisible component has no width177* unless excess space is available,178* in which case it takes its share of available space,179* just like any other component that has no maximum width.180*181* @param height the height of the invisible component, in pixels >= 0182* @return the component183* @see #createHorizontalStrut184* @see #createGlue185* @see #createRigidArea186*/187public static Component createVerticalStrut(int height) {188return new Filler(new Dimension(0,height), new Dimension(0,height),189new Dimension(Short.MAX_VALUE, height));190}191192/**193* Creates an invisible "glue" component194* that can be useful in a Box195* whose visible components have a maximum width196* (for a horizontal box)197* or height (for a vertical box).198* You can think of the glue component199* as being a gooey substance200* that expands as much as necessary201* to fill the space between its neighboring components.202*203* <p>204*205* For example, suppose you have206* a horizontal box that contains two fixed-size components.207* If the box gets extra space,208* the fixed-size components won't become larger,209* so where does the extra space go?210* Without glue,211* the extra space goes to the right of the second component.212* If you put glue between the fixed-size components,213* then the extra space goes there.214* If you put glue before the first fixed-size component,215* the extra space goes there,216* and the fixed-size components are shoved against the right217* edge of the box.218* If you put glue before the first fixed-size component219* and after the second fixed-size component,220* the fixed-size components are centered in the box.221*222* <p>223*224* To use glue,225* call <code>Box.createGlue</code>226* and add the returned component to a container.227* The glue component has no minimum or preferred size,228* so it takes no space unless excess space is available.229* If excess space is available,230* then the glue component takes its share of available231* horizontal or vertical space,232* just like any other component that has no maximum width or height.233*234* @return the component235*/236public static Component createGlue() {237return new Filler(new Dimension(0,0), new Dimension(0,0),238new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));239}240241/**242* Creates a horizontal glue component.243*244* @return the component245*/246public static Component createHorizontalGlue() {247return new Filler(new Dimension(0,0), new Dimension(0,0),248new Dimension(Short.MAX_VALUE, 0));249}250251/**252* Creates a vertical glue component.253*254* @return the component255*/256public static Component createVerticalGlue() {257return new Filler(new Dimension(0,0), new Dimension(0,0),258new Dimension(0, Short.MAX_VALUE));259}260261/**262* Throws an AWTError, since a Box can use only a BoxLayout.263*264* @param l the layout manager to use265*/266public void setLayout(LayoutManager l) {267throw new AWTError("Illegal request");268}269270/**271* Paints this <code>Box</code>. If this <code>Box</code> has a UI this272* method invokes super's implementation, otherwise if this273* <code>Box</code> is opaque the <code>Graphics</code> is filled274* using the background.275*276* @param g the <code>Graphics</code> to paint to277* @throws NullPointerException if <code>g</code> is null278* @since 1.6279*/280protected void paintComponent(Graphics g) {281if (ui != null) {282// On the off chance some one created a UI, honor it283super.paintComponent(g);284} else if (isOpaque()) {285g.setColor(getBackground());286g.fillRect(0, 0, getWidth(), getHeight());287}288}289290291/**292* An implementation of a lightweight component that participates in293* layout but has no view.294* <p>295* <strong>Warning:</strong>296* Serialized objects of this class will not be compatible with297* future Swing releases. The current serialization support is298* appropriate for short term storage or RMI between applications running299* the same version of Swing. As of 1.4, support for long term storage300* of all JavaBeans™301* has been added to the <code>java.beans</code> package.302* Please see {@link java.beans.XMLEncoder}.303*/304@SuppressWarnings("serial")305public static class Filler extends JComponent implements Accessible {306307/**308* Constructor to create shape with the given size ranges.309*310* @param min Minimum size311* @param pref Preferred size312* @param max Maximum size313*/314@ConstructorProperties({"minimumSize", "preferredSize", "maximumSize"})315public Filler(Dimension min, Dimension pref, Dimension max) {316setMinimumSize(min);317setPreferredSize(pref);318setMaximumSize(max);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 {386// AccessibleContext methods387//388/**389* Gets the role of this object.390*391* @return an instance of AccessibleRole describing the role of392* the object (AccessibleRole.FILLER)393* @see AccessibleRole394*/395public AccessibleRole getAccessibleRole() {396return AccessibleRole.FILLER;397}398}399}400401/////////////////402// Accessibility support for Box403////////////////404405/**406* Gets the AccessibleContext associated with this Box.407* For boxes, the AccessibleContext takes the form of an408* AccessibleBox.409* A new AccessibleAWTBox instance is created if necessary.410*411* @return an AccessibleBox that serves as the412* AccessibleContext of this Box413*/414public AccessibleContext getAccessibleContext() {415if (accessibleContext == null) {416accessibleContext = new AccessibleBox();417}418return accessibleContext;419}420421/**422* This class implements accessibility support for the423* <code>Box</code> class.424*/425@SuppressWarnings("serial")426protected class AccessibleBox extends AccessibleAWTContainer {427// AccessibleContext methods428//429/**430* Gets the role of this object.431*432* @return an instance of AccessibleRole describing the role of the433* object (AccessibleRole.FILLER)434* @see AccessibleRole435*/436public AccessibleRole getAccessibleRole() {437return AccessibleRole.FILLER;438}439} // inner class AccessibleBox440}441442443