[英]Cell in a JTable not editable when cell type is not String?
我有自己的TableModel
實現,旨在顯示SQL
數據庫中的數據。 我重寫了所有必要的方法,對列名使用String數組,對數據使用arraylist<Object[]>
對於可以從數據庫中檢索到的所有不同類型,使用Class<?>[]
數組。 我也有一個布爾數組,指示哪些列是可編輯的,哪些列不可編輯。 在我將表中的所有內容存儲為對象之前,還沒有實現類型部分,並且它運行良好。 現在,我已將類型添加到模型中,即使該列在我的布爾數組中是可編輯的,我也無法編輯int類型的任何列。 我已經重寫了isEditable()
方法,以簡單地從該布爾數組返回值,並且在有問題的in列上返回true,但是仍然無法編輯。 這是定義行為還是出問題了? 恐怕我目前無法發布代碼,因為我在用手機,我的筆記本電腦目前無法連接互聯網,並且要等到本周末結束。 我已經搜索過,但是Google只顯示了很多有關使單元格可編輯或不可編輯的問題,而不是為什么您不能編輯int列的問題。 編輯:這是顯示我的問題的pastebin: http : //pastebin.com/cYJnyyqy
使用jdk7
,即使isEditable()
對所有列返回true,也只有字符串列是可編輯的。
嗯。 我從未將原始類型(例如int.class
)用於getColumnClass()。 我一直使用“包裝”類型,例如Integer.class
。
嘗試更改您的Class<?>[] types
以使用包裝的類而不是基元。 例如
Class<?>[] types = {
String.class,
Character.class,
Integer.class,
...
Swing可能需要找到正確的Renderer / TableCellEditor。 但我不確定...
后續問題的答案
- 為什么char仍然不可編輯
原因是默認的通用編輯器:它只能處理具有以String為參數的構造函數的類,而Character則不能。 出路是Character類的特定自定義編輯器。
這是JTable.GenericEditor拋出的地方:
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row, int column) {
this.value = null;
((JComponent)getComponent()).setBorder(new LineBorder(Color.black));
try {
Class<?> type = table.getColumnClass(column);
// Since our obligation is to produce a value which is
// assignable for the required type it is OK to use the
// String constructor for columns which are declared
// to contain Objects. A String is an Object.
if (type == Object.class) {
type = String.class;
}
// JW: following line fails
constructor = type.getConstructor(argTypes);
}
catch (Exception e) {
// JW: so the editor returns a null
return null;
}
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
這是JTable處理null的地方:
// JTable.editCellAt(...)
TableCellEditor editor = getCellEditor(row, column);
if (editor != null && editor.isCellEditable(e)) {
editorComp = prepareEditor(editor, row, column);
if (editorComp == null) {
// JW: back out if the comp is null
removeEditor();
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.