Wednesday 29 March 2017

A Tour of Swing-Icons & Text Fields - Java Tutorials

A Tour of Swing-Icons

In Part II, you saw how to build user interfaces with the AWT classes. Here, we will take a tour of a supercharged alternative called Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar components, such as buttons, check boxes, and labels, Swing supplies several exciting additions, including tabbed panes, scroll panes, trees, and tables. Even familiar components such as buttons have more capabilities in Swing. For example, a button may have both an image and a text string associated with it. Also, the image can be changed as the state of the button changes.

Unlike AWT components, Swing components are not implemented by platform-specific code. Instead, they are written entirely in Java and, therefore, are platform-independent. The term lightweight is used to describe such elements.

The number of classes and interfaces in the Swing packages is substantial, and this chapter provides an overview of just a few. Swing is an area that you will want to explore further on your own.

The Swing component classes that are used in this book are shown here:

Class  --  Description

AbstractButton:  Abstract superclass for Swing buttons.

ButtonGroup:  Encapsulates a mutually exclusive set of buttons.

ImageIcon:  Encapsulates an icon.

JApplet:  The Swing version of Applet.

JButton:  The Swing push button class.

JCheckBox:  The Swing check box class.

JComboBox:  Encapsulates a combo box (an combination of a drop-down list and text field).

JLabel:  The Swing version of a label.

JRadioButton:  The Swing version of a radio button.

JScrollPane:  Encapsulates a scrollable window.

JTabbedPane:  Encapsulates a tabbed window.

JTable:  Encapsulates a table-based control.

JTextField:  The Swing version of a text field.

JTree:  Encapsulates a tree-based control.


The Swing-related classes are contained in javax.swing and its subpackages, such as javax.swing.tree. Many other Swing-related classes and interfaces exist that are not examined in this chapter.

The remainder of this chapter examines various Swing components and illustrates them through sample applets.


JApplet

Fundamental to Swing is the JApplet class, which extends Applet. Applets that use Swing must be subclasses of JApplet. JApplet is rich with functionality that is not found in Applet. For example, JApplet supports various “panes,” such as the content pane, the glass pane, and the root pane. For the examples in this chapter, we will not be using most of JApplet’s enhanced features. However, one difference between Applet and JApplet is important to this discussion, because it is used by the sample applets in this chapter. When adding a component to an instance of JApplet, do not invoke the add( ) method of the applet. Instead, call add( ) for the content pane of the JApplet object. The content pane can be obtained via the method shown here:

      Container getContentPane( )

The add( ) method of Container can be used to add a component to a content pane. Its form is shown here:

      void add(comp)

Here, comp is the component to be added to the content pane.


Icons and Labels

In Swing, icons are encapsulated by the ImageIcon class, which paints an icon from an image. Two of its constructors are shown here:

      ImageIcon(String filename)
      ImageIcon(URL url)

The first form uses the image in the file named filename. The second form uses the image in the resource identified by url.

The ImageIcon class implements the Icon interface that declares the methods shown here:

Method  --  Description

int getIconHeight( ):  Returns the height of the icon in pixels.

int getIconWidth( ):  Returns the width of the icon in pixels.

void paintIcon(Component comp, Graphics g, int x, int y):  Paints the icon at position x, y on the graphics context g. Additional information about the paint operation can be provided in comp.


Swing labels are instances of the JLabel class, which extends JComponent. It can display text and/or an icon. Some of its constructors are shown here:

      JLabel(Icon i)
      Label(String s)
      JLabel(String s, Icon i, int align)

Here, s and i are the text and icon used for the label. The align argument is either LEFT, RIGHT, CENTER, LEADING, or TRAILING. These constants are defined in th SwingConstants interface, along with several others used by the Swing classes.

The icon and text associated with the label can be read and written by the following methods:

      Icon getIcon( )
      String getText( )
      void setIcon(Icon i)
      void setText(String s)

Here, i and s are the icon and text, respectively.

The following example illustrates how to create and display a label containing both an icon and a string. The applet begins by getting its content pane. Next, an ImageIcon object is created for the file france.gif. This is used as the second argument to the JLabel constructor. The first and last arguments for the JLabel constructor are the label text and the alignment. Finally, the label is added to the content pane.

  import java.awt.*;
  import javax.swing.*;
  /*
    <applet code="JLabelDemo" width=250 height=150>
    </applet>
  */

  public class JLabelDemo extends JApplet {

    public void init() {
      // Get content pane
      Container contentPane = getContentPane();

      // Create an icon
      ImageIcon ii = new ImageIcon("france.gif");

      // Create a label
      JLabel jl = new JLabel("France", ii, JLabel.CENTER);

      // Add label to the content pane
      contentPane.add(jl);
    }
  }




Text Fields

The Swing text field is encapsulated by the JTextComponent class, which extends JComponent. It provides functionality that is common to Swing text components. One of its subclasses is JTextField, which allows you to edit one line of text. Some of its constructors are shown here:

      JTextField( )
      JTextField(int cols)
      JTextField(String s, int cols)
      JTextField(String s)

Here, s is the string to be presented, and cols is the number of columns in the text field.

The following example illustrates how to create a text field. The applet begins by getting its content pane, and then a flow layout is assigned as its layout manager. Next, a JTextField object is created and is added to the content pane.

  import java.awt.*;
  import javax.swing.*;
  /*
    <applet code="JTextFieldDemo" width=300 height=50>
    </applet>
  */

  public class JTextFieldDemo extends JApplet {
    JTextField jtf;

    public void init() {

      // Get content pane
      Container contentPane = getContentPane();
      contentPane.setLayout(new FlowLayout());

      // Add text field to content pane
      jtf = new JTextField(15);
      contentPane.add(jtf);
    }
  }

No comments:

Post a Comment