Please send questions to
st10@humboldt.edu .
/**
* our first JTable example;
* adapted from Horstmann, Cornell, "Core Java 2 - Volume 2 - Advanced
* Features", pp.360-361
*
* modified by: Sharon M. Tuttle
* last modified: 2-18-01
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// note! tables are in a DIFFERENT package
import javax.swing.table.*;
public class TryJTable1
{
// again, main just sets up an instance of an
// "internal" extension of JFrame
public static void main(String args[])
{
JFrame frame = new TryJTable1Frame();
frame.show();
}
}
// here's that extension of JFrame used in main()
class TryJTable1Frame extends JFrame
{
// you can declare the column names for the table
// to be built as an array of String's
// (these are directly from Horstmann and Cornell)
private String columnNames[] =
{
"Planet", "Radius", "Moons", "Gaseous", "Color"
};
// you can declare the rows for the table
// as an array of arrays --- array of arrays of objects;
// (values here also directly from Horstmann and Cornell)
private Object cells[][] =
{
// notice the different types of objects in the different
// columns;
{"Mercury", new Double(2440), new Integer(0), Boolean.FALSE,
Color.yellow
},
{"Venus", new Double(6052.33333), new Integer(0), Boolean.FALSE,
Color.red
}
// continued...
};
// here's the constructor for this JFrame extension
public TryJTable1Frame()
{
JTable table;
Container contentPane;
setTitle("TryJTable1");
setSize(1000, 100);
// thou shalt exit properly when frame is closed...
addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
// instantiate this JTable with given contents,
// columns (array of array of objects, array of Strings;
// for other constructors, see Java API)
table = new JTable(cells, columnNames);
contentPane = getContentPane();
// notice the anonymous declaration of a
// JScrollPane to hold the table, and
// "Center" to indicate the center location
// of contentPane's BorderLayout
contentPane.add(new JScrollPane(table), "Center");
}
}