java.lang.IllegalArgumentException:列必须有效,was-1

时间:2014-10-29 07:22:50

标签: java swing swingx jxtreetable

我想为JXTreeTable添加一组值,但在运行程序时遇到这组错误。

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: column must be valid, was-1
    at org.jdesktop.swingx.JXTreeTable.isHierarchical(JXTreeTable.java:1760)
    at org.jdesktop.swingx.JXTreeTable.getToolTipText(JXTreeTable.java:1197)
    at javax.swing.ToolTipManager$insideTimerAction.actionPerformed(ToolTipManager.java:629)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

请帮我解决这个错误。

这是我的代码:

    final Date doj = Calendar.getInstance().getTime();        
    List<Department> departmentList = new ArrayList<Department>();

    List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee(1, "Ram", doj, "emp1.jpg"));
    empList.add(new Employee(2, "Krishna", doj, "emp2.jpg"));
    departmentList.add(new Department(1, "Sales", empList));

    List<Employee> empList2 = new ArrayList<Employee>();
    empList2.add(new Employee(3, "Govind", doj, "emp1.jpg"));
    empList2.add(new Employee(4, "Kiran", doj, "emp2.jpg"));
    empList2.add(new Employee(5, "Prabhu", doj, "emp1.jpg"));
    departmentList.add(new Department(2, "Marketing", empList2));

    //we use a no root model
    NoRootTreeTableModel noRootTreeTableModel = new NoRootTreeTableModel(departmentList);
    treeTable = new JXTreeTable(noRootTreeTableModel);
    treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    treeTable.setRootVisible(false);
   // treeTable.getColumnModel().getColumn(3).setCellRenderer(new PhotoRenderer());
    treeTable.setRowHeight(50);

    add(new JScrollPane(treeTable));

    setTitle("JXTreeTable Example");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);

class NoRootTreeTableModel extends AbstractTreeTableModel{
 private final static String[] COLUMN_NAMES = {"Id", "Name", "Doj", "Photo"};

    private List<Department> departmentList;

    public NoRootTreeTableModel(List<Department> departmentList) {
        super(new Object());
        this.departmentList = departmentList;
    }


    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }


    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }


    public boolean isCellEditable(Object node, int column) {
        if (node instanceof Department && column == 2) {
            return false;
        }
        return true;
    }


    public boolean isLeaf(Object node) {
        return node instanceof Employee;
    }


    public int getChildCount(Object parent) {
        if (parent instanceof Department) {
            Department dept = (Department) parent;
            return dept.getEmployeeList().size();
        }
        return departmentList.size();
    }


    public Object getChild(Object parent, int index) {
        if (parent instanceof Department) {
            Department dept = (Department) parent;
            return dept.getEmployeeList().get(index);
        }
        return departmentList.get(index);
    }


    public int getIndexOfChild(Object parent, Object child) {
        Department dept = (Department) parent;
        Employee emp = (Employee) child;
        return dept.getEmployeeList().indexOf(emp);
    }


    public Object getValueAt(Object node, int column) {
        if (node instanceof Department) {
            Department dept = (Department) node;
            switch (column) {
                case 0:
                    return dept.getId();
                case 1:
                    return dept.getName();
            }
        } else if (node instanceof Employee) {
            Employee emp = (Employee) node;
            switch (column) {
                case 0:
                    return emp.getId();
                case 1:
                    return emp.getName();
                case 2:
                    return emp.getDoj();
                case 3:
                    return emp.getPhoto();
            }
        }
        return null;
    }


    public void setValueAt(Object value, Object node, int column) {
        String strValue = (String) value;
        if (node instanceof Department) {
            Department dept = (Department) node;
            switch (column) {
                case 0:
                    dept.setId(Integer.valueOf(strValue));
                    break;
                case 1:
                    dept.setName(strValue);
                    break;
            }
        } else if (node instanceof Employee) {
            Employee emp = (Employee) node;
            switch (column) {
                case 0:
                    emp.setId(Integer.valueOf(strValue));
                    break;
                case 1:
                    emp.setName(strValue);
                    break;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

columnAtPoint返回-1,因为它不在JXTreeTable的列上 因此,在向column == -1添加值的同时添加JXTreeTable检查,并在条件发生时跳过添加值。