The Final GUI


The Components

So far, we have learned how to create a window, how to place components in a window, how to position components with layout managers, and how to combine layout managers by organizing components into JPanels. Components provide the visual representation of an application’s data and they provide the user with the means to graphically communicate complex instructions to an application. The only components we have used so far are JButton, JLabel, JTextArea and JPanel, but the Swing API offers a great wealth of components to handle just about any need a program could have. All Swing components except for the top-level Swing containers extend from javax.swing.JComponent which, as you can see from the inheritance tree in figure 12-40, extends from Container. This means, strangely enough, that JButton, JTextField, JLabel, etc. are containers just as much as JPanel is, and can theoretically be used as such. Should we use them as containers? Probably not, since this would be working against their intended purpose. If we need a container to which to add components, we should stick with JPanel. But the more courageous of you may want to try adding a JTextArea to a JButton just to see what would happen. Go ahead, I’ll wait.

image from book

 java.lang.Object    java.awt.Component       java.awt.Container          javax.swing.JComponent

image from book

Figure 12-40: JComponent Inheritance Hierarchy

Brief Descriptions

Tables 12-9 through 12-16 include a brief description of all the components that will be used in this chapter’s final program. It attempts to organize them into functional categories, but it should be noted that there is much overlap of functionality. For example, while some components are listed as containers, all JComponents are actually Contain- ers. As another example, while the JButton and JMenuItem are the only components listed as initiating actions, almost all of the components can and often do initiate actions.

Table 12-9: Top-Level Components For Containing Other Components

Component

Brief Description

JWindow

A window.

JFrame

A window with a title, border, window controls and an optional menubar.

JDialog

A window that appears when necessary to notify a user of something and/or to receive user input.

JOptionPane

Technically, JOptionPane is not a top-level container. It is used to create modal Dialogs.

Table 12-10: Non Top-Level Components For Containing Other Components

Component

Brief Description

JPanel

JPanel is a generic lightweight container that groups components together under the control of its layout manager.

JScrollPane

Contains a component that is typically larger than the display area and provides scrollbars for displaying different portions of the component.

JMenuBar

Displays a list of menus horizontally across the top of a JFrame.

JMenu

A special button type that lives in the menu and displays a list of JMenus or JMenuItems in a pop-up list. It is not actually a container but it functions as one.

Table 12-11: Components that Allow the Selection of a Value from a Discrete Set of Values

Component

Brief Description

JComboBox

A pop-up list that allows a user to choose from a discrete set of options.

JList

Allows a user to select from a number of items.

Table 12-12: Components that Allow the Selection of a Value from a Virtual Continuum of Values

Component

Brief Description

JSlider

Lets the user select a value by sliding a knob.

JColorChooser

Allows a user to select a color and returns the selected color to the program.

Table 12-13: Components that Allow the User to Initiate an Action

Component

Brief Description

JButton

A button.

JMenuItem

A selectable item in a list of items that pops up when a menu is selected.

Table 12-14: Components that Represent a Boolean Value

Component

Brief Description

JToggleButton

A two-state button. It can be either selected or not selected.

JCheckBox

A checkbox. It can be checked (true) or unchecked (false).

JRadioButton

A two-state button. It can be either selected or not selected. Radio buttons are typically arranged in groups where only one radio button can be selected at a time. As such, they allow the user to select an item from a discrete set of options.

Table 12-15: Components for Entering Text

Component

Brief Description

JTextField

A single-line area for displaying and editing text.

JTextArea

A multi-line area for displaying and editing text.

JPasswordField

A single-line area for displaying and editing text where the view indicates something was typed, but does not show the original characters.

Table 12-16: View-Only Components

Component

Brief Description

JLabel

Displays a small amount of text and/or an image.

JToolTip

When the mouse lingers over a component for a short time, this can appear with a short message typically explaining the use of the component. Can be thought of as a pop-up label.

Component, Container, and JComponent Methods

Tables 12-17 through 12-28 list and define the most often used methods available to Component, Container and JComponent, filtering away the more esoteric and advanced methods. Event-related methods were also omitted from the table because they are the subject of the next chapter. As a result, only approximately 25% of Component’s methods, 33% of Container’s methods and 10% of JComponent’s methods are included here. The methods have been grouped into the functional categories of Appearance, Size and Location, Visibility, Containment Hierarchy and Other Properties. Browse through the tables now to become familiar with the available methods.

Component Method Tables

Table 12-17: Appearance-Related Component Methods

Method Name and Purpose

public Color getBackground()

Gets the background color of this component.

public void setBackground(Color)

Sets the background color of this component.

public boolean isBackgroundSet()

Returns whether the background color has been explicitly set for this Component.

public Color getForeground()

Gets the foreground color of this component.

public void setForeground(Color)

Sets the foreground color of this component.

public boolean isForegroundSet()

Returns whether the foreground color has been explicitly set for this Component.

public boolean isOpaque()

Returns true if this component is completely opaque. It returns false by default.

public Cursor getCursor()

Gets the cursor set in the component.

public void setCursor(Cursor)

Sets the cursor image to the specified cursor.

public boolean isCursorSet()

Returns whether the cursor has been explicitly set for this Component.

public Font getFont()

Gets the font of this component.

public void setFont(Font)

Sets the font of this component.

public boolean isFontSet()

Returns whether the font has been explicitly set for this Component.

public void paint(Graphics)

Paints this component.

public void paintAll(Graphics)

Paints this component and all of its subcomponents.

public void repaint()

Repaints this component.

public void repaint(int x, int y, int width, int height)

Repaints the specified rectangle of this component.

Table 12-18: Size- and Location-Related Component Methods

Method Name and Purpose

public Rectangle getBounds()

Gets the bounds of this component in the form of a Rectangle object.

public void setBounds(int x, int y, int width, int height)

Moves and resizes this component.

public void setBounds(Rectangle)

Moves and resizes this component to conform to the specified bounding rectangle.

public Dimension getSize()

Returns the size of this component in the form of a Dimension object.

public void setSize(Dimension)

Resizes this component so that it has the specified dimension.

public void setSize(int width, int height)

Resizes this component so that it has the specified width and height.

public Dimension getMaximumSize()

Gets the maximum size of this component.

public Dimension getMinimumSize()

Gets the minimum size of this component.

public Dimension getPreferredSize()

Gets the preferred size of this component.

public Point getLocation()

Gets the location of the top-left corner of this component.

public void setLocation(int x, int y)

Moves this component to a new location.

public void setLocation(Point p)

Moves this component to a new location.

public Point getLocationOnScreen()

Gets the location of the top-left corner of this component (relative to the screen’s coordinate system).

public int getWidth()

Returns the current width of this component.

public int getHeight()

Returns the current height of this component.

public int getX()

Returns the current x-coordinate of the component’s origin.

public int getY()

Returns the current y-coordinate of the component’s origin.

public boolean contains(int x, int y)

Checks whether this component "contains" the specified point, where x and y are relative to the coordinate system of this component.

Table 12-19: Visibility-Related Component Methods

Method Name and Purpose

public boolean isDisplayable()

Determines whether this component is displayable.

public boolean isShowing()

Determines whether this component is showing on screen.

public boolean isVisible()

Determines whether this component should be visible when its parent is visible.

public void setVisible(boolean b)

Shows or hides this component depending on the value of parameter b.

Table 12-20: Containment Hierarchy-Related Component Methods

Method Name and Purpose

public Container getParent()

Gets the parent of this component.

Table 12-21: Other Property-Related Component Methods

Method Name and Purpose

public String getName()

Gets the name of the component.

public void setName(String name)

Sets the name of the component to the specified string.

public boolean isEnabled()

Determines whether this component is enabled.

public void setEnabled(boolean b)

Enables or disables this component, depending on the value of the parameter.

public boolean isLightweight()

Determines whether or not this component is lightweight. Lightweight components don't have native toolkit peers.

Container Method Tables

Table 12-22: Appearance-Related Container Methods

Method Name and Purpose

public Insets getInsets()

Determines the insets of this container, which indicate the size of the container's border.

public LayoutManager getLayout()

Gets the layout manager for this container.

public void setLayout(LayoutManager mgr)

Sets the layout manager for this container.

public void doLayout()

Causes this container to layout its components.

Table 12-23: Containment Hierarchy-Related Container Methods

Method Name and Purpose

public Component add(Component)

Adds the specified component to the end of this container.

public Component add(Component, int)

Adds the specified component to this container at the given position.

public void add(Component comp, Object constraints)

Adds the specified component to the end of this container with the specified constraints.

public void add(Component comp, Object constraints, int index)

Adds the specified component to this container with the specified constraints at the specified index.

public Component add(String name, Component comp)

Adds the specified component to this container assigning it the specified name.

public int getComponentCount()

Gets the number of components in this container.

public Component[] getComponents()

Gets all the components in this container.

public Component getComponent(int n)

Gets the nth component in this container.

public void remove(Component comp)

Removes the specified component from this container.

public void remove(int index)

Removes the component at the specified index from this container.

public void removeAll()

Removes all the components from this container.

public Component getComponentAt(int x, int y)

Locates the component that contains the specified coordinate relative to the container’s coordinate system.

public Component getComponentAt(Point p)

Gets the component that contains the specified coordinate.

public Component findComponentAt(int x, int y)

Locates the visible child component that contains the specified position.

public Component findComponentAt(Point p)

Locates the visible child component that contains the specified point.

public boolean isAncestorOf(Component c)

Checks if the specified component is contained in the component hierarchy of this container.

Table 12-24: Appearance-Related JComponent Methods

Method Name and Purpose

public void setOpaque(boolean isOpaque)

If true, the component should paint every pixel within its bounds.

public Border getBorder()

Returns the border of this component or null if no border is currently set.

public void setBorder(Border border)

Sets the border of this component.

Table 12-25: Size- and Location-Related JComponent Methods

Method Name and Purpose

public void setMaximumSize(Dimension maximumSize)

Sets the maximum size of this component to the specified Dimension.

public boolean isMaximumSizeSet()

Returns true if the maximum size has been set to a non-null value; otherwise returns false.

public void setMinimumSize(Dimension minimumSize)

Sets the minimum size of this component to the specified Dimension.

public boolean isMinimumSizeSet()

Returns true if the minimum size has been set to a non-null value; otherwise returns false.

public void setPreferredSize(Dimension preferredSize)

Sets the preferred size of this component.

public boolean isPreferredSizeSet()

Returns true if the preferred size has been set to a non-null value; otherwise returns false.

Table 12-26: Visibility-Related JComponent Methods

Method Name and Purpose

public Rectangle getVisibleRect()

Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle and all of its ancestors' visible rectangles.

Table 12-27: Containment Hierarchy-Related JComponent Methods

Method Name and Purpose

public Container getTopLevelAncestor()

Returns the top-level ancestor of this component (either the containing Window or Applet), or null if this component has not been added to any container.

Table 12-28: Other Property-Related JComponent Methods

Other Properties

public String getToolTipText()

Returns the tooltip string that has been set with setToolTipText.

public void setToolTipText(String text)

Registers the text to display in a tooltip.

JComponent Method Tables

Quick Review

Components provide the visual representation of an application’s data and they provide the user with the means to graphically communicate instructions to an application. All Swing components, except for the top-level Swing containers, extend from javax.swing.JComponent. Although all Swing components are technically containers, JPanel is the Swing container to which we should add components.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net