Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

GUI Objects

This Applet simply demonstrates use of a couple or three GUI objects from the awt that you might find useful in future programs. You can see some of their methods here. For more try the Java 1.4.2 Documentation set at:

http://java.sun.com/j2se/1.3/docs/api/index.html

Click on the awt link and then the Class that you want more information on.

For example, select Choice then see the documentation for the constructor and the method add(String) that we used in the following example.

 

 

 

 

New objects:

The Checkbox - like a radio button, you can select it on or off. You can place Checkboxes in a CheckboxGroup - in which case selecting one button automatically de-selects any others.

The Choice - like a drop-down list, you can click and select items listed.

The TextArea - a bigger version of the TextField with multiple lines and scrollbars.

Notice the Checkboxes being associated with the CheckboxGroup:

 

 

We can set the size of the Applet window:

 

 

 

 

 

 

 

 

We manually add the choices to the Choice list:

 

 

 

 

We append (add) a line to the TextArea

Each time the button is pressed, data from the GUI objects is collected and displayed in the TextArea.

Notice the multi-line layout.

getSelectedCheckbox() is a method of the CheckboxGroup Class .

 

Source code: Gooey.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
* An Applet to demonstrate some GUI objects
*
* @author
* @version
*/

public class Gooey extends Applet implements ActionListener
{
 
  // sample awt objects
   private TextField name = new TextField("Type your name here!");
   private CheckboxGroup theGroup = new CheckboxGroup();
   private Checkbox cbf = new Checkbox("Female", theGroup, false);
   private Checkbox cbm = new Checkbox("Male", theGroup, false);
   private Choice theList = new Choice();
   private TextArea theDisplay = new TextArea();
   private Button pressMe = new Button("Press me!");

/**
  * Add objects to the Applet
  */

  public void init()
  {
   setSize(500, 400);
   add(name);
   // Panel for radio buttons
   Panel radioPanel = new Panel();
   radioPanel.add(cbf);
   radioPanel.add(cbm);
   add(radioPanel);
   add(theList);
   // Panel for text area and button
   Panel bottomPanel = new Panel();
   bottomPanel.add(theDisplay);
   bottomPanel.add(pressMe);
   add(bottomPanel);
   add(pressMe);

   pressMe.addActionListener(this);

   // Initialize some GUI objects
   theList.add("Chocolate");
   theList.add("Cabbage");
   theList.add("Potato");
   theList.add("Apples");
   theList.add("Bananas");
   theList.add("Crisps");
   theDisplay.append("Name sex favourite\n");
 }
 /**
 *
 * @param e carries details about the event that occurred
 */

 public void actionPerformed(ActionEvent e)
 {
   // add collected data to the text area:
   theDisplay.append( name.getText() + " "
                    + theGroup.getSelectedCheckbox().getLabel() + " "
                    + theList.getSelectedItem() + "\n" );
 }
}

 

 

GUI - graphical user interface


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009