我看了這裏的幾個帖子,但無法找到答案。將JComboBox添加到JTable單元格。選擇的項目不留
我已成功將JComboBox添加到Jtable單元格。但是選定的項目不被「記住」。
更具體地:
1)從行的組合框中選擇一個項目A
2)轉到選擇行B的組合框,但在A行所顯示的項消失(任何時候焦點丟失,行A組合框選擇消失)
3)回到A行,選擇被記住。
只是要清除信息是不是正在丟失。雖然組合框不能直觀地顯示選擇了哪些設備,但選擇已保存。但是,我希望用戶能夠直觀地看到他們剛剛選擇的內容,而不必再次選擇該行。
這使我相信一定會有與我用
這裏是下面的相關代碼自定義渲染錯誤。
TableModel的(請注意,Level2Area相當於設備):
public class PreDefJobPlanDialogDataTable extends CygnusAbstractTableModel{
private static final long serialVersionUID = 1344977933386754731L;
static final ColumnData columns[] = {
new ColumnData("Sel." , 25, 10000, JLabel.LEFT),
new ColumnData("Type", 25, 10000, JLabel.LEFT),
new ColumnData("Status", 25, 10000,JLabel.LEFT),
new ColumnData("Title", 100, 10000,JLabel.LEFT),
new ColumnData("Equipment Class", 50, 10000, JLabel.LEFT),
new ColumnData("Equipment", 100, 10000, JLabel.LEFT)
};
...
public boolean isCellEditable(int rowIndex, int columnIndex) {
boolean ret = false;
if(columnIndex == 0 || columnIndex == 5) {
ret = true;
}
return ret;
}
...
public Object getValueAt(int rowIndex, int columnIndex) {
Object ret = " ";
if(rowIndex >= 0 && rowIndex < getRowCount()) {
PreDefJobPlan obj = (PreDefJobPlan)modelData.get(rowIndex);
switch(columnIndex) {
case 0: ret = obj.getGroupFlag();
break;
case 1: ret = obj.getType();
break;
case 2: ret = obj.getStatus();
break;
case 3: ret = obj.getTitle();
break;
case 4: ret = obj.getLevel1Area();
break;
case 5: ret = obj.getLevel2Area();
break;
case 9: ret = obj.getPreDefJobPlanID();
break;
default: ret = " ";
}
}
return ret;
}
...
public void setValueAt(Object obj, int row, int col) {
// get the object
PreDefJobPlan o = (PreDefJobPlan) this.modelData.get(row);
switch(col) {
case 0:
o.setGroupFlag((Boolean) obj);
break;
case 5:
o.setLevel2Area((Equipment) obj);
break;
default: ;
}
}
創建表:
protected void createTable() {
PreDefJobPlanDialogDataTable dataModel = new PreDefJobPlanDialogDataTable();
dataModel.setTimeOffset(0);
this.view.getOrdersTable().setHighlighters(HighlighterFactory.createAlternateStriping());
this.view.getOrdersTable().setAutoCreateColumnsFromModel(false);
this.view.getOrdersTable().setModel(dataModel);
this.view.getOrdersTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.view.getOrdersTable().setTransferHandler(new TableRowTransferHandler(this.view.getOrdersTable()));
for(int i = 0; i < dataModel.getColumnCount(); i++) {
if(i== 0) {
...
}
else if (i == 5){
TableCellRenderer renderer = new ComboCellRenderer();
ca.cygnusconsulting.utilities.ColumnData col = dataModel.getColumn(i);
JComboBox tableCheckBox = new JComboBox();
tableCheckBox.setModel(lvl1Model);
tableCheckBox.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
tableCheckBox.setBackground(this.view.getOrdersTable().getBackground());
TableCellEditor editor = new ComboBoxCellEditor(tableCheckBox);
TableColumnExt column = new TableColumnExt(i, col.getWidth(), renderer, editor);
column.setMaxWidth(col.getMaxWidth());
this.view.getOrdersTable().addColumn(column);
}
else{
...
}
}
渲染:
public class ComboCellRenderer extends JComboBox implements TableCellRenderer {
protected static Border noFocusBorder = new EmptyBorder(1,1,1,1);
protected static Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
public ComboCellRenderer() {
super();
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
setBackground(isSelected && !hasFocus ? table.getSelectionBackground() : table.getBackground());
setForeground(isSelected && !hasFocus ? table.getSelectionForeground() : table.getForeground());
setFont(table.getFont());
setSelectedItem(value);
return this;
}
}
有沒有人有任何合理的建議,或對此有任何見解?
[此問題]可能的重複(http://stackoverflow.com/questions/19977908/jcombobox-as-jtable-celleditor-with-overriden-stopcellediting-modifies-wrong-tab)。 – 2015-02-24 21:57:53
This ['ComboBoxCellEditor'](http://download.java.net/javadesktop/swinglabs/releases/0.8/docs/api/org/jdesktop/swingx/autocomplete/package-summary.html)? – trashgod 2015-02-25 10:31:01
值未被切換。組合框會記住他們的選擇。該問題與組合框直觀顯示選擇的內容有關。所以不,我不相信這是一個重複的問題。至於垃圾,我不確定你想說什麼。 – ballBreaker 2015-02-25 14:21:00