[英]Linking renderer that displays a jComboBox to a cell in the jTable
[英]Cell renderer class for a JComboBox in a JTable without parameters in the constructor
我正在為JTable中的JComboBox創建一個單元格渲染器。 這個類的構造函數不應該參數。 我有getTableCellRendererComponent方法的以下基本代碼:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,int row, int column)
{
if (value != null) {
removeAllItems();
value = value_to_string;
addItem(value);
if (isSelected) {
this.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
this.setForeground(table.getForeground());
this.setBackground(table.getBackground());
}
// Select the current value
this.setSelectedItem(value);
}
return this;
}
問題是我將作為一個值,而不是一個Object,一個String對象數組(String [])。我試圖使用String[] value_to_string = (String[]) value
; 但這會導致拋出異常錯誤。 正如我所說,構造函數中不應該有任何參數。 有人能找到解決這個問題的方法嗎? 提前致謝!
問題是我將一個String對象(String [])作為一個值,而不是一個Object。
然后模型中的數據是錯誤的。 TableModel應該只包含一個值。 它是從組合框中選擇的值。 String []僅由組合框編輯器使用,而不是渲染器。
你應該調整你的TableModel。
@Override
public Class<?> getColumnClass(final int col) {
return String[].class;
}
@Override
public Object getValueAt(final int row, final int col) {
String[] yourStringArray = // some code
return yourStringArray;
}
如果你這樣做,你可以像上面在渲染器中提到的那樣將Object轉換為String []。 String[] value_to_string = (String[]) value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.