First off, sorry for all these posts about JPanels, JScrollPanes, and resizing. I have been making different posts because the issues in each are not exactly directly related and I didn't want to take each post in a whole new direction a bunch of times.
Anywho, I have a rather interesting issue. So I made a small test program to play around with a functionality of setting JPanel's preferred size based on the painting inside of it. That JPanel was inside of a JScrollPane, which was the ContentPane for the JFrame on the test program. The test program worked as expected, by adding Scroll Bars on the JScrollPane when necessary.
But, when I implemented the same code in my large program (with the same input and all), it was not successful by any means. The Scroll Bars were not added and I am once again very frustrated. The key difference between the test program and the full sized program is that in the full sized program the JScrollPane is not the JFrame's ContentPane (as there are a bunch more elements in the JFrame). It would seem the JPanel does not want to reset its size when its JScrollPane is not the ContentPane. I have attempted revalidating in every location I could think of. I have attempted setting the preferred size of the JPanel is every location I could think of. And, based on advice I found on another messageboard, I attempted to override the JPanel's getPreferredSize() method to set it based on the values I want it to be. All failed to work and I am beginning to struggle with my sanity.
Does anyone have any advice for me that will actually work?
I can't really post any code since there is no way for me to narrow my problem down any smaller than my 200 line test program, where the issue does not occur. Plus, I cannot provide the image and text files the test program and the full program use so the context of the code I would provide wouldn't even be replicable.
EDIT: Ok, a much more interesting piece of the puzzle has been added and now I really am confused.
So my code create an Object that extends JPanel called TutorialPanel. TutorialPanel has a method called loadInput(int) that decides what will be painted to the JPanel, then calls the overwritten paintComponent() method. While painting, it determines what the last Y value was. I then overrode the getPreferredSize() method like this:
Now, the user decides what they want painted to the JPanel by using a JTree. I made a half-assed Change Listener for the JTree as I have not extensively researched how to find out what element has been selected. That Change Listener looks like this:
tree.addTreeSelectionListener(new TreeSelectionListener(){ public void valueChanged(TreeSelectionEvent e) { TreePath path = e.getPath(); System.out.println(path.toString()); if(path.toString().equalsIgnoreCase("[Features, Event Table, Category Overview]")) { outputPane.loadInput(0); outputPane.revalidate(); } } });
Now, what is interesting here is that when the user selected the path above, the JPanel paints, but not revalidates. BUT, if the user selects a different path (currently no implementation for other paths) and then reselects the path above, suddenly the TutorialPane revalidates and I have Scroll Bars.
Why doesn't it work the first time but it does the second? Has anything changed?