Thursday, December 20, 2012

Java Tutorial 49 Source Code

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); } }

2 comments:

  1. i'm getting error when using code. got an error on public boolean isCellEditable and public Component also Component c. even though i imported all that was needed.

    ReplyDelete