0% found this document useful (0 votes)
78 views

Explain The Advantages of Swing

The document discusses the key advantages of Swing which include lightweight components and a pluggable look and feel. It also summarizes containers, components, layout managers, and specific Swing components like JTextField, JList, JButton and how to create a basic Swing applet.

Uploaded by

Adithya Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Explain The Advantages of Swing

The document discusses the key advantages of Swing which include lightweight components and a pluggable look and feel. It also summarizes containers, components, layout managers, and specific Swing components like JTextField, JList, JButton and how to create a basic Swing applet.

Uploaded by

Adithya Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Explain the advantages of Swing.

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.

2. Write a short note on 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.

3. Briefly explain 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.
JApplet, JButton, JCheckBox, JCheckBoxMenuItem, JColorChooser, JComboBox, JComponent,
JDesktopPane, JDialog, JEditorPane, JFileChooser, JFormattedTextField, JFrame, JInternalFrame,
JLabel, JLayeredPane, JList JMenu, JMenuBar and JMenuItem etc.
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.

4. Mention the purpose of different layout managers available in Swing.


layout manager automatically arranges controls within a window by using some type of algorithm. If you
have programmed for other GUI environments, such as Windows, then you are accustomed to laying out
your controls by hand. While it is possible to lay out Java controls by hand, too,you generally won‘t want
to, for two main reasons. First, it is very tedious to manually layout a large number of components.
Second, sometimes the width and height information is not yet available when you need to arrange some
control, because the native toolkit components haven‘t been realized.
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:
void setLayout(LayoutManager layoutObj)

5. Explain the use of JTextField and any methods associate with it.
TextField is the simplest Swing text component. It is also probably its most widely used text component.
JTextField allows you to edit one line of text. It is derived from JTextComponent, which provides the
basic functionality common to Swing text components. JTextField uses the Document interface for its
model.
Three of JTextField‘s constructors are shown here:
JTextField(int cols) JTextField(String str, int cols) JTextField(String str)
Here, str is the string to be initially presented, and cols is the number of columns in the text field. If no
string is specified, the text field is initially empty. If the number of columns is not specified, the text field
is sized to fit the specified string. JTextField generates events in response to user interaction. For
example, an ActionEvent is fired when the user presses ENTER. ACaretEvent is fired each time the caret
(i.e., the cursor) changes position. (CaretEvent is packaged in javax.swing.event.) Other events are also
possible. In many cases, your program will not need to handle these events. Instead, you will simply
obtain the string currently in the text field when it is needed. To obtain the text currently in the text field,
call getText( ).

6. Explain the use of JList and any methods associated with it.
In Swing, the basic list class is called JList. It supports the selection of one or more items from a list.
Although the list often consists of strings, it is possible to create a list of just about any object that can be
displayed. JList is so widely used in Java that it is highly unlikely that you have not seen one before.
JList provides several constructors. The one used here is
JList(Object[ ] items)
This creates a JList that contains the items in the array specified by items. JList is based on two models.
The first is ListModel. This interface defines how access to the list data is achieved. The second model is
the ListSelectionModel interface, which defines methods that determine what list item or items are
selected. Although a JList will work properly by itself, most of the time you will wrap a JList inside a
JScrollPane.
ListSelectionListener. This listener specifies only one method, called valueChanged( ), which is shown
here:
void valueChanged(ListSelectionEvent le)
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

7. Explain the purpose of JButton and explain any methods associated with it.
The JButton class provides the functionality of a push button. JButton allows an icon, a string, or both to
be associated with the push button. Three of its constructors are shown here: JButton(Icon icon)
JButton(String str) JButton(String str, Icon icon)
Here, str and icon are the string and icon used for the button. When the button is pressed, an ActionEvent
is generated. Using the ActionEvent object passed to the actionPerformed( ) method of the registered
ActionListener, you can obtain the action command string associated with the button. By default, this is
the string displayed inside the button. However, you can set the action command by calling
setActionCommand( ) on the button. You can obtain the action command by calling
getActionCommand( ) on the event object. It is declared like this:
String getActionCommand( )
The action command identifies the button. Thus, when using two or more buttons within the same
application, the action command gives you an easy way to determine which button was pressed.

8. With an example explain how to create a swing applet.


The second type of program that commonly uses Swing is the applet. Swing-based applets are similar to
AWT-based applets, but with an important difference: ASwing applet extends JApplet rather than
Applet. JApplet is derived from Applet. Thus, JApplet includes all of the functionality found in Applet
and adds support for Swing. JApplet is a top-level Swing container, which means that it is not derived
from JComponent. Because JApplet is a top- level container, it includes the various panes described
earlier. This means that all components are added to JApplet‘s content pane in the same way that
components are added to JFrame‘s 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.

9. Explain the use of JCheckBox and any methods associated with it.
The JCheckBox class provides the functionality of a check box. Its immediate superclass is
JToggleButton, which provides support for two-state buttons, as just described. JCheckBox defines
several constructors. The one used here is JCheckBox(String str) It creates a check box that has the text
specified by str as a label. Other constructors let you specify the initial selection state of the button and
specify an icon. When the user selects or deselects a check box, an ItemEvent is generated. You can
obtain a reference to the JCheckBox that generated the event by calling getItem( ) on the ItemEvent
passed to the itemStateChanged( ) method defined by ItemListener. The easiest way to determine the
selected state of a check box is to call isSelected( ) on the JCheckBox instance. In addition to supporting
the normal check box operation, JCheckBox lets you specify the icons that indicate when a check box is
selected, cleared, and rolled-over. We won‘t be using this capability here, but it is available for use in
your own programs.

10. Give one example for swing program

// A simple Swing-based applet import javax.swing.*;


import java.awt.*; import java.awt.event.*;
/*
This HTML can be used to launch the applet:
<object code="MySwingApplet" width=220 height=90>
</object>
*/
public class MySwingApplet extends JApplet { JButton jbtnAlpha;
JButton jbtnBeta; JLabel jlab;
// Initialize the applet. public void init( ) {
try {
SwingUtilities. invokeAndWait(new Runnable ( ) { public void run() {
makeGUI( ); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
// This applet does not need to override start(), stop(),
// or destroy().
// Set up and initialize the GUI. private void makeGUI( ) {
// Set the applet to use flow layout. setLayout(new FlowLayout());
// Make two buttons.
jbtnAlpha = new JButton("Alpha"); jbtnBeta = new JButton("Beta");
// Add action listener for Alpha. jbtnAlpha.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent le) { jlab.setText("Alpha was pressed.");
}
});
// Add action listener for Beta. jbtnBeta.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent le) { jlab.setText("Beta was pressed.");
}
});
// Add the buttons to the content pane. add(jbtnAlpha);
add(jbtnBeta);
// Create a text-based label.
jlab = new JLabel("Press a button.");
// Add the label to the content pane. add(jlab);
}
}

You might also like