Applet and Swing Components : Applet « Swing JFC « Java

Applet and Swing Components

Applet and Swing Components

// :
// <applet code=List width=250 height=375></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// See copyright notice in CopyRight.txt.

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class List extends JApplet {
  private String[] flavors = { "Chocolate", "Strawberry",
      "Vanilla Fudge Swirl", "Mint Chip", "Mocha Almond Fudge",
      "Rum Raisin", "Praline Cream", "Mud Pie" };

  private DefaultListModel lItems = new DefaultListModel();

  private JList lst = new JList(lItems);

  private JTextArea t = new JTextArea(flavors.length, 20);

  private JButton b = new JButton("Add Item");

  private ActionListener bl = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      if (count < flavors.length) {
        lItems.add(0, flavors[count++]);
      } else {
        // Disable, since there are no more
        // flavors left to be added to the List

  private ListSelectionListener ll = new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
      if (e.getValueIsAdjusting())
      Object[] items = lst.getSelectedValues();
      for (int i = 0; i < items.length; i++)
        t.append(items[i] + "\n");

  private int count = 0;

  public void init() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    // Create Borders for components:
    Border brd = BorderFactory.createMatteBorder(1, 1, 2, 2, Color.BLACK);
    // Add the first four items to the List
    for (int i = 0; i < 4; i++)
    // Add items to the Content Pane for Display
    // Register event listeners

  public static void main(String[] args) {
    run(new List(), 250, 375);

  public static void run(JApplet applet, int width, int height) {
    JFrame frame = new JFrame();
    frame.setSize(width, height);
} ///:~


Related examples in the same category

1.Icon Demo Applet
2.Socket Applet
3.Applet Socket Quote
4.Applet Print
5.Applet System Properties
6.Applet Sound
7.Show Document
8.Applet communication (talk to each other)
9.Get Applets
10.JDBC applet
11.An application and an appletAn application and an applet
12.Icon behavior in JbuttonsIcon behavior in Jbuttons
13.Signed Applet
14.Load resource in Applet
15.Scribble AppletScribble Applet
16.Toggle buttonToggle button
17.Bouncing CircleBouncing Circle
18.Applet Menu Bar Demo
19.Applet clock demoApplet clock demo
20.Applet event testerApplet event tester
21.Applet with parameters
22.First applet
23.AppletViewer - a simple Applet Viewer program
24.A simple loan calculator appletA simple loan calculator applet
25.URLButton -- a Button-like object that jumps to a URL
26.Get list of APPLET tags in one HTML file
27.Demonstration of Applet Methods
28.Demonstrates getParameterInfo() and getAppletInfo()
29.Walking Text Demo
30.Java Applets Can Run CGI's (on some browsers)
31.Demo Choice Applet
32.Demo Applet: Connect to Legacy System
33.ShowDocApplet: try out showDocument()
35.Java Wave Applet Demo
36.Slide Puzzle
37.A class to allow use of the ncsa ISMAP format in java applets
38.Image Loader Applet
39.Thread Race Applet
40.Applet: Print from an Applet
41.Calling methods of an Applet from JavaScript code
42.Read an applet parameters
43.Change an applet background color
44.Passing Parameters to Java Applet
45.Display message in browser status bar
46.Your own Applet runtime environment
47.This applet is a simple button that plays an audio clip when pushed
48.EventQueue.invokeLater and JApplet
49.An applet component that draws a bar chart
50.This applet displays a greeting from the authors