posts - 41,  comments - 40,  trackbacks - 0

import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.DefaultCellEditor;

import javax.swing.event.ChangeEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import javax.swing.table.TableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;


public class WhoFirstTest
{
  public static void main( String[]args )
  {
    final Object[][] data=new Object[][]{ { "11", new Integer( 1 ) }, { "22", new Integer( 2 ) } };

    final String[] header=new String[]{ "表头1", "表头2" };

    Table table=new Table( new BaseModel( data, header ) );

    JTextField editor=new JTextField();
    editor.setBorder( null );
    table.setDefaultEditor( Object.class, new BaseEditor( editor ) );

    table.setDefaultRenderer( Object.class, new BaseRenderer() );

    table.getModel().addTableModelListener( new TableModelListener()
    {
      public void tableChanged( TableModelEvent e )
      {
        System.out.println( "tableChanged called" );
        /*/
        System.out.println( "FirstRow="+e.getFirstRow() );
        System.out.println( "LastRow="+e.getLastRow() );
        System.out.println( "Column="+e.getColumn() );
        System.out.println( "Type="+( ( e.getType()==TableModelEvent.INSERT ) ? "Insert" : ( e.getType()==TableModelEvent.UPDATE ) ? "Update" : ( e.getType()==TableModelEvent.DELETE ) ? "Delete" : "Unknow" ) );
        /*/
        //Do CRUD for DB ...
        //*/
      }
    } );

    JFrame frame=new JFrame( "看看给单元格赋值的顺序" );
    frame.setContentPane( new JScrollPane( table ) );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.pack();
    frame.setVisible( true );
  }
}


class Table extends JTable
{
  public Table( TableModel model )
  {
    super( model );
  }

  public void editingStopped( ChangeEvent e )
  {
    System.out.println( "editingStopped called" );
    super.editingStopped( e );
  }

  public Component prepareRenderer( TableCellRenderer renderer, int row, int column )
  {
    System.out.println( "prepareRenderer called" );
    return super.prepareRenderer( renderer, row, column );
  }
}


class BaseModel extends AbstractTableModel
{
  Object[][] data = null;
  Object[] header = null;

  public BaseModel( Object[][] data, Object[] header )
  {
    this.data   = data;
    this.header = header;
  }

  public String getColumnName( int column )
  {
    return header[column].toString();
  }

  public int getRowCount()
  {
    return data.length;
  }

  public int getColumnCount()
  {
    return header.length;
  }

  public Object getValueAt( int row, int col )
  {
    return data[row][col];
  }

  public boolean isCellEditable( int row, int column )
  {
    return true;
  }

  public void setValueAt( Object value, int row, int col )
  {
    System.out.println( "setValueAt called" );
    data[row][col]=value;
    fireTableCellUpdated( row, col );
  }

  public void fireTableCellUpdated( int row, int column )
  {
    super.fireTableCellUpdated( row, column );
    System.out.println( "fireTableCellUpdated called" );
  }

  public void fireTableChanged( TableModelEvent e )
  {
    super.fireTableChanged( e );
    System.out.println( "fireTableChanged called" );
  }
}


class BaseRenderer extends DefaultTableCellRenderer
{
  public BaseRenderer()
  {
    super();
  }

  public Component getTableCellRendererComponent( JTable table,
                                                  Object value,
                                                  boolean isSelected,
                                                  boolean hasFocus,
                                                  int row,
                                                  int column )
  {
    System.out.println( "BaseRenderer called" );

    setText( ( value==null ) ? "" : value.toString() );

    return this;
  }
}


class BaseEditor extends DefaultCellEditor
{
  public BaseEditor( JTextField editor )
  {
    super( editor );
  }

  public boolean stopCellEditing()
  {
    System.out.println( "stopCellEditing called" );

    String value=(String)getCellEditorValue();

    try{ Integer.parseInt( value );
       }
    catch( NumberFormatException e )
         {
           this.editorComponent.requestFocus();
           return false;
         }

    return super.stopCellEditing();
  }
}

posted on 2007-07-03 04:14 NeedJava 阅读(1141) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: