import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class Tutorial extends JPanel
{
JTable jt;
public Tutorial()
{
String[] columns = {"Name", "Age", "Gender"};
String[][] data = {{"John", "18", "Male"},
{"Daisy", "19", "Female"},
{"Dave", "23", "Male"},
{"Jake", "30", "Male"}};
jt = new JTable(data, columns)
{
public boolean isCellEditable(int data, int columns)
{
return false;
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns)
{
Component c = super.prepareRenderer(r, data, columns);
if (data % 2 == 0)
c.setBackground(Color.WHITE);
else
c.setBackground(Color.LIGHT_GRAY);
return c;
}
};
jt.setPreferredScrollableViewportSize(new Dimension(450, 63));
jt.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jt);
add(jps);
}
public static void main(String[] args)
{
JFrame jf = new JFrame("Tutorial");
Tutorial t = new Tutorial();
jf.setSize(500, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(t);
}
}