Home : Course Map : Chapter 7 : Java : Tech :
Histogram Display with User Interface
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

In this section we add interactive graphics components to our histogram display so that the user can interact with the program.

We use the Histogram, HistFormat, and HistPanel classes discussed in the Chapter 6: Tech : Histogram Display program. Here the applet program adds the histogramming panel HistPanel to its content pane and also a panel with 3 buttons and a textfield. The textfield holds the number of values to be added to the histogram when the "Go" button is pushed. The "Clear" button empties the histogram.


UIDrawHistApplet.java - The applet uses HistPanel object to display the contents of an instance of Histogram. PlotFormat used by HistPanel to format the scale values. The applet holds the output JPanel subclass for graphics   output. Includes "Go","Clear", and "Exit" buttons to initiate processing, clear the text area, and exit when in standalone mode.

+ Previous classes:
Chapter 6:Tech: Histogram.java, HistPanel.java
Chapter 6:Tech: PlotPanel.java, PlotFormat.java

Java Techniques & Other Programming Notes

  • The conversion of the string obtained from the textfield to an integer can throw an exception. Here we indicate the bad value by showing an error message on the browser status bar. A more sophisticated approach would involve opening a dialog warning window but we will discuss dialogs later.

  • Programmers will typically add the ActionListener object, here the applet, to the actionlistenters list in the text field . Then when one enters text and hits Enter on the keyboard, the text field will send an action event actionPerformed() method in the ActionListener object., which can then grab the string from the text field and update the particular parameter of interest.

    However, if the user forgets to hit Enter, no action will occur and the old value will remain in the parameter.

    The example here shows a more user friendly approach. The value in the text field is taken whenever the Go button is pushed. This insures that the value showing in the textfile is always the parameter used in the calculation.

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
  *  This program will run as an applet inside
  *  an application frame.
  *
  * The applet uses the HistPanel to display contents of
  *  an instance of Histogram. HistFormat used by HistPanel to
  *  format the scale values.
  *
  *  Includes "Go" button to add random values from a Gaussian
  *  distribution to the histogram. The number of values taken from
  *  entry in a JTextField. "Clear"  button clears the histogram.
  *  In standalone mode, the Exit button closes the program.
  *  
**/
public class UIDrawHistApplet extends JApplet
             implements ActionListener
{
  // Use the HistPanel JPanel subclass here
  HistPanel fOutputPanel = null;

  Histogram fHistogram;
  int fNumDataPoints   = 100;

  // A text field for input strings
  JTextField fTextField = null;

  // Flag for whether the applet is in a browser
  // or running via the main () below.
  boolean fInBrowser = true;

  //Buttons
  JButton fGoButton;
  JButton fClearButton;
  JButton fExitButton;

  /**
    * Create a User Interface with a textarea with sroll bars
    * and a Go button to initiate processing and a Clear button
    * to clear the textarea.
   **/
  public void init () {

    Container content_pane = getContentPane ();

    JPanel panel = new JPanel (new BorderLayout ());

    // Create a histogram with Gaussian distribution.
    makeHist ();

    // JPanel subclass here.
    fOutputPanel = new HistPanel (fHistogram);

    panel.add (fOutputPanel,"Center");

    // Use a textfield for an input parameter.
    fTextField =
      new JTextField (Integer.toString (fNumDataPoints), 10);

    // If return hit after entering text, the
    // actionPerformed will be invoked.
    fTextField.addActionListener (this);

    fGoButton = new JButton ("Go");
    fGoButton.addActionListener (this);

    fClearButton = new JButton ("Clear");
    fClearButton.addActionListener (this);

    fExitButton = new JButton ("Exit");
    fExitButton.addActionListener (this);

    JPanel control_panel = new JPanel ();

    control_panel.add (fTextField);
    control_panel.add (fGoButton);
    control_panel.add (fClearButton);
    control_panel.add (fExitButton);

    if (fInBrowser) fExitButton.setEnabled (false);

    panel.add (control_panel,"South");

    // Add text area with scrolling to the contentPane.
    content_pane.add (panel);

  } // init

  /** Respond to buttons. **/
  public void actionPerformed (ActionEvent e){
    Object source = e.getSource ();
    if (source == fGoButton || source == fTextField) {
        String strNumDataPoints = fTextField.getText ();

        try {
            fNumDataPoints = Integer.parseInt (strNumDataPoints);
        } catch (NumberFormatException ex) {
          // Could open an error dialog here but just
          // display a message on the browser status line.
          showStatus ("Bad input value");
          return;
        }
        makeHist ();
        repaint ();

    } else if (source == fClearButton  ) {
        fHistogram.clear ();
        repaint ();

    } else if (!fInBrowser)
        System.exit (0);

  }// actionPerformed()

  /** Create a histogram if it doesn't yet exit. Fill it
    * with Gaussian random distribution.
   **/
  void makeHist () {
    // Create an instance of the Random class for
    // producing our random values.
    java.util.Random r = new java.util.Random ();

    // Them method nextGaussian in the class Random produces
    // a value centered at 0.0 and a standard deviation of 1.0.

    // Create an instance of our histogram class. Set the range
    // sot that it includes most of the distribution.
    if (fHistogram == null)
        fHistogram = new Histogram ("Gaussian Distribution",
                                  "random values",
                                  20,-3.0,3.0);

    // Fill histogram with Gaussian distribution
    for (int i=0; i < fNumDataPoints; i++) {
        double val = r.nextGaussian ();
        fHistogram.add (val);
    }
  } // makeHist

  /** Create a frame and add the applet to it. **/
  public static void main (String[] args) {
    // Dimensions for our frame
    int frame_width  = 450;
    int frame_height = 300;

    // Create an instance of the applet to add to the frame.
    UIDrawHistApplet applet = new UIDrawHistApplet ();
    applet.fInBrowser = false;
    applet.init ();

    // Create the frame with the title
    JFrame f = new JFrame ("Histogram with Gaussian");
    f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    // Add applet to the frame and display the frame.
    f.getContentPane ().add ( applet);
    f.setSize (new Dimension (frame_width,frame_height));
    f.setVisible (true);

  } // main

} // class UIDrawHistApplet

 

References & Web Resources

Last update: Nov. 4, 2004

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.