Programming Languages‎ > ‎Java‎ > ‎

09. Java GUI Examples (Part 4)

Example 9.3

/* Exercise 9.3: Change the example program given in the previous parts to NOT display to System.out,
      but to somewhere in the JFrame (for the display selected button, easier to use a JTextArea). 
      In itemStateChanged, display either in the JFrame or in a pop-up window (suggested to try JOptionPane's static method, showMessageDialog).
 */
import javax.swing.JFrame;
public class Exercise_9_3_TryGUIExample
{
    public static void main(String[] args)
    {
        String [] strs = {"Name", "Company", "Email Address", "Phone"};
        Exercise_9_3_GUIExample gui = new Exercise_9_3_GUIExample(strs);
        gui.setSize(400,300);
        gui.setVisible(true);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}



Example 9.4

/* Exercise 9.4: Re-do the Java application programs given in Chapter 9, Part I (TryCheckBoxes or FrameProgram ) as awt Applets or swing JApplets
    that display in a GUI window. Run this by choosing a run configuration for Applets.
    To capture the output, type [Alt]-[PrintScreen], then paste in a Word document.
*/

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

public class Exercise_9_4_TryCheckBoxes_Applets  extends JApplet implements ActionListener
{
    private JCheckBox chBoxA, chBoxB, chBoxC, chBoxD;
    private JLabel labelA, labelB, labelC, labelD;

    public void init()
    {
        setLayout(new FlowLayout());                // UPDATED!
       
        chBoxA = new JCheckBox("A");
        chBoxB = new JCheckBox("B");
        chBoxC = new JCheckBox("C");
        chBoxD = new JCheckBox("D");

        chBoxA.addItemListener(new CheckBoxAHandler());
        chBoxB.addItemListener(new CheckBoxBHandler());
        chBoxC.addItemListener(new CheckBoxCHandler());
        chBoxD.addItemListener(new CheckBoxDHandler());

        labelA = new JLabel("A is ON "); labelA.setVisible(false);
        labelB = new JLabel("B is ON "); labelB.setVisible(false);
        labelC = new JLabel("C is ON "); labelC.setVisible(false);
        labelD = new JLabel("D is ON "); labelD.setVisible(false);
       
        add(chBoxA);
        add(chBoxB);
        add(chBoxC);
        add(chBoxD);
       
        add(labelA);
        add(labelB);
        add(labelC);
        add(labelD);
    }                     // end init
   
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == (Object) chBoxA)
            labelA.setVisible(chBoxA.isSelected());
        else if( source == (Object) chBoxA )
            labelB.setVisible(chBoxA.isSelected());

        } // end actionPerformed

    // inner classes for listener classes
    class CheckBoxAHandler implements ItemListener
    {
        public void itemStateChanged( ItemEvent e )
        {
            if( chBoxA.isSelected() )                                // One way to check if selected
                labelA.setVisible(true);
            else
                labelA.setVisible(false);
        } // end itemStateChanged for check box A
    } // end CheckBoxAHandler listener class

    class CheckBoxBHandler implements ItemListener
    {
        public void itemStateChanged( ItemEvent e )
        {
            if( e.getStateChange() == ItemEvent.SELECTED )             // another way to check if selected
                labelB.setVisible(true);
            else
                labelB.setVisible(false);
        } // end itemStateChanged  for check box B
    } // end CheckBoxBHandler listener class

    class CheckBoxCHandler implements ItemListener
    {
        public void itemStateChanged( ItemEvent e )
        {
            labelC.setVisible(chBoxC.isSelected());                 // BEST WAY
        } // end itemStateChanged for check box C
    } // end CheckBoxCHandler listener class
   
    class CheckBoxDHandler implements ItemListener
    {
        public void itemStateChanged(ItemEvent e)
        {
            labelD.setVisible(chBoxD.isSelected());
        }
    }
}



Comments