www.pudn.com > TableGrid.zip > TableGrid.java


package TableGrid; 
 
import javax.swing.JTable; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.JScrollPane; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.JOptionPane; 
import javax.swing.JApplet; 
import java.awt.*; 
import java.awt.event.*; 
 
public class TableGrid extends JApplet  
{ 
   private boolean DEBUG = true; 
 
   public void init() 
	{ 
        MyTableModel myModel=new MyTableModel(); 
		JTable table = new JTable(myModel); 
		table.setPreferredScrollableViewportSize(new Dimension(500,700)); 
		JScrollPane scrollPane=new JScrollPane(table); 
		setContentPane(scrollPane); 
	} 
	class MyTableModel extends AbstractTableModel 
	{ 
		final String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"}; 
        final Object[][] data={ 
			{"Mary","Campione","Snowboarding",new Integer(5),new Boolean(false)}, 
			{"Alison","Huml","Rowing",new Integer(3),new Boolean(true)}, 
			{"Kathy","Walrath","Chasing toddlers",new Integer(5),new Boolean(false)} 
		}; 
        public int getColumnCount() 
		{ 
			return columnNames.length; 
		} 
		public int getRowCount() 
		{ 
			return data.length; 
		} 
		public String getColumnName(int col) 
		{ 
			return columnNames[col]; 
		} 
		public Object getValueAt(int row,int col) 
		{ 
			return data[row][col]; 
		} 
		public Class getColumnClass(int c) 
		{ 
			return getValueAt(0,c).getClass(); 
		} 
       public boolean isCellEditable(int row,int col) 
		{ 
	/*	   if (col<2) 
		   { 
			   return false; 
		   }else 
			{ 
				   return true; 
			}*/ 
			return false; 
		} 
		public void setValueAt(Object value,int row,int col) 
		{ 
			 if (DEBUG) 
			 { 
				 System.out.println("Setting Value at "+row+","+col+"to"+value+"(an instance of"+value.getClass()+")"); 
			 } 
			 data[row][col]=value; 
			 fireTableCellUpdated(row,col); 
			 if (DEBUG) 
			 { 
				 System.out.println("New value of data:"); 
				 printDebugData(); 
			 } 
		} 
		private void printDebugData() 
		{ 
			int numRows = getRowCount(); 
			int numCols = getColumnCount(); 
			for (int i=0;i