2012年8月14日火曜日

Java: Get the component Of cell editor in JTable

JTableのセルに設定してある、コンボボックスなどのコンポーネントを取得するのに、getCellEditor(行数、列数)を実行すると、そのとき選択されている行のコンポーネントが取得され、行数は無関係である。 その理由は、もとのコードにおいて、行数は何ら機能していないから。

We can't get the valid component from a cell in JTable by "JTable.getCellEditor(row , col)". I think it's the original source's bug.
Argument "row" is not used.


 source

javax.swing.JTable    

 public TableCellEditor getCellEditor(int row, int column) {
        TableColumn tableColumn = getColumnModel().getColumn(column);
        TableCellEditor editor = tableColumn.getCellEditor();
        if (editor == null) {
            editor = getDefaultEditor(getColumnClass(column));
        }
        return editor;
    }


これを、指定行数の指定列のコンポーネントを取得するには、セルを選択して、getCellEditorを実行する。 そうすると、指定行数のコンポーネントが取得できる。もっと違うやり方があるかもしれないけど。

If you want to get the component form cell editor in JTable, select each cells and execute "getCellEditor".



int rowQty = oJTable.getTableModel().getRowCount();
int colQty = oJTable.getTableModel().getColumnCount();
int i = 0;
int j = 0;

oJTable.setColumnSelectionAllowed(false);
oJTable.setRowSelectionAllowed(false);  
  
oJTable.setVisible(false);
for(i=0;i
  for(j=0;j
     oJTable.changeSelection(i, j, false, false);
     Component oComponent = ((DefaultCellEditor)oJTable.getCellEditor(i , j)).getComponent();
  }
}
oJTable.setVisible(true);

 

0 件のコメント: