2

我通过在 java 中扩展默认编辑器为我的表创建了一个自定义编辑器。代码看起来像

import java.awt.Component;
import java.text.ChoiceFormat;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;

import com.ushustech.nmsazzist.model.mib.MibVariableModel;


public class MibFormattedValueEditor extends DefaultCellEditor
{

private JComboBox m_comboBox;
public MibFormattedValueEditor()
{
    this(new JComboBox());
}

public MibFormattedValueEditor(JComboBox comboBox)
{
    super(comboBox);
    this.m_comboBox = comboBox;
}

@Override
public Object getCellEditorValue()
{
    return this.m_comboBox.getSelectedItem();
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
    this.m_comboBox.removeAllItems();
    MibVariableModel model = (MibVariableModel) table.getModel();
    ChoiceFormat format = model.getMibVariable(row).getFormat();

    if(null != format){
        Object[] obj = format.getFormats();
        for(int i=0;i<obj.length;i++){
            this.m_comboBox.addItem(obj[i].toString());
        }

    }

    return super.getTableCellEditorComponent(table, value, isSelected, row, column);

}

}  

如果格式为 null ,我想显示一个文本字段编辑器吗?请帮我这样做?谢谢。

4

2 回答 2

1

我不会更换,ComboBox因为这会变脏。我宁愿将其设置为可编辑,以防万一format == null让用户在此处输入信息。像这样:

if(null != format) {
   // ...
} else {
   this.m_comboBox.setEditable(true);
}
于 2012-11-14T10:00:05.797 回答
-1

默认情况下使 textField 不可见,并且当格式为 null 时将可见设置为 true:

textField.setVisible(true);
于 2012-11-14T09:55:38.003 回答