Explain The Advantages of Swing
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.
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.
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.