Sort JTree Node To Always Stay First Or Last


Recently I wrote a post on how to sort JTree nodes alphabetically. But what if you want certain nodes that are equal to some words and you want them to always stay as the first and last node.

For example, I want to make the String “CARS” to always be the first node in the JTree and the String “HOUSE” to be the last while the rest of the tree nodes will be sorted alphabetically.

Using the value of the compare() method of the Comparator class, I did a dirty workaround to make this work with a little help from the Math class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public int compare(Object o1, Object o2) {
    int output = o1.toString().compareToIgnoreCase(o2.toString());
    if (o1.toString().equalsIgnoreCase("CARS")) {
        if (output > 0) {
            output = -Math.abs(output);
        }
    } else if (o2.toString().equalsIgnoreCase("CARS")) {
        if (output < 0) {
            output = Math.abs(output);
        }
    } else if (o1.toString().equalsIgnoreCase("HOUSE")) {
        if (output < 0) {
            output = Math.abs(output);
        }
    } else if (o2.toString().equalsIgnoreCase("HOUSE")) {
        if (output > 0) {
            output = -output;
        }
    }
}

Found this post useful? Buy me a cup of coffee or subscribe to my RSS feeds and Google Friend Connect

Sort JTree


Sorting a JTree‘s nodes is easy. Just make sure your nodes are of type DefaultMutableTreeNode. Have your class extend DefaultMutableTreeNode and add this code within the class. As you can see in the code, once a tree node is inserted into the tree, the nodes are sorted alphabetically using the Collections‘ sort method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Override
public void insert(MutableTreeNode newChild, int childIndex)    {
    super.insert(newChild, childIndex);
    Collections.sort(this.children, nodeComparator);
}
 
protected Comparator nodeComparator = new Comparator () {
    @Override
    public int compare(Object o1, Object o2) {
        return o1.toString().compareToIgnoreCase(o2.toString());
    }
 
    @Override
    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    public boolean equals(Object obj)    {
        return false;
    }
 
    @Override
    public int hashCode() {
        int hash = 7;
        return hash;
    }
};

Found this post useful? Buy me a cup of coffee or subscribe to my RSS feeds and Google Friend Connect

Sort JComboBox Items


To sort items in a JComboBox object, check the code below. What the code does is compare the object to be added with the rest of the items that were already added. The compareToIgnoreCase() returns either a negative value if the first object less than the second one, 0 for equal and a positive value if it is greater than.

If the value returned is less than or equal to 0, the item will be inserted at the specified index of the item list else, add the item at the end of the item list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
JComboBox combo = new JComboBox() {
    public void addItem(Object anObject) {
        int size = ((DefaultComboBoxModel) dataModel).getSize();
        Object obj;
        boolean added = false;
        for (int i=0; i<size; i++) {
            obj = dataModel.getElementAt(i);
            int compare = anObject.toString().compareToIgnoreCase(obj.toString());
            if (compare <= 0) { // if anObject less than or equal obj
                super.insertItemAt(anObject, i);
                added = true;
                break;
            }
        }
 
        if (!added) {
            super.addItem(anObject);
        }
    }
};

Found this post useful? Buy me a cup of coffee or subscribe to my RSS feeds and Google Friend Connect

Related Posts with Thumbnails