JTable指定单元格加控件

JTable可以给表格的某一列加入控件,下面方法可以实现
 
try{
   TableColumn column = jTable.getColumn(colName);
   column.setCellEditor(new DefaultCellEditor(new JComboBox()));
 }catch(IllegalArgumentException ex){
 }
 或者
TableColumn column = JTable.getColumnModel().getColumn(int column);
column.setCellEditor(new DefaultCellEditor(new JComboBox()));
 
 
 
下面这个类可以帮助我们在表格的某一个指定的单元格实现下拉列表效果
 
JTable指定单元格加控件
JTable指定单元格加控件import java.awt.Component;
JTable指定单元格加控件import java.awt.event.ActionEvent;
JTable指定单元格加控件import java.awt.event.ActionListener;
JTable指定单元格加控件import java.util.EventObject;
JTable指定单元格加控件
JTable指定单元格加控件import javax.swing.JComboBox;
JTable指定单元格加控件import javax.swing.JTable;
JTable指定单元格加控件import javax.swing.event.CellEditorListener;
JTable指定单元格加控件import javax.swing.event.ChangeEvent;
JTable指定单元格加控件import javax.swing.event.EventListenerList;
JTable指定单元格加控件import javax.swing.table.TableCellEditor;
JTable指定单元格加控件
JTable指定单元格加控件public class ComboBoxCellEditor extends JComboBox implements TableCellEditor{
JTable指定单元格加控件
JTable指定单元格加控件    protected EventListenerList listenerList = new EventListenerList();
JTable指定单元格加控件    protected ChangeEvent changeEvent = new ChangeEvent(this);
JTable指定单元格加控件    
JTable指定单元格加控件    public ComboBoxCellEditor() {
JTable指定单元格加控件        super();
JTable指定单元格加控件        
JTable指定单元格加控件        addItem("日");
JTable指定单元格加控件        addItem("月");
JTable指定单元格加控件        addActionListener(new ActionListener() {
JTable指定单元格加控件            public void actionPerformed(ActionEvent event) {
JTable指定单元格加控件                fireEditingStopped();
JTable指定单元格加控件            }
JTable指定单元格加控件        });
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public void addCellEditorListener(CellEditorListener listener) {
JTable指定单元格加控件        listenerList.add(CellEditorListener.class, listener);
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public void removeCellEditorListener(CellEditorListener listener) {
JTable指定单元格加控件        listenerList.remove(CellEditorListener.class, listener);
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    protected void fireEditingStopped() {
JTable指定单元格加控件        CellEditorListener listener;
JTable指定单元格加控件        Object[] listeners = listenerList.getListenerList();
JTable指定单元格加控件        for (int i = 0; i < listeners.length; i++) {
JTable指定单元格加控件            if (listeners[i] == CellEditorListener.class) {
JTable指定单元格加控件                listener = (CellEditorListener) listeners[i + 1];
JTable指定单元格加控件                listener.editingStopped(changeEvent);
JTable指定单元格加控件            }
JTable指定单元格加控件        }
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    protected void fireEditingCanceled() {
JTable指定单元格加控件        CellEditorListener listener;
JTable指定单元格加控件        Object[] listeners = listenerList.getListenerList();
JTable指定单元格加控件        for (int i = 0; i < listeners.length; i++) {
JTable指定单元格加控件            if (listeners[i] == CellEditorListener.class) {
JTable指定单元格加控件                listener = (CellEditorListener) listeners[i + 1];
JTable指定单元格加控件                listener.editingCanceled(changeEvent);
JTable指定单元格加控件            }
JTable指定单元格加控件        }
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public void cancelCellEditing() {
JTable指定单元格加控件        fireEditingCanceled();
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public boolean stopCellEditing() {
JTable指定单元格加控件        fireEditingStopped();
JTable指定单元格加控件        return true;
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public boolean isCellEditable(EventObject event) {
JTable指定单元格加控件        return true;
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public boolean shouldSelectCell(EventObject event) {
JTable指定单元格加控件        return true;
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public Object getCellEditorValue() {
JTable指定单元格加控件        return getSelectedItem();
JTable指定单元格加控件    }
JTable指定单元格加控件
JTable指定单元格加控件    public Component getTableCellEditorComponent(JTable table, Object value,
JTable指定单元格加控件            boolean isSelected, int row, int column) {
JTable指定单元格加控件        return this;
JTable指定单元格加控件    }
JTable指定单元格加控件}
JTable指定单元格加控件
 
 
 
客户端调用上述代码,可以实现在表格第一行第二列的单元格内加入下拉列表
 
JTable指定单元格加控件JTable table = new JTable(){                
JTable指定单元格加控件    @Override
JTable指定单元格加控件    public TableCellEditor getCellEditor(int row, int column) {
JTable指定单元格加控件        // TODO Auto-generated method stub
JTable指定单元格加控件        if(row ==1 && column == 2){
JTable指定单元格加控件            return new ComboBoxCellEditor();
JTable指定单元格加控件        }
JTable指定单元格加控件        return super.getCellEditor(row, column);
JTable指定单元格加控件    }
JTable指定单元格加控件};

JTable指定单元格加控件

 
上一篇:【转】GDI+中发生一般性错误的解决办法


下一篇:Atitit.java c++指针使用总结O7