import javax.swing.JComboBox;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import java.awt.Dimension;
import javax.swing.table.TableCellEditor;
import java.util.EventObject;
import javax.swing.event.CellEditorListener;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.event.EventListenerList;
import javax.swing.event.ChangeEvent;

/** *//**
*
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: wri</p>
*
* @author tang08
* @version 1.0
*/
public class ComboBoxCellEditor
extends JComboBox implements TableCellEditor ...{
protected EventListenerList listenerList = new EventListenerList();
protected ChangeEvent changeEvent = new ChangeEvent(this);
private DefaultComboBoxModel model = new DefaultComboBoxModel();

public ComboBoxCellEditor() ...{
super();
this.setPreferredSize(new Dimension(80, 25));
this.setModel(model);
}

/** *//**
* 设置初始值
* @param value Object
*/
public void setValue(Object value) ...{
if (model.getSize() > 0) ...{
model.removeAllElements();
}
if (value != null) ...{
if (value instanceof String) ...{
model.addElement(value);
return;
}
List list = (List) value;
for (int i = 0; i < list.size(); i++) ...{
model.addElement(list.get(i));
}
this.setSelectedIndex(0);
}
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row,
int column) ...{
if (value == null) ...{
String select = (String)this.getSelectedItem();
setSelectedItem(select);
return this;
}
setSelectedItem(value);
return this;
}

/** *//**
* 获得设置数据值
* @return Object
*/
public Object getValue() ...{
return this.getSelectedItem();
}

public void setEnable(boolean enable) ...{
super.setEnabled(enable);
}

public Object getCellEditorValue() ...{
return (String)this.getSelectedItem();
}

public boolean isCellEditable(EventObject anEvent) ...{
return true;
}

public boolean shouldSelectCell(EventObject anEvent) ...{
return true;
}

public boolean stopCellEditing() ...{
fireEditingStopped();
return true;
}

public void cancelCellEditing() ...{
fireEditingCanceled();
}

public void addCellEditorListener(CellEditorListener l) ...{
listenerList.add(CellEditorListener.class, l);
}

public void removeCellEditorListener(CellEditorListener l) ...{
listenerList.remove(CellEditorListener.class, l);
}

/** *//**
* fireEditingStopped
*/
protected void fireEditingStopped() ...{
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) ...{
if (listeners[i] == CellEditorListener.class) ...{
listener = (CellEditorListener) listeners[i + 1];
listener.editingStopped(changeEvent);
}
}
}

protected void fireEditingCanceled() ...{
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) ...{
if (listeners[i] == CellEditorListener.class) ...{
listener = (CellEditorListener) listeners[i + 1];
listener.editingCanceled(changeEvent);
}
}
}
}
本文介绍了一个自定义的 JComboBox 实现,该实现被用作 JTable 的单元格编辑器。通过继承 JComboBox 类并实现 TableCellEditor 接口,本文详细展示了如何创建一个可以为表格单元格提供下拉选择功能的组件。
757

被折叠的 条评论
为什么被折叠?



