Y07.java

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*; // for TreeSelectionListener
import java.util.*; // for Enumeration
import java.awt.*;
import java.awt.event.*;

/**
Simple demonstration of JTable with custom cell editor.
*/
public class Y07
{
    public static void main(String argv[])
    {
        JFrame f = new ZxFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.show();
    }
}

class ZxFrame extends JFrame
{
    public ZxFrame()
    {
        setTitle("Tables");
        setSize(400,200);
        setLocation(100,100);

        Object[][] cells = {
            { "Walt", new Integer(48), "5' 7\"", "Unknown", "Female"},
            { "Sharon", new Integer(51), "5' 0\"", "Human", "Female"},
            { "Daniel", new Integer(21), "5' 8\"", "Human", "Female"},
            { "Paul", new Integer(18), "5' 9\"", "Human", "Female"},
            { "Alan", new Integer(23), "5' 8\"", "Human", "Female"},
            { "Emily", new Integer(20), "5' 6\"", "Human", "Female"},
            { "Barnie", new Integer(12), "1' 0\"", "Cat", "Female"},
            { "Inkie", new Integer(11), "1' 2\"", "Cat", "Female"},
            };
        String[] colNames = {"Name","Age","Height","Species","Sex"};

        table = new JTable(cells,colNames);
        JScrollPane sp = new JScrollPane(table);
        getContentPane().add(sp,BorderLayout.CENTER);

        // Add custom cell editors.
        TableColumn spCol = table.getColumnModel().getColumn(3);
        JComboBox spBox = new JComboBox();
        spBox.addItem("Human");
        spBox.addItem("Cat");
        spBox.addItem("Dog");
        spBox.addItem("Crab");
        spBox.addItem("Fish");
        spBox.addItem("Bird");
        spBox.addItem("Unknown");
        spCol.setCellEditor(new DefaultCellEditor(spBox));

        TableColumn sxCol = table.getColumnModel().getColumn(4);
        JComboBox sxBox = new JComboBox();
        sxBox.addItem("Male");
        sxBox.addItem("Female");
        sxCol.setCellEditor(new DefaultCellEditor(sxBox));
    }

    private JTable table;
}