Wednesday 29 March 2017

Combo Boxes, Tabbed Panes & Scroll Panes - Java Tutorials

Combo Boxes

Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class, which extends JComponent. A combo box normally displays one entry. However, it can also display a drop-down list that allows a user to select a different entry. You can also type your selection into the text field. Two of JComboBox’s constructors are shown here:

      JComboBox( )
      JComboBox(Vector v)

Here, v is a vector that initializes the combo box. 

Items are added to the list of choices via the addItem( ) method, whose signature is shown here:

      void addItem(Object obj)

Here, obj is the object to be added to the combo box.

The following example contains a combo box and a label. The label displays an icon. The combo box contains entries for “France”, “Germany”, “Italy”, and “Japan”. When a country is selected, the label is updated to display the flag for that country.

  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  /*
    <applet code="JComboBoxDemo" width=300 height=100>
    </applet>
  */

  public class JComboBoxDemo extends JApplet
  implements ItemListener {
    JLabel jl;
    ImageIcon france, germany, italy, japan;

    public void init() {

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

      // Create a combo box and add it
      // to the panel
      JComboBox jc = new JComboBox();
      jc.addItem("France");
      jc.addItem("Germany");
      jc.addItem("Italy");
      jc.addItem("Japan");
      jc.addItemListener(this);
      contentPane.add(jc);

      // Create label
      jl = new JLabel(new ImageIcon("france.gif"));
      contentPane.add(jl);
    }

    public void itemStateChanged(ItemEvent ie) {
      String s = (String)ie.getItem();
      jl.setIcon(new ImageIcon(s + ".gif"));
    }
  }




Tabbed Panes

A tabbed pane is a component that appears as a group of folders in a file cabinet. Each folder has a title. When a user selects a folder, its contents become visible. Only one of the folders may be selected at a time. Tabbed panes are commonly used for setting configuration options.

Tabbed panes are encapsulated by the JTabbedPane class, which extends JComponent. We will use its default constructor. Tabs are defined via the following method:

      void addTab(String str, Component comp)

Here, str is the title for the tab, and comp is the component that should be added to the tab. Typically, a JPanel or a subclass of it is added.

The general procedure to use a tabbed pane in an applet is outlined here:
  1. Create a JTabbedPane object.
  2. Call addTab( ) to add a tab to the pane. (The arguments to this method define the title of the tab and the component it contains.)
  3. Repeat step 2 for each tab.
  4. Add the tabbed pane to the content pane of the applet.


The following example illustrates how to create a tabbed pane. The first tab is titled “Cities” and contains four buttons. Each button displays the name of a city. The second tab is titled “Colors” and contains three check boxes. Each check box displays the name of a color. The third tab is titled “Flavors” and contains one combo box. This enables the user to select one of three flavors.

  import javax.swing.*;
  /*
    <applet code="JTabbedPaneDemo" width=400 height=100>
    </applet>
  */

  public class JTabbedPaneDemo extends JApplet {

    public void init() {

      JTabbedPane jtp = new JTabbedPane();
      jtp.addTab("Cities", new CitiesPanel());
      jtp.addTab("Colors", new ColorsPanel());
      jtp.addTab("Flavors", new FlavorsPanel());
      getContentPane().add(jtp);
    }
  }

  class CitiesPanel extends JPanel {

    public CitiesPanel() {

      JButton b1 = new JButton("New York");
      add(b1);
      JButton b2 = new JButton("London");
      add(b2);
      JButton b3 = new JButton("Hong Kong");
      add(b3);
      JButton b4 = new JButton("Tokyo");
      add(b4);
    }
  }

  class ColorsPanel extends JPanel {

    public ColorsPanel() {

      JCheckBox cb1 = new JCheckBox("Red");
      add(cb1);
      JCheckBox cb2 = new JCheckBox("Green");
      add(cb2);
      JCheckBox cb3 = new JCheckBox("Blue");
      add(cb3);
    }
  }

  class FlavorsPanel extends JPanel {

    public FlavorsPanel() {

      JComboBox jcb = new JComboBox();
      jcb.addItem("Vanilla");
      jcb.addItem("Chocolate");
      jcb.addItem("Strawberry");
      add(jcb);
    }
  }




Scroll Panes

A scroll pane is a component that presents a rectangular area in which a component may be viewed. Horizontal and/or vertical scroll bars may be provided if necessary. Scroll panes are implemented in Swing by the JScrollPane class, which extends JComponent. Some of its constructors are shown here:

      JScrollPane(Component comp)
      JScrollPane(int vsb, int hsb)
      JScrollPane(Component comp, int vsb, int hsb)

Here, comp is the component to be added to the scroll pane. vsb and hsb are int constants that define when vertical and horizontal scroll bars for this scroll pane are shown. These constants are defined by the ScrollPaneConstants interface. Some examples of these constants are described as follows:

Constant  --  Description

HORIZONTAL_SCROLLBAR_ALWAYS:  Always provide horizontal scroll bar

HORIZONTAL_SCROLLBAR_AS_NEEDED:  Provide horizontal scroll bar, if needed

VERTICAL_SCROLLBAR_ALWAYS:  Always provide vertical scroll bar

VERTICAL_SCROLLBAR_AS_NEEDED:  Provide vertical scroll bar, if needed


Here are the steps that you should follow to use a scroll pane in an applet:
  1. Create a JComponent object.
  2. Create a JScrollPane object. (The arguments to the constructor specify the component and the policies for vertical and horizontal scroll bars.)
  3. Add the scroll pane to the content pane of the applet.


The following example illustrates a scroll pane. First, the content pane of the JApplet object is obtained and a border layout is assigned as its layout manager. Next a JPanel object is created and four hundred buttons are added to it, arranged into twenty columns. The panel is then added to a scroll pane, and the scroll pane is added to the content pane. This causes vertical and horizontal scroll bars to appear. You can use the scroll bars to scroll the buttons into view.

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

  public class JScrollPaneDemo extends JApplet {

    public void init() {

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

      // Add 400 buttons to a panel
      JPanel jp = new JPanel();
      jp.setLayout(new GridLayout(20, 20));
      int b = 0;
      for(int i = 0; i < 20; i++) {
        for(int j = 0; j < 20; j++) {
          jp.add(new JButton("Button " + b));
          ++b;
        }
      }

      // Add panel to a scroll pane
      int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
      int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
      JScrollPane jsp = new JScrollPane(jp, v, h);

      // Add scroll pane to the content pane
      contentPane.add(jsp, BorderLayout.CENTER);
    }
  }

No comments:

Post a Comment