2012年7月23日月曜日

Java:Make Cell Editable When getting focus On JTable. JTable上のセルをフォーカスが移ったときに、編集可能にする。

JTableで、セルにフォーカスが移ったときに、日本語編集を有効にするには、 JTableを拡張して、「changeSelection」メソッドをオーバーライドする。 オーバーライドするには、「editCellAt」メソッドで選択したCellEditorを編集状態にして、 requestFocusInWindowで、セル内のEditorにフォーカスを移動させる。 (I refered to this page : Refered Page If you want to make the cell editable on JTable When cell's getting focus , override the method " changeSelection" of JTable Class as below codes.


import java.awt.Component;
import javax.swing.JTable;

・・・

String[][] tabledata = {
        {"10", "MIKE"},
        {"11", "JANE"};

String[] columnNames = {"ID", "NAME"};  

oTable = new JTable( tabledata  ,columnNames ) {

 private static final long serialVersionUID = 1L;

 @Override
 public void changeSelection(int row, int column, boolean toggle, boolean extend)
 {
     super.changeSelection(row, column, toggle, extend);
  
     if (editCellAt(row, column))
     {
         Component editor = getEditorComponent();
         editor.requestFocusInWindow();
     }
 }

}


・・・


0 件のコメント: