javaswingswingxjxtreetable

java.lang.IllegalArgumentException: column must be valid, was-1


I want to add a set of values to JXTreeTable but I am getting this set of error while running program.

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)

Please help me to solve this error.

This is my code:

.

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

Solution

  • columnAtPoint returns -1 because it isn't over a column of JXTreeTable So, add a check for column == -1 while adding values to JXTreeTable and skip adding value whenever the condition occurs.