Swing Components

 

Top-Level Containers

The components at the top of any Swing containment hierarchy.

JApplet JDialog JFrame
  JOptionPane image  

 

 

General-Purpose Containers

Intermediate containers that can be used under many different circumstances.

JPanel JScrollPane JSplitPane
  JScrollPane image JSplitPane image
JTabbedPane Box  
JTabbedPane image    

 

 

Special-Purpose Containers

Intermediate containers that play specific roles in the UI.

JInternalFrame JLayeredPane JRootPane
     
JToolBar JDeskTopPane  
JToolBar image    

 

 

Basic Controls

Regular components that exist primarily to get input from the user; they generally also show simple state.

JButtons JComboBox JList
JButton image JComboBox image JList image
JMenu JSlider JTextField
JMenu image JSlider image JTextField image
JCheckBox    
     

 

 

Uneditable Information Displays

Regular components that exist solely to give the user information.

JLabel JProgressBar JToolTip
JLabel image JProgressBar image JToolTip image

 

 

 

Editable Displays of Formatted Information

Regular components that display highly formatted information that (if you choose) can be edited by the user.

JColorChooser JFileChooser JTable
    JTable image
JText JTree  
JTextArea image JTree image  

 

 

Learning By Example

spbook.jpg (29453 bytes)

 

Intended to be a Messy Example for learning ...


//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
                               //Swing releases before Swing 1.1 Beta 3.

import javax.swing.*;          //This is the final real package name.
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.undo.*;

import java.awt.*;             //The awt event model are still valid
import java.awt.event.*;
import java.io.IOException;


public class SPbook
{

	private String newline = System.getProperty("line.separator");
	private JFrame frame;
        private JLabel label;	
	

	private String infoStr;

	// Buttons
	private JButton addButton;
	private JButton deleteButton;
	private JButton prevButton;
	private JButton nextButton;
	private JButton openButton;
	private JButton saveButton;
	private JButton finishButton;

	final JTextArea textarea = new JTextArea(22, 40);
	private JComboBox statusList;
	JTextPane textPane = new JTextPane();

	

	// Textfields
	private JTextField	nameField, phoneField, namefField,
						addr1Field, addr2Field, cityField, stateField,
						zipField, emailField, urlField;
	
	private static String labelPrefix = "  Total: ";

	
	/*
	 * Utility function for adding GridBag
	 */
	private void gAdd(JComponent p, JComponent c, GridBagConstraints gbc, 
					 int x, int y, int w, int h)
	{
		// ... some coding in here ... 
	}

	/*
	 * Create my label
	 */
	private JLabel myLabel(String lstr)
	{
		JLabel label = new JLabel(lstr);
		Font f = new Font("ScansSerif", Font.BOLD | Font.ITALIC, 14);
		label.setFont(f);
		return label;
	}


	/*
	 * Routine to setup UI components
	 */
	private void spbInit()
	{

		// Choice for combo box
		String[] statusStrings = { "Work Full Time", "Senior", "Junior", "Sophmore", "Freshmen", "CS Grad", "Other Grad" };

		// Create the combo box and default to Senior
		statusList = new JComboBox(statusStrings);
		statusList.setSelectedIndex(1);
		// statusList.addActionListener(this);


		// Create the top-level container.
		frame = new JFrame("Swing Info Book Main Frame");

		//
		// Buttons code
		//
	        // Add
		addButton = new JButton("Add", new ImageIcon("bPenIcon.gif"));
        	addButton.setMnemonic('a');

		// Setup the label
	        label = new JLabel(labelPrefix + "0    ");
        	label.setLabelFor(addButton);

		// Create the action listenser
		SPbActionChanger sac = new SPbActionChanger(label);
	        addButton.addActionListener(sac);

		// Delete
		deleteButton = new JButton(	"Delete");
		deleteButton.setMnemonic('d');
		
		// Prev
		prevButton = new JButton(	" Prev ");
		prevButton.setMnemonic('p');

		// Next
		nextButton = new JButton(	" Next ");
		nextButton.setMnemonic('n');

		// Open
		openButton = new JButton(	" Open ");
		openButton.setMnemonic('o');

		// Save
		saveButton = new JButton(	" Save ");
		saveButton.setMnemonic('s');


		// Init the record information
		nameField  = new JTextField();
		phoneField = new JTextField();
		namefField = new JTextField();
		addr1Field = new JTextField();
		addr2Field = new JTextField();
		cityField = new JTextField();
		stateField = new JTextField();
		zipField = new JTextField();
		emailField = new JTextField();
		urlField = new JTextField();

		JPanel p = new JPanel();
		GridBagLayout gbl = new GridBagLayout();
		p.setLayout(gbl);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.WEST;
		gbc.weightx = 10;
		gbc.weighty = 10;
		JComponent c;

		/*
		 * Set the GridBagConstraints for each of the elements 
		 * display in the applet
		 */		
		// Set the First name
		gAdd(p, myLabel("  First name"),			gbc, 0, 0, 1, 1);
		gAdd(p, namefField,							gbc, 1, 0, 2, 1);

		// Set the Last name
		gAdd(p, myLabel("  Last name"),				gbc, 0, 1, 1, 1);
		gAdd(p,	nameField,							gbc, 1, 1, 2, 1);

		// Status
		gAdd(p, myLabel("  Status  "),				gbc, 0, 2, 1, 1);
		gAdd(p, statusList,							gbc, 1, 2, 1, 1);

		// Email
		gAdd(p, myLabel("  Email   "),				gbc, 0, 3, 1, 1);
		gAdd(p,	emailField,							gbc, 1, 3, 1, 1);

		// URL
		gAdd(p, myLabel("  URL     "),				gbc, 0, 4, 1, 1);
		gAdd(p, urlField,							gbc, 1, 4, 2, 1);

		// Set for phone number
		gAdd(p, myLabel("  Phone"),					gbc, 0, 5, 1, 1);
		gAdd(p, phoneField,							gbc, 1, 5, 1, 1);

		// Address
		gAdd(p, myLabel("  Address  "),				gbc, 0, 6, 1, 1);
		gAdd(p, addr1Field,							gbc, 1, 6, 3, 1);

		// Street Address2
		gAdd(p, myLabel("           "),				gbc, 0, 7, 1, 1);
		gAdd(p,	addr2Field,							gbc, 1, 7, 3, 1);

		// City
		gAdd(p, myLabel("  City "),					gbc, 0, 8, 1, 1);
		gAdd(p, cityField,							gbc, 1, 8, 1, 1);
		
		// State and ZIP
		gAdd(p, myLabel("  State"),					gbc, 0, 9, 1, 1);
		gAdd(p, stateField,							gbc, 1, 9, 1, 1);
		gAdd(p, myLabel("    Zip"),					gbc, 2, 9, 1, 1);
		gAdd(p, zipField,							gbc, 3, 9, 1, 1);

		gAdd(p, label,								gbc, 0, 14, 3, 1);

		// Set for Buttons
		gbc.fill = GridBagConstraints.BOTH;
		gAdd(p, addButton,							gbc, 0, 15, 1, 1);		
		gAdd(p, deleteButton,						gbc, 1, 15, 1, 1);
		gAdd(p, prevButton,							gbc, 2, 15, 1, 1);
		gAdd(p, nextButton,							gbc, 3, 15, 1, 1);
		
		/*
		 * Create the tabbed pane
		 */
		Container contents = frame.getContentPane();
		JTabbedPane jtp = new JTabbedPane();
		// Set border for top, left, bottom, right 
		jtp.setBorder(BorderFactory.createEmptyBorder(0, 3, 3, 3));
		jtp.addTab(" Main Info ", p);
		jtp.addTab(" Computer Background ", myUndoTPane());
		jtp.addTab(" Expectation ", myEditTextPane());
		jtp.addTab(" Finish-> ", myFinishPane());
		contents.add(jtp);
   
        //Finish setting up the frame, and show it.
        frame.addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e) 
            {
                System.exit(0);
            }
        });

		frame.setSize(500, 500);
        frame.setVisible(true);

	}

	/*
	 * This is the main constructor for setting up all the components
	 */
	public SPbook() 
    	{
		spbInit();
    	}

    
	/*
	 * Main line of this program
	 */
	public static void main(String[] args) 
	{
		/*
		 * Set the UI component look and feel
		 */
        try 
        {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } 
		catch (Exception e) 
        {
            // Skip the exception and just get out for this sample program 
        }

        new SPbook(); //Create and show the GUI.
    }
}