• 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()));
     
     
     
    下面这个类可以帮助我们在表格的某一个指定的单元格实现下拉列表效果
     

    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.EventObject;

    import javax.swing.JComboBox;
    import javax.swing.JTable;
    import javax.swing.event.CellEditorListener;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.EventListenerList;
    import javax.swing.table.TableCellEditor;

    public class ComboBoxCellEditor extends JComboBox implements TableCellEditor{

        protected EventListenerList listenerList = new EventListenerList();
        protected ChangeEvent changeEvent = new ChangeEvent(this);
        
        public ComboBoxCellEditor() {
            super();
            
            addItem("日");
            addItem("月");
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    fireEditingStopped();
                }
            });
        }

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

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

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

        public void cancelCellEditing() {
            fireEditingCanceled();
        }

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

        public boolean isCellEditable(EventObject event) {
            return true;
        }

        public boolean shouldSelectCell(EventObject event) {
            return true;
        }

        public Object getCellEditorValue() {
            return getSelectedItem();
        }

        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            return this;
        }
    }
     
     
     
    客户端调用上述代码,可以实现在表格第一行第二列的单元格内加入下拉列表
     
    JTable table = new JTable(){                
        @Override
        public TableCellEditor getCellEditor(int row, int column) {
            // TODO Auto-generated method stub
            if(row ==1 && column == 2){
                return new ComboBoxCellEditor();
            }
            return super.getCellEditor(row, column);
        }
    };
     
  • 相关阅读:
    sass安装与使用
    前端兼容性问题
    springboot项目数据库错误记录
    C#实现微信聊天对话框
    关于html的多行匹配,正则re.S的使用(爬取豆瓣电影短评)
    判断网页能否打开?python
    利用python爬取点小图片,满足私欲(爬虫)
    ajax加载时的进度条
    java 发送微信客服消息
    Fastjson Mysql JDBC 反序列化
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/5049042.html
Copyright © 2020-2023  润新知