Reputation: 3
I am working on a very simple mind mapping application, and I use a JPanel to draw the structure of the mind map on. The nodes of the mind map are circles, and if you write in a node, the structure expands in that direction. I put my JPanel in a JScrollPane, but my problem is, that I can only expand it downwards, or to the right, but it should be expandable to the left and upwards as well. When my JScrollPane should expand in "negative directions" so upwards or to the left, I tried calculating how many coordinates I shift, and than redrawing the whole graphic, changing the x and y coordinates of everything respectively. But it still did not work, it drew the part that is visible correctly, but then when I scrolled down, the rest of my drawing disappeared.
I hope there is a solution for this, because I've searched for hours and found nothing useful, and because I can't think of any other way to solve this. Thank you for your help! :)
Upvotes: 0
Views: 228
Reputation: 324197
When my JScrollPane should expand in "negative directions" so upwards or to the left
You can't have negative coordinates, so you need to translate all the indexes to positive values.
and if not, I resize the JPanel.
This is the key. You need to override the getPreferredSize(...)
method of your custom panel to return the appropriate size of the panel after all nodes have been translated to positive values.
Then you invoke:
revalidate();
repaint();
on the panel to make sure the layout manager is invoked and the nodes are repainted.
Upvotes: 1