Sunday, February 12, 2012

Creating and adding buttons

Swing enables you to create buttons and add them to a GUI, using one of the subclasses of the AbstractButton class.
The subclasses of the AbstractButton class include
  • JButton
  • JCheckBox
  • JRadioButton
JButton
You use the JButton class to create an ordinary button. You can create a button with no constructor parameters, with a specified text string or image, or both. You can also use the constructor to create a button that gets its properties from a specified action.

The syntax for the constructor class includes

public JButton()
public JButton(String st, Icon ic)
public JButton(String st)
public JButton(Icon ic)
public JButton(Action ac)

You implement event handling in ordinary buttons using an action listener, which is informed each time a user clicks the button.
JCheckBox
You use the JCheckBox class to create checkboxes. You can create a checkbox with no constructor parameters, or you can specify a text string or an image, or both. You can also pass a boolean argument that indicates whether the checkbox is initially selected.

The syntax for the constructor includes

public JCheckBox()
public JCheckBox(String st)
public JCheckBox(Icon ic)
public JCheckBox(String st, Icon ic)
public JCheckBox(String st, Icon ic, boolean bool)

A checkbox generates an item event and an action event each time it is clicked. You usually listen for item events, as these enable you to determine whether an action selected or deselected the checkbox.
JRadioButton
You use the JRadioButton class to create individual radio buttons. You can create a radio button with no constructor arguments, or you can specify a text string or an icon, or both. You can also pass a boolean argument that indicates whether the radio button is initially selected.

The syntax for the constructor includes

public JRadioButton()
public JRadioButton(String st)
public JRadioButton(Icon ic)
public JRadioButton(String st, Icon ic)
public JRadioButton(String st, Icon ic, boolean bool)


You use the ButtonGroup class to create a group of buttons. In a group of radio buttons, by convention, only one button can be selected at a time.

The syntax for the constructor is

public ButtonGroup()

A radio button generates an action event each time a user clicks it. One or two item events also occur - one from the button that is selected and another from the button that is deselected. You usually implement an action listener to handle radio button clicks.
Suppose you're creating a user interface that contains three different types of buttons - a common button, a radio button, and a checkbox - and displays a different message each time the user clicks a button.

You first add a common button - with the text "A JButton" - to the interface.
Then you create an action listener to ensure that clicking the button generates a response on the interface.
    // Add a JButton
    commonButton = new JButton("A JButton") ;
    commonButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        label.setText("JButton clicked") ;
      }
    }) ;
Next you want to add a checkbox named checkBox to the interface and specify that it is initially unselected.
    // Add a JCheckBox
    checkBox = MISSING CODE("Order special deals", false) ;
    checkBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (checkBox.isSelected())
          label.setText("You will be receiving special deals") ;
        else
          label.setText("You have chosen not to
receive special deals") ;
      }
    }) ;
You type new JCheckBox.
After you create the checkbox, you add an action listener to handle the actions from the checkbox.
You use the isSelected method to check if the action event selected or deselected the checkbox.

If the user selects the checkbox, the text label - "You will be receiving special deals" - displays.
    // Add a JCheckBox
    checkBox = new JCheckBox("Order special deals", false) ;
    checkBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (checkBox.isSelected())
          label.setText("You will be receiving special deals") ;
        else
          label.setText("You have chosen not to
receive special deals") ;
      }
    }) ;
Next you want to create a set of radio buttons that enables a user to choose a particular ice cream flavor - vanilla, chocolate, or strawberry.
So you create an instance of the ButtonGroup class, named icecreams, which you use to organize individual radio buttons. Then you create a radio button for each ice cream flavor - labeled "Vanilla", "Chocolate", and "Strawberry".
You set the action command string for each radio button using the setActionCommand method, which enables you to attach a string to an action within a generic event handler, for identification purposes.
You add an action listener for the radio buttons to detect when each is clicked.
    // Add a ButtonGroup to hold three radio buttons
    icecreams = new ButtonGroup() ;

    radio1 = new JRadioButton("Vanilla") ;
    radio1.setActionCommand("Vanilla") ;
    myRadioListener listenIn = new myRadioListener() ;
    radio1.addActionListener(listenIn) ;

    radio1.setSelected(true) ;
    radio2 = new JRadioButton("Chocolate") ;
    radio2.setActionCommand("Chocolate") ;
    radio2.addActionListener(listenIn) ;
    radio3 = new JRadioButton("Strawberry") ;
    radio3.setActionCommand("Strawberry") ;
    radio3.addActionListener(listenIn) ;

    icecreams.add(radio1) ;
    icecreams.add(radio2) ;
    icecreams.add(radio3) ;

    // A JPanel will hold the contents
    JPanel radiopane = new JPanel(new GridLayout(3,1)) ;
    radiopane.add(radio1) ;
    radiopane.add(radio2) ;
    radiopane.add(radio3) ;
You add the radio buttons to the button group using the add method of the ButtonGroup class.
void add(AbstractButton ab)
And you use the setSelected method so that the Vanilla radio button is initially selected.
    // Add a ButtonGroup to hold three radio buttons
    icecreams = new ButtonGroup() ;

    radio1 = new JRadioButton("Vanilla") ;
    radio1.setActionCommand("Vanilla") ;
    myRadioListener listenIn = new myRadioListener() ;
    radio1.addActionListener(listenIn) ;

    radio1.setSelected(true) ;
    radio2 = new JRadioButton("Chocolate") ;
    radio2.setActionCommand("Chocolate") ;
    radio2.addActionListener(listenIn) ;
    radio3 = new JRadioButton("Strawberry") ;
    radio3.setActionCommand("Strawberry") ;
    radio3.addActionListener(listenIn) ;

    icecreams.add(radio1) ;
    icecreams.add(radio2) ;
    icecreams.add(radio3) ;

    // A JPanel will hold the contents
    JPanel radiopane = new JPanel(new GridLayout(3,1)) ;
    radiopane.add(radio1) ;
    radiopane.add(radio2) ;
    radiopane.add(radio3) ;
You have now created an interface that contains a standard button, a checkbox, and a set of three radio buttons.

0 comments:

Post a Comment