JTable单元重叠

时间:2016-03-24 03:35:20

标签: java swing jtable tablecellrenderer tablecelleditor

我已经实现了一个JTree并填充了一些数据。该表包含三列,并根据特定单元格上的某些值,它应具有标签或组合框。第3列中的所有值都是可编辑的。我也有一个JTree,从中选择一个节点,并且基于它,表值会相应地改变。当我编辑表格中的单元格并移动到树中的另一个节点(填充表格中的一组新数据)时,存在问题,先前编辑的单元格值存在于新单元格值顶部的表格中。下面是我实现TableCellRenderer和TableCellEditor的方法。我可能已经使用了一些错误的概念,因为我是摇摆的初学者。请帮我弄清楚我做错了什么。

        public void populateTableData(List<Field> list,JTree jTree){
        fieldList = null;
        tcBuilderTree = jTree;
        fieldList = list;
        md=new PropertiesTableModel(fieldList);
        getPropertieseTable().setModel(md);

        final TableCellRenderer cellRenderer = new TableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable arg0, 
                                                            Object value, 
                                                            boolean isSelected, 
                                                            boolean hasFocus, 
                                                            int row, 
                                                            int col) {

                if(value instanceof List<?>) {
                    List<Value> valueList=(ArrayList)value;
                    return createComboBox(valueList);
                }
                else{
                    JLabel lbl=new JLabel();
                    lbl.setText((String)value);
                    return lbl;
                }

            }
        };
        propertiesTable.setDefaultRenderer(Object.class, cellRenderer);

        final TableCellEditor cellEditor = new TableCellEditor() {
            private DefaultCellEditor textEditor;
            private DefaultCellEditor currentEditor;
            @Override
            public Component getTableCellEditorComponent(JTable table,
                                                        Object value, 
                                                        boolean isSelected, 
                                                        int row, 
                                                        int column) {
                textEditor = new DefaultCellEditor(new JTextField());
                PropertiesTableModel model = (PropertiesTableModel) table.getModel();
                List<Value> values = model.getPossibleValues(row, column);
                if (values != null) {
                    List<Value> valueList=(ArrayList)value;
                    currentEditor = new DefaultCellEditor(createComboBox(valueList));
                } else {
                    currentEditor = textEditor;
                }
                return currentEditor.getTableCellEditorComponent(table, value,
                        isSelected, row, column);
            }

            @Override
            public Object getCellEditorValue() {
                return currentEditor.getCellEditorValue();
            }

            @Override
            public boolean isCellEditable(EventObject anEvent) {
                JTable tbl = (JTable) anEvent.getSource();
                int row, col;
                if (anEvent instanceof MouseEvent) {
                    MouseEvent evt = (MouseEvent) anEvent;
                    row = tbl.rowAtPoint(evt.getPoint());
                    col = tbl.columnAtPoint(evt.getPoint());
                } else {
                    row = tbl.getSelectedRow();
                    col = tbl.getSelectedColumn();
                }
                if(col<2){
                    return false;
                }
                else
                {
                    return true;
                }
            }

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

            @Override
            public boolean stopCellEditing() {
                Object obj = currentEditor.getCellEditorValue();
                fieldList.get(propertiesTable.getEditingRow())
                                                .setDefaultValue(obj);
                return currentEditor.stopCellEditing();
            }

            @Override
            public void  cancelCellEditing() {
                currentEditor.cancelCellEditing();
            }

            @Override
            public void addCellEditorListener(CellEditorListener l) {
            }

            @Override
            public void removeCellEditorListener(CellEditorListener l) {

            }
        };
        propertiesTable.setDefaultEditor(Object.class,cellEditor);
    }

enter image description here

1 个答案:

答案 0 :(得分:3)

  

当我编辑表格中的单元格并移动到树中的另一个节点(填充表格中的一组新数据)时,存在问题,先前编辑的单元格值存在于新单元格顶部的表格中值。

我猜你在用新数据重新加载表之前不会停止编辑表格单元格。

创建JTable时,您可能应该在代码中添加以下内容:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

有关详细信息,请参阅Table Stop Editing

  

该表包含三列,并根据特定单元格上的某些值,它应该有标签或组合框

您可能不需要创建自定义编辑器。只需让表根据正在编辑的单元格选择适当的默认编辑器。您可以通过覆盖JTable的getCellEditor(...)方法来完成此操作。请查看:How to add unique JComboBoxes to a column in a JTable (Java)以获取此方法的示例。