UNIT-3 Introducing Swing: 4.1 The Origins and Design Philosophy of Swing
UNIT-3 Introducing Swing: 4.1 The Origins and Design Philosophy of Swing
Introducing Swing
4.1 The Origins and Design Philosophy of Swing
The AWT defines a basic set of controls, windows, and dialog boxes that support a usable, but
limited graphical interface. One reason for the limited nature of the AWT is that it translates
its various visual components into their corresponding, platform-specific equivalents, or peers.
This means that the look and feel of a component is defined by the platform, not by Java.
Because the AWT components use native code resources, they are referred to as heavyweight.
The use of native peers led to several problems. First, because of variations between operating
systems, a component might look, or even act, differently on different platforms. This potential
variability threatened the overarching philosophy of Java: write once, run anywhere. Second,
the look and feel of each component was fixed (because it is defined by the platform) and could
not be (easily) changed. Third, the use of heavyweight components caused some frustrating
restrictions.
Two Key Swing Features: Swing was created to address the limitations present in the AWT.
It does this through two key features: lightweight components and a pluggable look and feel.
Together they provide an elegant, yet easy-to-use solution to the problems of the AWT. More
than anything else, it is these two features that define the essence of Swing.
Swing Components Are Lightweight: With very few exceptions, Swing components are
lightweight. This means that they are written entirely in Java and do not map directly to
platform-specific peers. Because lightweight components are rendered using graphics
primitives, they can be transparent, which enables nonrectangular shapes. Thus, lightweight
components are more efficient and more flexible. Furthermore, because lightweight
components do not translate into native peers, the look and feel of each component is
determined by Swing, not by the underlying operating system. This means that each component
will work in a consistent manner across all platforms.
Swing Supports a Pluggable Look and Feel: Swing supports a pluggable look and feel
(PLAF). Because each Swing component is rendered by Java code rather than by native peers,
the look and feel of a component is under the control of Swing. This fact means that it is
possible to separate the look and feel of a component from the logic of the component, and this
is what Swing does. Separating out the look and feel provides a significant advantage: it
becomes possible to change the way that a component is rendered without affecting any of its
other aspects. In other words, it is possible to ―plug in‖ a new look and feel for any given
component without creating any side effects in the code that uses that component. Moreover,
it becomes possible to define entire sets of look-and-feels that represent different GUI styles.
To use a specific style, its look and feel is simply ―plugged in.‖ Once this is done, all
components are automatically rendered using that style. Pluggable look-and-feels offer several
important advantages. It is possible to define a look and feel that is consistent across all
platforms. Conversely, it is possible to create a and feel that acts like a specific platform. For
example, if you know that an application will be running only in a Windows environment, it is
possible to specify the Windows look and feel. It is also possible to design a custom look and
feel. Finally, the look and feel can be changed dynamically at run time.
Java SE 6 provides look-and-feels, such as metal and Motif, that are available to all Swing
users. The metal look and feel is also called the Java look and feel. It is platform-independent
and available in all Java execution environments. It is also the default look and feel. Windows
environments also have access to the Windows and Windows Classic look and feel.
Components: In general, Swing components are derived from the JComponent class.
JComponent provides the functionality that is common to all components. For example,
JComponent supports the pluggable look and feel. JComponent inherits the AWT classes
Container and Component. Thus, a Swing component is built on and compatible with an AWT
component. All of Swing‘s components are represented by classes defined within the package
javax.swing. The following are the class names for Swing components.
Notice that all component classes begin with the letter J. For example, the class for a label is
JLabel; the class for a push button is JButton; and the class for a scroll bar is JScrollBar.
Containers
Swing defines two types of containers. The first are top-level containers: JFrame, JApplet,
JWindow, and JDialog. These containers do not inherit JComponent. They do, however, inherit
the AWT classes Component and Container. Unlike Swing‘s other components, which are
lightweight, the top-level containers are heavyweight. This makes the top-level containers a
special case in the Swing component library.
As the name implies, a top-level container must be at the top of a containment hierarchy. A
top-level container is not contained within any other container. Furthermore, every
containment hierarchy must begin with a top-level container. The one most commonly used
for applications is JFrame. The one used for applets is JApplet. The second type of containers
supported by Swing are lightweight containers. Lightweight containers do inherit JComponent.
An example of a lightweight container is JPanel, which is a general-purpose container.
Lightweight containers are often used to organize and manage groups of related components
because a lightweight container can be contained within another container. Thus, you can use
lightweight containers such as JPanel to create subgroups of related controls that are contained
within an outer container.
Each top-level container defines a set of panes. At the top of the hierarchy is an instance of
JRootPane. JRootPane is a lightweight container whose purpose is to manage the other panes.
It also helps manage the optional menu bar. The panes that comprise the root pane are called
the glass pane, the content pane, and the layered pane. The glass pane is the top-level pane. It
sits above and completely covers all other panes. By default, it is a transparent instance of
JPanel. The glass pane enables you to manage mouse events that affect the entire container
(rather than an individual control) or to paint over any other component, for example. In most
cases, you won‘t need to use the glass pane directly, but it is there if you need it.
Each Container object has a layout manager associated with it. A layout manager is an instance
of any class that implements the LayoutManager interface. The layout manager is set by the
setLayout( ) method. If no call to setLayout( ) is made, then the default layout manager is used.
Whenever a container is resized (or sized for the first time), the layout manager is used to
position each of the components within it. The setLayout( ) method has the following general
form:
By default, a JList allows the user to select multiple ranges of items within the list, but you can
change this behavior by calling setSelectionMode( ), which is defined by JList. It is shown
here:
void setSelectionMode(int mode)
Here, mode specifies the selection mode. It must be one of these values defined by
ListSelectionModel:
SINGLE_SELECTION
SINGLE_INTERVAL_SELECTION
MULTIPLE_INTERVAL_SELECTION
The default, multiple-interval selection, lets the user select multiple ranges of items within a
list.With single-interval selection, the user can select one range of items.With single
selection, the user can select only a single item. Of course, a single item can be selected in
the other two modes, too. It‘s just that they also allow a range to be selected.
You can obtain the index of the first item selected, which will also be the index of the only
selected item when using single-selection mode, by calling getSelectedIndex( ), shown here:
int getSelectedIndex( ) Indexing begins at zero. So, if the first item is selected, this method will
return 0. If no item is selected, –1 is returned. Instead of obtaining the index of a selection, you
can obtain the value associated with the selection by calling getSelectedValue(
):
Object getSelectedValue( )
It returns a reference to the first selected value. If no value has been selected, it returns null.
The event handling mechanism used by Swing is the same as that used by the AWT. This
approach is called the delegation event model. In many cases, Swing uses the same events as
does the AWT, and these events are packaged in java.awt.event. Events specific to Swing are
stored in javax.swing.event. Although events are handled in Swing in the same way as they are
with the AWT, it is still useful to work through a simple example. The following program
handles the event generated by a Swing push button. Event handling program imports both the
java.awt and java.awt.event packages. The java.awt package is needed because it contains the
FlowLayout class, which supports the standard flow layout manager used to lay out
components in a frame.
The java.awt.event package is needed because it defines the ActionListener interface and the
ActionEvent class. The EventDemo constructor begins by creating a JFrame called jfrm. It
then sets the layout manager for the content pane of jfrmto FlowLayout. Recall that, by default,
the content pane uses BorderLayout as its layout manager. However, for this example,
FlowLayout is more convenient. Notice that FlowLayout is assigned using this statement
jfrm.setLayout(new FlowLayout());
As explained, in the past you had to explicitly call getContentPane( ) to set the layout manager
for the content pane.
Swing applets use the same four lifecycle methods as init( ), start( ), stop( ), and destroy( ). Of
course, you need override only those methods that are needed by your applet. Painting is
accomplished differently in Swing than it is in the AWT, and a Swing applet will not normally
override the paint( ) method. All interaction with components in a Swing applet must take place
on the event dispatching thread, as described in the previous section. This threading issue
applies to all Swing programs.
Here is an example of a Swing applet. It provides the same functionality as the previous
application, but does so in applet form.
Applets must use invokeAndWait( ) because the init( ) method must not return until the entire
initialization process has been completed. In essence, the start( ) method cannot be called until
after initialization, which means that the GUI must be fully constructed. Inside makeG UI( ),
the two buttons and label are created, and the action listeners are added to the buttons. Finally,
the components are added to the content pane. Although this example is quite simple, this same
general approach must be used when building any Swing GUI that will be used by an applet.
Srinivas University IV Semester BCA
Assignment-3
1. Where are the following four methods commonly used?
1) public void add(Component c)
2) public void setSize(int width,int height)
3) public void setLayout(LayoutManager m)
4) public void setVisible(boolean)
A. Graphics class
B. Component class
C. Both A & B
D. None of the above
Answer: Option B
2. Implement the Listener interface and overrides its methods is required to perform in
event handling.
A. True
B. False
Answer: Option A
3. Which is the container that doesn't contain title bar and MenuBars but it can have other
components like button, textfield etc?
A. Window
B. Frame
C. Panel
D. Container
Answer: Option C
4. Which object can be constructed to show any number of choices in the visible window?
A. Labels
B. Choice
C. List
D. Checkbox
Answer: Option C
5. Object which can store group of other objects is called
A. Collection object
B. Java object
C. Package
D. Wrapper
Answer: Option A
6. JFrame myFrame = new JFrame (); Any command (such as the one listed above) which
creates a new object of a specific class (in this case a new JFrame object called myFrame) is
generally called a ...
A. Constructor
B. Layout manager
C. Parameter
8. What is the name of the Swing class that is used for frames?
A. Window
B. Frame
C. JFrame
D. SwingFrame
Answer: Option C
9. What is the name of the Swing class that is used for frames?
A. swing
B. Frame
C. applet
D. SwingFrame
Answer: Option A
11. The layout of a container can be altered by using which of the following methods.
A. setLayout(aLayoutManager)
B. layout(aLayoutManager)
C. addLayout(aLayoutManager)
D. setLayoutManager(aLayoutManager)
Answer: Option A
12. Which of these methods can be used to know which key is pressed?
A. getActionEvent()
B. getActionKey()
C. getModifier()
D. getKey()
Answer: Option C
14. Which of these packages contains all the classes and methods required for event
handling in java?
A. Java.applet
B. Java.awt
C. Java.awt.event
D. Java.event
Answer: Option C
15. In Java, what do you call an area on the screen that has nice borders and various buttons
along the top border?
A. A window
B. A screen
C. A box
D. A frame
Answer: Option D
16. Suppose you are developing a Java Swing application and want to toggle between
various views of the design area. Which of the views given below are present for the users to
toggle?
A. Design View
B. Requirements View
C. Source View
D. Management View
Answer: Option B
A. Inches
B. Nits
C. Dots
D. Pixels
Answer: Option D
It is lightweight.
It supports pluggable look and feel.
It follows MVC (Model View Controller) architecture.
A. Swing
B. AWT
C. Both A & B
D. None of the above
A. java.awt
B. java.Graphics
C. java.awt.Graphics
D) None of the above
Answer: Option C
20. The ActionListener interface is used for handling action events,For example,it's used by
a
A) JButton
B) JCheckbox
C) JMenuItem
D) All of these
Answer: Option D
21. Which class is used to create a pop-up list of items from which the user may choose?
A. List
B. Choice
C. Labels
D. Checkbox
Answer: Option B
22. Which class is used for this Processing Method processActionEvent( )?
A. Button,List,MenuItem
B. Button,Checkbox,Choice
C. Scrollbar,Component,Button
D. None of the above
Answer: Option A
23. The Swing Component classes that are used in Encapsulates a mutually exclusive set of
buttons?
A. AbstractButton
B. ButtonGroup
C. JButton
D. ImageIcon
Answer: Option B