JTable单元编辑器错误

时间:2011-03-10 15:25:42

标签: java swing jtable cell

我写了JTable的单元格编辑器。现在它有一个bug。我希望它在第一次加载时显示默认值。编辑后,它将显示用户输入。运行应用程序,您将了解该错误。

如何解决?

感谢。

package com.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class PropPageTest extends JPanel {

    private static final long serialVersionUID = 1555593265606867986L;

    public JTableX table;
    public DefaultTableModel model;
    private String[] col_names = { "Name", "Value" };
    private String[] anchor_values = { "CENTER", "NORTH" };
    public String[] default_value = {"John", "NORTH"};

    public boolean isFirstTimeLoad = true;

    private void createGUI() {
        setLayout(new BorderLayout());

        model = new DefaultTableModel(col_names, 2) {
            private static final long serialVersionUID = 478700329843848400L;
            public String[] prop_names = { "Name", "Anchor" };

            public Object getValueAt(int row, int col) {
                if (col == 0) return prop_names[row];
                if(isFirstTimeLoad){
                    isFirstTimeLoad = false;
                    return default_value[row];
                }
                else {
                    return super.getValueAt(row, col);
                }               
            }

            public boolean isCellEditable(int row, int col) {
                if (col == 0) return false;
                return true;
            }
        };

        table = new JTableX(model);

        JTextField txtFld = new JTextField();
        table.registerCellEditor(0, new DefaultCellEditor(txtFld));

        JComboBox cb = new JComboBox(anchor_values);        
        table.registerCellEditor(1, new DefaultCellEditor(cb));         

        add(table, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("test");
        f.setSize(300, 350);
        final PropPageTest test = new PropPageTest();
        f.getContentPane().add(test, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        JButton saveBtn = new JButton("Save");
        buttonPanel.add(saveBtn);
        f.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        saveBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                test.table.stopCellEditing();
                String[] newArr = new String[2];
                newArr[0] = test.model.getValueAt(0, 1)+"";
                newArr[1] = test.model.getValueAt(1, 1)+"";
                boolean isTrue = Arrays.equals(test.default_value, newArr);

                System.out.println("new value --" + test.model.getValueAt(0, 1));
                System.out.println("new value --" + test.model.getValueAt(1, 1));
                System.out.println("is true --" + isTrue);
            }
        });

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setLocationRelativeTo(null);
    }

    public PropPageTest() {
        createGUI();
    }
}

- 另一个文件

package com.swing;


import java.util.HashMap;
import java.util.Set;

import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

public class JTableX extends JTable {
    private static final long serialVersionUID = -7180009397735740833L;
    private final HashMap<Integer, TableCellEditor> rowEditorMap;

    public JTableX(TableModel tm) {
        super(tm);
        rowEditorMap = new HashMap<Integer, TableCellEditor>();
        this.setRowSelectionAllowed(false);
        this.setColumnSelectionAllowed(false);
    }

    public void registerCellEditor(int row, TableCellEditor editor){
        rowEditorMap.put(row, editor);
    }

    public TableCellEditor getCellEditor(int row, int col) {
        TableCellEditor tmpEditor = null;
        if (rowEditorMap != null) tmpEditor = rowEditorMap.get(row);

        if (tmpEditor != null) return tmpEditor;
        return super.getCellEditor(row, col);
    }

    public void stopCellEditing() {
        if (rowEditorMap != null) {
            Set<Integer> keySet = rowEditorMap.keySet();
            for (Integer i : keySet) {
                TableCellEditor tmpEditor = rowEditorMap.get(i);
                if (tmpEditor != null) {                    
                    tmpEditor.stopCellEditing();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:5)

你做不到。 JTable(或代码的任何其他部分)可以在需要时调用getValueAt。该方法应该返回表实际包含的内容。如果最初需要表中的默认值,则将这些默认值存储在模型中。

当模型中的数据发生变化时,它必须触发TableModelEvent。当调用setValueAt时,DefaultTableModel会自动完成此操作。