Wednesday 29 March 2017

Buttons - Java Tutorials

Swing buttons provide features that are not found in the Button class defined by the AWT. For example, you can associate an icon with a Swing button. Swing buttons are subclasses of the AbstractButton class, which extends JComponent. AbstractButton contains many methods that allow you to control the behavior of buttons, check boxes, and radio buttons. For example, you can define different icons that are displayed for the component when it is disabled, pressed, or selected. Another icon can be used as a rollover icon, which is displayed when the mouse is positioned over that component. The following are the methods that control this behavior:

      void setDisabledIcon(Icon di)
      void setPressedIcon(Icon pi)
      void setSelectedIcon(Icon si)
      void setRolloverIcon(Icon ri)

Here, di, pi, si, and ri are the icons to be used for these different conditions. The text associated with a button can be read and written via the following methods:

      String getText( )
      void setText(String s)

Here, s is the text to be associated with the button. Concrete subclasses of AbstractButton generate action events when they are pressed. Listeners register and unregister for these events via the methods shown here:

      void addActionListener(ActionListener al)
      void removeActionListener(ActionListener al)

Here, al is the action listener. AbstractButton is a superclass for push buttons, check boxes, and radio buttons. Each is examined next.


The JButton Class

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. Some of its constructors are shown here:

      JButton(Icon i)
      JButton(String s)
      JButton(String s, Icon i)

Here, s and i are the string and icon used for the button. The following example displays four push buttons and a text field. Each button displays an icon that represents the flag of a country. When a button is pressed, the name of that country is displayed in the text field. The applet begins by getting its content pane and setting the layout manager of that pane. Four image buttons are created and added to the content pane. Next, the applet is registered to receive action events that are generated by the buttons. A text field is then created and added to the applet. Finally, a handler for action events displays the command string that is associated with the button. The text field is used to present this string.

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

  public class JButtonDemo extends JApplet
  implements ActionListener {
    JTextField jtf;

    public void init() {

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

      // Add buttons to content pane
      ImageIcon france = new ImageIcon("france.gif");
      JButton jb = new JButton(france);
      jb.setActionCommand("France");
      jb.addActionListener(this);
      contentPane.add(jb);

      ImageIcon germany = new ImageIcon("germany.gif");
      jb = new JButton(germany);
      jb.setActionCommand("Germany");
      jb.addActionListener(this);
      contentPane.add(jb);

      ImageIcon italy = new ImageIcon("italy.gif");
      jb = new JButton(italy);
      jb.setActionCommand("Italy");
      jb.addActionListener(this);
      contentPane.add(jb);

      ImageIcon japan = new ImageIcon("japan.gif");
      jb = new JButton(japan);
      jb.setActionCommand("Japan");
      jb.addActionListener(this);
      contentPane.add(jb);

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

    public void actionPerformed(ActionEvent ae) {
      jtf.setText(ae.getActionCommand());
    }
  }


Check Boxes

The JCheckBox class, which provides the functionality of a check box, is a concrete implementation of AbstractButton. Its immediate superclass is JToggleButton, which provides support for two-state buttons. Some of its constructors are shown here:

      JCheckBox(Icon i)
      JCheckBox(Icon i, boolean state)
      JCheckBox(String s)
      JCheckBox(String s, boolean state)
      JCheckBox(String s, Icon i)
      JCheckBox(String s, Icon i, boolean state)

Here, i is the icon for the button. The text is specified by s. If state is true, the check box is initially selected. Otherwise, it is not. The state of the check box can be changed via the following method:

      void setSelected(boolean state)

Here, state is true if the check box should be checked. The following example illustrates how to create an applet that displays four check boxes and a text field. When a check box is pressed, its text is displayed in the text field. The content pane for the JApplet object is obtained, and a flow layout is assigned as its layout manager. Next, four check boxes are added to the content pane, and icons are assigned for the normal, rollover, and selected states. The applet is then registered to receive item events. Finally, a text field is added to the content pane.

When a check box is selected or deselected, an item event is generated. This is handled by itemStateChanged( ). Inside itemStateChanged( ), the getItem( ) method gets the JCheckBox object that generated the event. The getText( ) method gets the text for that check box and uses it to set the text inside the text field.

  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  /*
    <applet code="JCheckBoxDemo" width=400 height=50>
    </applet>
  */

  public class JCheckBoxDemo extends JApplet
  implements ItemListener {
    JTextField jtf;

    public void init() {

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

      // Create icons
      ImageIcon normal = new ImageIcon("normal.gif");
      ImageIcon rollover = new ImageIcon("rollover.gif");
      ImageIcon selected = new ImageIcon("selected.gif");

      // Add check boxes to the content pane
      JCheckBox cb = new JCheckBox("C", normal);
      cb.setRolloverIcon(rollover);
      cb.setSelectedIcon(selected);
      cb.addItemListener(this);
      contentPane.add(cb);

      cb = new JCheckBox("C++", normal);
      cb.setRolloverIcon(rollover);
      cb.setSelectedIcon(selected);
      cb.addItemListener(this);
      contentPane.add(cb);

      cb = new JCheckBox("Java", normal);
      cb.setRolloverIcon(rollover);
      cb.setSelectedIcon(selected);
      cb.addItemListener(this);
      contentPane.add(cb);

      cb = new JCheckBox("Perl", normal);
      cb.setRolloverIcon(rollover);
      cb.setSelectedIcon(selected);
      cb.addItemListener(this);
      contentPane.add(cb);

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

    public void itemStateChanged(ItemEvent ie) {
      JCheckBox cb = (JCheckBox)ie.getItem();
      jtf.setText(cb.getText());
    }
  }


Radio Buttons

Radio buttons are supported by the JRadioButton class, which is a concrete implementation of AbstractButton. Its immediate superclass is JToggleButton, which provides support for two-state buttons. Some of its constructors are shown here:

      JRadioButton(Icon i)
      JRadioButton(Icon i, boolean state)
      JRadioButton(String s)
      JRadioButton(String s, boolean state)
      JRadioButton(String s, Icon i)
      JRadioButton(String s, Icon i, boolean state)

Here, i is the icon for the button. The text is specified by s. If state is true, the button is initially selected. Otherwise, it is not.

Radio buttons must be configured into a group. Only one of the buttons in that group can be selected at any time. For example, if a user presses a radio button that is in a group, any previously selected button in that group is automatically deselected. The ButtonGroup class is instantiated to create a button group. Its default constructor is invoked for this purpose. Elements are then added to the button group via the following method:

      void add(AbstractButton ab)

Here, ab is a reference to the button to be added to the group.

The following example illustrates how to use radio buttons. Three radio buttons and one text field are created. When a radio button is pressed, its text is displayed in the text field. First, the content pane for the JApplet object is obtained and a flow layout is assigned as its layout manager. Next, three radio buttons are added to the content pane. Then, a button group is defined and the buttons are added to it. Finally, a text field is added to the content pane.

Radio button presses generate action events that are handled by actionPerformed( ). The getActionCommand( ) method gets the text that is associated with a radio button and uses it to set the text field.

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

  public class JRadioButtonDemo extends JApplet
  implements ActionListener {
    JTextField tf;

    public void init() {

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

      // Add radio buttons to content pane
      JRadioButton b1 = new JRadioButton("A");
      b1.addActionListener(this);
      contentPane.add(b1);

      JRadioButton b2 = new JRadioButton("B");
      b2.addActionListener(this);
      contentPane.add(b2);

      JRadioButton b3 = new JRadioButton("C");
      b3.addActionListener(this);
      contentPane.add(b3);

      // Define a button group
      ButtonGroup bg = new ButtonGroup();
      bg.add(b1);
      bg.add(b2);
      bg.add(b3);

      // Create a text field and add it
      // to the content pane
      tf = new JTextField(5);
      contentPane.add(tf);
    }

    public void actionPerformed(ActionEvent ae) {
      tf.setText(ae.getActionCommand());
    }
  }

No comments:

Post a Comment