import java.util.*;import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.Robot;/**
 * <p>Title: 重写JTABLE</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: GDHIT</p>
 *
 */
public class ListInputTable
    extends JTable {
  final String[] title = {
      "序号", "选择", "费用名称", "自付比例", "合计金额", "费用类型", "自费金额", "列入医保"};
  Object[] row = {
      new Integer(1), new String(), new String(), new Float(0), new Float(0),
      new String(), new Float(0), new Float(0)};
  Object[][] data = {
      row, };
  public ListInputTable() {
    super();
    try {
      init();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }  public void selectByAll(boolean b) {  }  /**
   *
   * @throws Exception 没有选择记录
   */
  public void selectDel() throws Exception {  }  /**
   * 输入明细名称时是否提示
   * @param b boolean
   */
  public void isPrompt(boolean b) {  }  private void init() throws Exception {
    this.setModel(new DefaultTableModel(data, title) {
      public boolean isCellEditable(int row,
                                    int column) {
        if (column > 1 && column < 5) {
          return true;
        }
        else {
          return false;
        }
      }
    });
    this.setRowSelectionAllowed(false);
    this.setAutoResizeMode(this.AUTO_RESIZE_OFF);
//    this.getColumnModel().getColumn(1).getCellRenderer().getTableCellRendererComponent(this,"",true,true,this.getSelectedRow(),this.getSelectedColumn()).seth
    this.getColumnModel().getColumn(0).setPreferredWidth(40);
    this.getColumnModel().getColumn(1).setPreferredWidth(40);
    this.getColumnModel().getColumn(2).setPreferredWidth(200);
    this.getColumnModel().getColumn(3).setPreferredWidth(60);
    this.getColumnModel().getColumn(4).setPreferredWidth(60);
    this.getColumnModel().getColumn(5).setPreferredWidth(100);
    this.getColumnModel().getColumn(6).setPreferredWidth(60);
    this.getColumnModel().getColumn(7).setPreferredWidth(60);
    this.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        ListInputTable lit = (ListInputTable) e.getSource();
        int row = lit.getSelectedRow();
        int col = lit.getSelectedColumn();
        if (col == 1) {
          lit.setValueAt(lit.getValueAt(row, col).toString().equals("") ? "√" :
                         "", row, col);
        }
      }
    });
    this.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        ListInputTable lit = (ListInputTable) e.getSource();
        int row = lit.getSelectedRow();
        int col = lit.getSelectedColumn();
        if (col > 1 && col < 5) {
          if (e.getKeyCode() == e.VK_ENTER) {
            if (col == 4) {
              ( (DefaultTableModel) getOwner().getModel()).addRow(getOwner().
                  row);
              getOwner().selectCell(row+1, 2);//在新增加一行,移动到输入位置
            }
            else {
              getOwner().selectCell(row, col);
            }
          }
        }
      }
    });
    selectCell(0, 2);
  }  private ListInputTable getOwner() {
    return this;
  }  public void selectCell(int row, int col) {
    this.setRowSelectionInterval(row, row);
    this.setColumnSelectionInterval(col, col);
  }  public static void main(String[] args) {
    ListInputTable listinputtable = new ListInputTable();
  }
}

解决方案 »

  1.   

    接上面的,下面是个测试类:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Rectangle;
    import javax.swing.border.TitledBorder;
    import com.gdhit.cash.ui.ListInputTable;
    /**
     * <p>Title: 测试类</p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: GDHIT</p>
     *
     */
    public class Frame2
        extends JFrame {
      public Frame2() {
        try {
          jbInit();
          this.setExtendedState(this.MAXIMIZED_BOTH);
          this.setVisible(true);
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }  private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jScrollPane1.setBounds(new Rectangle(28, 25, 628, 239));
        this.getContentPane().add(jScrollPane1);    jScrollPane1.getViewport().add(jTable1);
      }  TitledBorder titledBorder1 = new TitledBorder("asd");
      JScrollPane jScrollPane1 = new JScrollPane();
      ListInputTable jTable1 = new ListInputTable();
      public static void main(String[] args) {
        new Frame2();
      }
    }
      

  2.   

    this.addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            ListInputTable lit = (ListInputTable) e.getSource();
            int row = lit.getSelectedRow();
            int col = lit.getSelectedColumn();
            System.out.println("*******"+row+" | "+col+"******");
            if (col >1 && col <5) {
              if (e.getKeyCode() == e.VK_ENTER) {
                if (col == 4) {
                  ( (DefaultTableModel) getOwner().getModel()).addRow(getOwner().row);
                  col=2;
                  getOwner().selectCell(row, col);//在新增加一行,移动到输入位置
                  row=row+1;
                  System.out.println("下一行 "+row+" | " +col);            }else {
                 row=row-1;
                 col=col+1;
                   getOwner().selectCell(row, col);
                   row=row+1;
                    System.out.println("下一个 "+row+" | " +col);            }
              }
            }
          }
        });
    这样就可以了,其中的注释,你运行以下看的
      

  3.   

    ukeychen() 的代码不行,回车就抛错误!
      

  4.   

    我又改了一下: this.addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            ListInputTable lit = (ListInputTable) e.getSource();
            int row = lit.getSelectedRow();
            int col = lit.getSelectedColumn();
            //System.out.println("*******"+row+" | "+col+"******");
            if (col >1 && col <5) {
              if (e.getKeyCode() == e.VK_ENTER) {
                if (col == 4) {
                if(row+1==getOwner().getModel().getRowCount()){ 
                             ( (DefaultTableModel) getOwner().getModel()).addRow(getOwner().row); //如果是最后一行,就增加一行,否则不增加行
                }
                col=2;//调整列位置
       getOwner().selectCell(row, col);
       //row=row+1;
       //System.out.println("下一行 "+row+" | " +col);            }else {
                 if (row==0){ //如果是第一行,不调整,
                       //如果就只有一行的数据可以正常运行,
                       //如果数据在2行以上,而且当前位置是(0,col)
                                       //那么运行后,将跳到(1,col)
                 getOwner().selectCell(0, col);
                 }else{ //非第一行,对row和col进行相应处理
                 row=row-1;
                 col=col+1;
                 getOwner().selectCell(row, col);
               }
                   //row=row+1;
                    //System.out.println("下一个 "+row+" | " +col);            }
              }
            }
          }
        });
        selectCell(0, 2);
      }
    这样一般都行,但是还有个小毛病,就是你将数据增加到大于等于 2行 时,
    你把光标在移动到第一行,然后在按回车键,
    这时,它不是移动到第一行的下一列单元格,
            而是移动到下一行的当前列单元格其他功能正常至于出现这个问题,主要出在以下这两个函数上
        setRowSelectionInterval(row, row);
        setColumnSelectionInterval(col, col);这两个函数,都对row和col做了相应的处理我现在只不过是,将其调回来而已
      

  5.   

    ukeychen 实在对不起,我给错分了
    -_-