Hi,

Eg, i have a JTree loaded from a XML document which has alot of nodes. How do i just show 10 nodes at a time and show the next 10 nodes, etc later when i click on next button?

Basically i want my JTree to work like this:
When i click on Next button, it will get the next 10 nodes according to hierarchal order to display on the JTree
When i click on Previous button, it will get the previous 10 nodes according to hierarchal order to display on the JTree

if i just put a for loop:
for (int a = 0; a < 10; a++) it will give me error as my JTree has more than 10 nodes.

Anyone has any idea in solving this problem?

The XML document that i'm reading in is dynamic. There will always be ONE root and the children for each node is not fixed. This section of code provides how the JTree is created:

public void reconstruct() throws FileNotFoundException, SQLException, IOException {
int newBranchOrder;
_rValue = getRValue(_dbConnection);
_unsplitPaths = getPaths(_dbConnection);
_paths = new String[_unsplitPaths.length][];

_pathIsAttribute = new boolean[_unsplitPaths.length];

long maxMem = (Runtime.getRuntime().maxMemory() / 10) * 5; // set the max memory to use before throw out of memory exception

_results.last();
totalRow = _results.getRow();

// go to the first node to be constructed, either from row 0 or from a specify location currentRow
if (currentRow == 0) {
_results.first();
} else {
_results.absolute((int) currentRow);
}

try {
_pathID = _results.getInt(2);
} catch (SQLException e) {
writeLeaf("No results");
return;
}

// write out the paths from queryDepth to the first leaf node
int j;
for (j = _queryDepth; j < getPath(_pathID).length - 1; j++) {
startElement(getPath(_pathID)[j]);
//root
System.out.println("root: "+getPath(_pathID)[j]);
}

_oldDeweyOrderSum = _results.getLong(4);
_oldPathID = _pathID;

//added pam - leaf value
String startValue = _results.getString(1);

if (isPathAttribute(_pathID) == false) {
if (getPath(_pathID).length > j) //klarinda 20060804 - add if condition to make sure no array out of bound exception (if the 1st result is text w/o parent element)
{
startElement(getPath(_pathID)[j]);
//added pam
System.out.println("start element: "+getPath(_pathID)[j]);
}

if (startValue != null) {
writeLeaf(startValue.trim());
//added pam
System.out.println("leaf value of start element: "+startValue.trim());
} else {
writeLeaf(" ");
}

} else {
String attributeName = getPath(_pathID)[j];
attributeName = attributeName.substring(1, attributeName.length());
if (startValue != null) {
writeAttribute(attributeName + " = '" + startValue.trim() + "'");
} else {
writeAttribute(attributeName + " = ' '");
}
}
currentRow++;

//added pam - elements at next level, loop from row 1 to row 350
// loop through all nodes until we reach the last node
while (_results.next()) {
_pathID = _results.getInt(2);
//added pam
System.out.println("path ID: "+_pathID);
System.out.println("elements 2nd lvl: "+getPath(_pathID)[j]);
_deweyOrderSum = _results.getLong(4);

long deweyDiff = _deweyOrderSum - _oldDeweyOrderSum;

// find the nearest common ancestor using theorem 3
for (newBranchOrder = _results.getInt(3) - 2; newBranchOrder >= 0; newBranchOrder--) {
if (deweyDiff < _rValue[newBranchOrder]) {
break;
} //klarinda 20060817
}
newBranchOrder += 2;

if (newBranchOrder < _queryDepth - 1) {
newBranchOrder = _queryDepth - 1;
}

int e = (getPath(_oldPathID).length) - 1;
if (isPathAttribute(_oldPathID) == true) {
e--;
newBranchOrder = e; //klarinda 20061109 (because separate attribute table)
}

// close path till the common ancestor
for (; e > newBranchOrder; e--) {
endElement(getPath(_oldPathID)[e]);
}

//added pam - get all the leaf value
String value = _results.getString(1);
int s = newBranchOrder + 1;
//added pam
System.out.println("branch order: "+newBranchOrder);
// open path from common ancestor to second node
for (; s < getPath(_pathID).length - 1; s++) {
//startElement(getPath(_pathID)[s]);
startElement(getPath(_pathID)[s]);
//added pam - get common ancestor to second node
System.out.println("elements 3rd lvl: "+getPath(_pathID)[s]);
}

// write out the leaf value
if (isPathAttribute(_pathID) == false) {
if (s < getPath(_pathID).length) {
startElement(getPath(_pathID)[s]);
//leaf nodes? added pam
System.out.println("leaf nodes of 3rd lvl: "+getPath(_pathID)[s]);
}
if (value != null) {
writeLeaf(value.trim());
//added pam
System.out.println("leaf value of leaf nodes: "+value.trim());
} else {
writeLeaf(" ");
}
} else {
String attributeName = getPath(_pathID)[s];
attributeName = attributeName.substring(1, attributeName.length());
if (value != null) {
writeAttribute(attributeName + " = '" + value.trim() + "'");
} else {
writeAttribute(attributeName + " = ' '");
}
}

// replace first node with second node and move on to the next node
_oldDeweyOrderSum = _deweyOrderSum;
_oldPathID = _pathID;

currentRow++;
System.out.println("currentRow++ below: "+currentRow);


if (endRow == 0 && (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) > maxMem) {
overflow = true;
break;
} else if (endRow != 0 && currentRow == endRow) {
overflow = true;
break;
}

}

if (overflow) {
_results.next();
if (_results.getInt(3) > _queryDepth) {
nodeCount = nodeCount - _results.getInt(3) + _queryDepth - 1;
}
}

// last node, therefore close path till queryDepth
if (isPathAttribute(_oldPathID) == false) {
for (int i = getPath(_oldPathID).length - 1; i >= _queryDepth; i--) {
endElement(getPath(_oldPathID)[i]);
}
} else {
for (int i = getPath(_oldPathID).length - 2; i >= _queryDepth; i--) {
endElement(getPath(_oldPathID)[i]);
}
}

}

// declare a start element, the writing of start element is delay by 1
private void startElement(String element) throws IOException {
nodeCount++;
System.out.println("nodeCount: "+nodeCount);
writeStartElement();
_startElement = element;
}

// write the previous stored start element and its corresponding attribute if any
// writing in 3 different types of output depending on which output the user had choosen
private void writeStartElement() throws IOException {
String attribute;
if ((_startElement == null) || (_startElement.length() == 0)) {
return;
}
if (_output == FILE) {
fileOutputStream.write('<');
fileOutputStream.write(_startElement.getBytes());
for (int i = 0; i < _attribute.size(); i++) {
attribute = " " + (String) _attribute.elementAt(i);
fileOutputStream.write(attribute.getBytes());
}
fileOutputStream.write('>');
} else if (_output == TEXT) {

if (_startElement.length() > 0) {
stringResult.append("<" + _startElement);
for (int i = 0; i < _attribute.size(); i++) {
stringResult.append(" " + (String) _attribute.elementAt(i));
}

stringResult.append(">");
}
} else { // (output == TREE)

DefaultMutableTreeNode node = new DefaultMutableTreeNode(_startElement);
current.add(node);

for (int i = 0; i < _attribute.size(); i++) {
current = node;
String Attr = ((String) _attribute.elementAt(i)).trim();
String[] temp = Attr.split("=");
DefaultMutableTreeNode attrNode = new DefaultMutableTreeNode("@" + temp[0].trim());
current.add(attrNode);
current = attrNode;

attrNode = new DefaultMutableTreeNode(temp[1].trim());
current.add(attrNode);
}

current = node;
}
_attribute.clear();
_startElement = null;
}

// declare an end element and write out this element immediately
private void endElement(String element) throws IOException {
writeStartElement();
if (_output == FILE) {
fileOutputStream.write('<');
fileOutputStream.write('/');
fileOutputStream.write(element.getBytes());
fileOutputStream.write('>');
} else if (_output == TEXT) {
if (element.length() > 0) {
stringResult.append("</" + element + ">");
}
} else {
current = (DefaultMutableTreeNode) current.getParent();
}
}

// write the leaf value
private void writeLeaf(String leaf) throws IOException {

writeStartElement();
if (_output == FILE) {
fileOutputStream.write(leaf.getBytes());
} else if (_output == TEXT) {
stringResult.append(leaf);
} else { // (output == TREE)
DefaultMutableTreeNode node = new DefaultMutableTreeNode(leaf);
current.add(node);
}
}


Help very much appreciated.