2012-11-14 72 views
2

我通过在java中扩展默认编辑器为我的表创建了自定义编辑器。代码看起来像替换JTable的组合框编辑器

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); 

} 

} 

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

+0

是有真正的原因BUILT_IN支持或JComboBox的每一个(或多个)的改变可以已经得到不同的ComboBoxModel :-) – mKorbel

回答

1

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

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

我该怎么做?如果你不介意你能帮我吗? – Nikhil

+0

@Nikhil我已经添加了一个示例 – Kai

+0

谢谢你的帮助 – Nikhil

-1

使文本字段不可见默认情况下,当格式为空设置为true可见:

textField.setVisible(true); 
+0

这不回答这个问题。他希望在空的情况下用TestField代替ComboBox。 – Kai