Java Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CheckEvent extends JApplet 
	implements ItemListener
{
   JCheckBox isHappy;
   JTextArea textarea;

   public void init( )
   {
		setLayout( new FlowLayout( ) );

		isHappy = new JCheckBox( "Happy?", true );
		textarea = new JTextArea( 3,10);

		// add listener
		isHappy.addItemListener( this );

		add( isHappy );
		add( textarea );
   }
   public void itemStateChanged( ItemEvent ie )
   {
		Object source = ie.getSource( );
		if( source == isHappy  && isHappy.isSelected( ) )
			textarea.setText( "You're so happy!" );
		else
			textarea.setText( "Why not happy?" );
   }
} 

Back to Code Examples
©2006- by E.S.Boese. All Rights Reserved