java - 替换 JTable 的组合框编辑器

标签 java swing jtable

我通过扩展 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);

}

}  

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

最佳答案

我不会替换 ComboBox,因为它会变脏。我宁愿在 format == null 的情况下将其设置为可编辑,并让用户在此处输入信息。像这样:

if(null != format) {
   // ...
} else {
   this.m_comboBox.setEditable(true);
}

关于java - 替换 JTable 的组合框编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13376428/

相关文章:

java - 如何停止android中使用PostDelayed方法启动的线程

java - 对执行 Swing 应用程序的主类中的 SwingUtilities.invokeLater() 方法的调用到底是什么?

java - 学习关键事件

java - 在 Netbeans 平台中创建类似 Empathy 聊天窗口的主视图

java - 如何从 JTable 中删除一行,同时将其保留在数据库中

java - java中 'gss_import_name'和 'gss_init_sec_context'方法等效吗?

java - 错误插入元素(主键)

java - 凯撒密码 Java 程序无法移位超过 23

java - 编辑 JTable 后获取选定的行和列

java - 从 JTable 中删除行时出现问题