listing 1
// Demonstrate JLabel and ImageIcon. 
import java.awt.*; 
import javax.swing.*; 
 
public class JLabelDemo { 
 
   public JLabelDemo() { 

    // Set up the JFrame.
    JFrame jfrm = new JFrame("JLabelDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(260, 210);

    // Create an icon. 
    ImageIcon ii = new ImageIcon("hourglass.png"); 
 
    // Create a label. 
    JLabel jl = new JLabel("Hourglass", ii, JLabel.CENTER); 
 
    // Add the label to the content pane. 
    jfrm.add(jl); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JLabelDemo(); 
        } 
      } 
    ); 
  } 
}

listing 2
// Demonstrate JTextField. 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
   
public class JTextFieldDemo { 

  public JTextFieldDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JTextFieldDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(260, 120); 
 
    // Add a text field to content pane. 
    JTextField jtf = new JTextField(15); 
    jfrm.add(jtf); 

    // Add a label.
    JLabel jlab = new JLabel();
    jfrm.add(jlab);

    // Handle action events.
    jtf.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
        // Show text when user presses ENTER. 
        jlab.setText(jtf.getText()); 
      } 
    }); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JTextFieldDemo(); 
        } 
      } 
    ); 
  }
}

listing 3
// Demonstrate an icon-based JButton. 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
   
public class JButtonDemo implements ActionListener { 
  JLabel jlab; 
 
  public JButtonDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JButtonDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(500, 450); 
 
    // Add buttons to content pane. 
    ImageIcon hourglass = new ImageIcon("hourglass.png"); 
    JButton jb = new JButton(hourglass); 
    jb.setActionCommand("Hourglass"); 
    jb.addActionListener(this); 
    jfrm.add(jb); 
 
    ImageIcon analog = new ImageIcon("analog.png"); 
    jb = new JButton(analog); 
    jb.setActionCommand("Analog Clock"); 
    jb.addActionListener(this); 
    jfrm.add(jb); 
 
    ImageIcon digital = new ImageIcon("digital.png"); 
    jb = new JButton(digital); 
    jb.setActionCommand("Digital Clock"); 
    jb.addActionListener(this); 
    jfrm.add(jb); 
 
    ImageIcon stopwatch = new ImageIcon("stopwatch.png"); 
    jb = new JButton(stopwatch); 
    jb.setActionCommand("Stopwatch"); 
    jb.addActionListener(this); 
    jfrm.add(jb); 
 
    // Create and add the label to content pane. 
    jlab = new JLabel("Choose a Timepiece"); 
    jfrm.add(jlab); 

    // Display the frame.
    jfrm.setVisible(true);
  } 
 
  // Handle button events. 
  public void actionPerformed(ActionEvent ae) { 
    jlab.setText("You selected " + ae.getActionCommand()); 
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JButtonDemo(); 
        } 
      } 
    ); 
  }
}

listing 4
// Demonstrate JToggleButton. 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
public class JToggleButtonDemo { 
 
  public JToggleButtonDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JToggleButtonDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(200, 100); 
 
    // Create a label. 
    JLabel jlab = new JLabel("Button is off.");  
 
    // Make a toggle button. 
    JToggleButton jtbn =  new JToggleButton("On/Off"); 
 
    // Add an item listener for the toggle button. 
    jtbn.addItemListener(new ItemListener() { 
      public void itemStateChanged(ItemEvent ie) { 
        if(jtbn.isSelected()) 
          jlab.setText("Button is on."); 
        else 
          jlab.setText("Button is off."); 
      } 
    }); 
  
    // Add the toggle button and label to the content pane.  
    jfrm.add(jtbn);   
    jfrm.add(jlab);  

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JToggleButtonDemo(); 
        } 
      } 
    ); 
  }
}

listing 5
// Demonstrate JCheckbox. 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
   
public class JCheckBoxDemo implements ItemListener { 
  JLabel jlab; 
 
  public JCheckBoxDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JCheckBoxDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(250, 100); 
 
    // Add check boxes to the content pane. 
    JCheckBox cb = new JCheckBox("C");  
    cb.addItemListener(this); 
    jfrm.add(cb); 
 
    cb = new JCheckBox("C++"); 
    cb.addItemListener(this); 
    jfrm.add(cb); 
 
    cb = new JCheckBox("Java"); 
    cb.addItemListener(this); 
    jfrm.add(cb); 
 
    cb = new JCheckBox("Perl"); 
    cb.addItemListener(this); 
    jfrm.add(cb); 
 
    // Create the label and add it to the content pane. 
    jlab = new JLabel("Select languages"); 
    jfrm.add(jlab); 

    // Display the frame.
    jfrm.setVisible(true);
  } 
 
  // Handle item events for the check boxes. 
  public void itemStateChanged(ItemEvent ie) { 
    JCheckBox cb = (JCheckBox)ie.getItem(); 
 
    if(cb.isSelected()) 
      jlab.setText(cb.getText() + " is selected"); 
    else 
      jlab.setText(cb.getText() + " is cleared"); 
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JCheckBoxDemo(); 
        } 
      } 
    ); 
  }
}

listing 6
// Demonstrate JRadioButton 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
   
public class JRadioButtonDemo implements ActionListener { 
  JLabel jlab; 
 
  public JRadioButtonDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JRadioButtonDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(250, 100); 
 
    // Create radio buttons and add them to content pane. 
    JRadioButton b1 = new JRadioButton("A"); 
    b1.addActionListener(this); 
    jfrm.add(b1); 
 
    JRadioButton b2 = new JRadioButton("B"); 
    b2.addActionListener(this); 
    jfrm.add(b2); 
 
    JRadioButton b3 = new JRadioButton("C"); 
    b3.addActionListener(this); 
    jfrm.add(b3); 
       
    // Define a button group. 
    ButtonGroup bg = new ButtonGroup(); 
    bg.add(b1); 
    bg.add(b2); 
    bg.add(b3); 
 
    // Create a label and add it to the content pane. 
    jlab = new JLabel("Select One"); 
    jfrm.add(jlab); 

    // Display the frame.
    jfrm.setVisible(true);
  } 
 
  // Handle button selection. 
  public void actionPerformed(ActionEvent ae) { 
    jlab.setText("You selected " + ae.getActionCommand()); 
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JRadioButtonDemo(); 
        } 
      } 
    ); 
  }
}

listing 7
// Demonstrate JTabbedPane. 
import javax.swing.*; 
import java.awt.*; 
 
public class JTabbedPaneDemo { 
 
  public JTabbedPaneDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JTabbedPaneDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(400, 200); 

    // Create the tabbed pane.
    JTabbedPane jtp = new JTabbedPane(); 
    jtp.addTab("Cities", new CitiesPanel()); 
    jtp.addTab("Colors", new ColorsPanel()); 
    jtp.addTab("Flavors", new FlavorsPanel()); 
    jfrm.add(jtp); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JTabbedPaneDemo(); 
        } 
      } 
    ); 
  }
} 
 
// Make the panels that will be added to the tabbed pane. 
class CitiesPanel extends JPanel { 
 
  public CitiesPanel() { 
    JButton b1 = new JButton("New York"); 
    add(b1); 
    JButton b2 = new JButton("London"); 
    add(b2); 
    JButton b3 = new JButton("Hong Kong"); 
    add(b3); 
    JButton b4 = new JButton("Tokyo"); 
    add(b4); 
  } 
} 
 
class ColorsPanel extends JPanel { 
 
  public ColorsPanel() { 
    JCheckBox cb1 = new JCheckBox("Red"); 
    add(cb1); 
    JCheckBox cb2 = new JCheckBox("Green"); 
    add(cb2); 
    JCheckBox cb3 = new JCheckBox("Blue"); 
    add(cb3); 
  } 
} 
 
class FlavorsPanel extends JPanel { 
 
  public FlavorsPanel() { 
    JComboBox<String> jcb = new JComboBox<String>(); 
    jcb.addItem("Vanilla"); 
    jcb.addItem("Chocolate"); 
    jcb.addItem("Strawberry"); 
    add(jcb); 
  } 
}

listing 8
// Demonstrate JScrollPane. 
import java.awt.*; 
import javax.swing.*; 
   
public class JScrollPaneDemo { 
 
  public JScrollPaneDemo() { 

    // Set up the JFrame.  Use the default BorderLayot.
    JFrame jfrm = new JFrame("JScrollPaneDemo");
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(400, 400);  

    // Create a panel and add 400 buttons to it. 
    JPanel jp = new JPanel(); 
    jp.setLayout(new GridLayout(20, 20)); 

    int b = 0; 
    for(int i = 0; i < 20; i++) { 
      for(int j = 0; j < 20; j++) { 
        jp.add(new JButton("Button " + b)); 
        ++b; 
      } 
    } 
 
    // Create the scroll pane. 
    JScrollPane jsp = new JScrollPane(jp); 
 
    // Add the scroll pane to the content pane. 
    // Because the default border layout is used, 
    // the scroll pane will be added to the center. 
    jfrm.add(jsp, BorderLayout.CENTER); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JScrollPaneDemo(); 
        } 
      } 
    ); 
  } 
}

listing 9
// Demonstrate JList. 
import javax.swing.*;  
import javax.swing.event.*; 
import java.awt.*; 
import java.awt.event.*; 
   
public class JListDemo { 

  // Create an array of cities. 
  String Cities[] = { "New York", "Chicago", "Houston", 
                      "Denver", "Los Angeles", "Seattle", 
                      "London", "Paris", "New Delhi", 
                      "Hong Kong", "Tokyo", "Sydney" }; 
 
  public JListDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JListDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(200, 200); 
 
    // Create a JList. 
    JList<String> jlst = new JList<String>(Cities); 
 
    // Set the list selection mode to single-selection. 
    jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
 
    // Add the list to a scroll pane. 
    JScrollPane jscrlp = new JScrollPane(jlst); 
 
    // Set the preferred size of the scroll pane. 
    jscrlp.setPreferredSize(new Dimension(120, 90)); 
 
    // Make a label that displays the selection. 
    JLabel jlab = new JLabel("Choose a City"); 
 
    // Add selection listener for the list. 
    jlst.addListSelectionListener(new ListSelectionListener() {  
      public void valueChanged(ListSelectionEvent le) {  
        // Get the index of the changed item. 
        int idx = jlst.getSelectedIndex(); 
 
        // Display selection, if item was selected. 
        if(idx != -1) 
          jlab.setText("Current selection: " + Cities[idx]); 
        else // Othewise, reprompt. 
          jlab.setText("Choose a City"); 
      }  
    });  
 
    // Add the list and label to the content pane. 
    jfrm.add(jscrlp); 
    jfrm.add(jlab); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JListDemo(); 
        } 
      } 
    ); 
  }
}

listing 10
// Demonstrate JComboBox. 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
   
public class JComboBoxDemo { 
  
  String timepieces[] = { "Hourglass", "Analog", "Digital", "Stopwatch" }; 
 
  public JComboBoxDemo() { 
 
    // Set up the JFrame.
    JFrame jfrm = new JFrame("JCombBoxDemo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(400, 250); 
 
    // Instantiate a combo box and add it to the content pane. 
    JComboBox<String> jcb = new JComboBox<String>(timepieces); 
    jfrm.add(jcb); 
 
    // Create a label and add it to the content pane. 
    JLabel jlab = new JLabel(new ImageIcon("hourglass.png")); 
    jfrm.add(jlab); 

    // Handle selections. 
    jcb.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
        String s = (String) jcb.getSelectedItem(); 
        jlab.setIcon(new ImageIcon(s + ".png")); 
      } 
    }); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JComboBoxDemo(); 
        } 
      } 
    ); 
  }
}

listing 11
// Demonstrate JTree. 
import java.awt.*; 
import javax.swing.event.*; 
import javax.swing.*; 
import javax.swing.tree.*; 
 
public class JTreeDemo { 
 
  public JTreeDemo() { 
 
    // Set up the JFrame. Use default BorderLayout.
    JFrame jfrm = new JFrame("JTreeDemo");
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(200, 250); 

    // Create top node of tree. 
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options"); 
 
    // Create subtree of "A". 
    DefaultMutableTreeNode a = new DefaultMutableTreeNode("A"); 
    top.add(a); 
    DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1"); 
    a.add(a1); 
    DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2"); 
    a.add(a2); 
 
    // Create subtree of "B". 
    DefaultMutableTreeNode b = new DefaultMutableTreeNode("B"); 
    top.add(b); 
    DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1"); 
    b.add(b1); 
    DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2"); 
    b.add(b2); 
    DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3"); 
    b.add(b3); 
 
    // Create the tree. 
    JTree tree = new JTree(top); 
 
    // Add the tree to a scroll pane. 
    JScrollPane jsp = new JScrollPane(tree); 
 
    // Add the scroll pane to the content pane. 
    jfrm.add(jsp); 
 
    // Add the label to the content pane. 
    JLabel jlab = new JLabel(); 
    jfrm.add(jlab, BorderLayout.SOUTH); 
 
    // Handle tree selection events. 
    tree.addTreeSelectionListener(new TreeSelectionListener() { 
      public void valueChanged(TreeSelectionEvent tse) { 
        jlab.setText("Selection is " + tse.getPath()); 
      } 
    }); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JTreeDemo(); 
        } 
      } 
    ); 
  }
}

listing 12
// Demonstrate JTable. 
import java.awt.*; 
import javax.swing.*; 
 
public class JTableDemo { 
 
  // Initialize column headings. 
  String[] colHeads = { "Name", "Extension", "ID#" }; 
 
  // Initialize data. 
  Object[][] data = { 
    { "Gail", "4567", "865" }, 
    { "Ken", "7566", "555" }, 
    { "Viviane", "5634", "587" }, 
    { "Melanie", "7345", "922" }, 
    { "Anne", "1237", "333" }, 
    { "John", "5656", "314" }, 
    { "Matt", "5672", "217" }, 
    { "Claire", "6741", "444" }, 
    { "Erwin", "9023", "519" }, 
    { "Ellen", "1134", "532" }, 
    { "Jennifer", "5689", "112" }, 
    { "Ed", "9030", "133" }, 
    { "Helen", "6751", "145" } 
  }; 

  public JTableDemo() { 

    // Set up the JFrame. Use default BorderLayout.
    JFrame jfrm = new JFrame("JTableDemo");
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setSize(300, 300); 

    // Create the table. 
    JTable table = new JTable(data, colHeads); 
 
    // Add the table to a scroll pane. 
    JScrollPane jsp = new JScrollPane(table); 
 
    // Add the scroll pane to the content pane. 
    jfrm.add(jsp); 

    // Display the frame.
    jfrm.setVisible(true);
  } 

  public static void main(String[] args) { 

    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( 
      new Runnable() { 
        public void run() { 
          new JTableDemo(); 
        } 
      } 
    ); 
  }
}


