我得到一個JTable,其內容是類「item」的Vector。類項包含數字和字母數字字段以及適當的getter/setter,除了字段alt是字符串ArrayList。對於本專欄,我打算使用comboBox風格的渲染器和編輯器來選擇一個可用選項並顯示選項。 alt字段因行而異,這就是我所遇到的問題。我在這裏看到了很多這樣的渲染器的例子,但在所有例子中,每行的可用選項都是相同的。下面的代碼:在JTable中設置Combobox類型的渲染器/編輯器
public class item extends Vector {
private int no, code;
private String ....;
private List<String> alt;
@SuppressWarnings("unchecked")
public item(int no, int code, ...., List alt) {
super();
this.add(no);
this.add(code);
....
this.add(alt);
}
public int getNo() {
return (int) this.get(0);
}
public void setNo(int no) {
this.no = no;
}
// other setter/getters...
public void setAlt(List<String> alt) {
this.alt = alt;
}
@SuppressWarnings("unchecked")
public List<String> getAlt() {
return (List<String>) this.get(6);
}
@SuppressWarnings("all")
public Vector getAsVector() {
// ArrayList<Object> a= new ArrayList<Object>();
Vector a = new Vector(7);
a.add(no);
a.add(sifra);
a.add(red1);
a.add(red2);
a.add(sastojci);
a.add(eanKod);
a.add(alt);
return a;
}
}
JTable中獲取必要的功能:
public Class getColumnClass(int ci){
if (ci==6) return JComboBox.class;
else return String.class;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
item it = data.get(rowIndex);
switch (columnIndex) {
case 0:
it.setNo((int) value);
break;
case 1:
it.setCode((int) value);
break;
case 2:
....
case 6:
it.setAlt((List<String>) value);
break;
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object returnValue = null;
item it = data.get(rowIndex);
switch (columnIndex) {
case 0:
returnValue = it.getNo();
break;
case 1:
returnValue = it.getCode();
break;
case 2:
...
case 6:
returnValue = it.getAlt();
break;
}
return returnValue;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex > 0;
}
最後的渲染器/編輯器類:
String[] sval = {"alfa", "Beta", "Gamma"}; //dummy array
protected class ComboRenderer extends JComboBox implements TableCellRenderer {
public ComboRenderer(){
super(sval); //works, but this is not I want
// here should be used a similar super(list of options for a certain row), but how to get it?
// setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
this.setSelectedItem(items);
return this;
}
}
protected class ComboEditor extends AbstractCellEditor
implements TableCellEditor {
private List<String> alt;
protected ComboEditor() {
super();
}
@Override
public Object getCellEditorValue() {
return this.alt;
}
@SuppressWarnings("unchecked")
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value instanceof List) {
this.alt = (List<String>) value;
}
JComboBox<String> combo = new JComboBox<String>();
// combo.setEditable(true); //does not work for editable combobox!
for (String a : alt) {
combo.addItem(a);
}
combo.setSelectedItem(alt);
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
return combo;
}
}
而且這裏的問題:
1)渲染器對構造函數的錯誤部分(super(something))顯然不起作用。但在這部分我無法獲得渲染項目,所以我無法獲得初始化選項列表。如何解決這個問題?
2)編輯似乎工作正常,但當我從行中移動時,它再次顯示舊值。我應該在這裏保存些什麼?沒有新的數據,只是選擇可用於此項目的選項。
3)如何從外部達到某一行的選定值,比如打印?
4)如何獲得可編輯的Combobox而不是固定的?該生產線
// combo.setEditable(true); //does not work for editable combobox!
不會在這種情況下