0

I'd like to calculate the height and set the resizeWeight of a JSplitPane that may contain other JSplitPanes and other Components as well. The concept right now looks the following (warning: snippet, not a fully functional code fragment; I only need guidelines for rewriting the recursive invocations with a while-loop using accumulators, not a fully functional solution code).

splitPane.setResizeWeight(calculateComponentHeight(splitPane.getTopComponent()) / calculateNewSplitPaneHeight(splitPane));
...
private double calculateNewSplitPaneHeight(JSplitPane sp) {
  return calculateComponentHeight(sp.getTopComponent()) + calculateComponentHeight(sp.getBottomComponent());
}

private double calculateComponentHeight(Component c) {
  if (c instanceof JSplitPane) {
    return calculateNewSplitPaneHeight((JSplitPane) c);
  }
  return (double) c.getHeight();
}

As I may not know the level of embeddedness of the split panes, what can be a practical approach towards resolving such recursions?

And yes, I know, splitPane.getPreferredSize().height and splitPane.getTopComponent().getPreferredSize().height give me the answers I need without any recursive invocations. This is question is merely here to work one's brain on recursions and loops.

András Hummer
  • 960
  • 1
  • 17
  • 35
  • Out of interest, why are you wanting to make this change? – Duncan Jones Nov 11 '13 at 16:04
  • Because I find converting recursions to loops fun. Yet I've found myself an interesting problem that looks like a double recursion and it keeps me thinking how such recursions can be resolved. I want to teach my brain to think in such way. – András Hummer Nov 11 '13 at 16:11

1 Answers1

1

You can emulate recursion with a stack: How can you emulate recursion with a stack?

I once made a try on emulating a simple Fibonacci-like recursion:

f(n) = f(n-1) + f(n-2) + array[n]

The result is placed on a GitHub and is written according to the explanations in the linked answer. I should admit that it was a more complex task than I first imagined.

Community
  • 1
  • 1
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
  • Given an embeddedness of split panes large enough, won't the stack overload? That's why I've been thinking of an approach with accumulators. – András Hummer Nov 12 '13 at 05:56
  • Stack here is emulated - it's an `ArrayList` which is stored at heap. I'm quite sure that you may leave the recursionas you practically won't ever reach stack bottom in your example, which might be ~1000 embedded panes with a default stack size. – Andrey Chaschev Nov 12 '13 at 08:53