Post by chriscrawford on Feb 17, 2016 14:16:52 GMT -8
I'm STILL stuck on the damn scrollbars for the HistoryBook display. The problem is simple: I need to mix two incompatible elements: fixed drawing and Swing. So I decide to revert to AWT, the original drawing system for Java. After several hours of work, in which I face inexplicable problems, I finally find this notation in the Java documentation:
Note: We recommend using a Scrollbar for value selection only. If you want to implement a scrollable component inside a container, we recommend you use a ScrollPane. If you use a Scrollbar for this purpose, you are likely to encounter issues with painting, key handling, sizing and positioning.
"Issues". Right. I definitely had issues. OK, so now I'm going to have to go back to the damn ScrollPane, which I have tried twice and never got working. But this time I'll be using an offscreen buffer, which might make it work this time.
OK, so I go back to the ScrollPane, and after some confusion end up reverting to the JScrollPane. Of course, it refuses to work -- just like both previous times. Perhaps I should try the AWT version (ScrollPane instead of JScrollPane). Hallelujah, something actually appears in the window (my little test drawing)! Now all I have to do is get the right content into it.
For some reason, the damn scroll pane insists on a default size of 100 x 100. This is obviously some default value. But I have setPreferredSize to much larger values. What gives? I add a Frame.pack() instruction because everybody else does that. I've never used it before. Sure enough, when I add it, the Frame simply disappears. Great.
As an experiment, I set the background color of the ScrollPane to red. Sure enough, it appears, red -- but it's still only 100 x 100.. Perhaps I need to be more emphatic. I add an explicit SetSize instruction. That does the trick: now the thing is the right size. So politely asking for a preferred size is a waste of time when there's only one component inside a ScrollPane. Lesson learned. No more Mr. Nice Guy. In the future, I don't ask, I bark.
But this STILL doesn't solve the problem. The thing displays the first sentence, and refuses to display any further sentences. "I'll work just a little bit for you, and then I'll fold my arms and refuse. Just because I'm so finicky." Right. I hate Java.
I found the mistake: I was asking the Frame to repaint when I should have asked the ScrollPane to repaint. I assumed that telling the Frame to repaint would cause it to issue orders to all its components to repaint. But no, it doesn't work that way.
So now I have it drawing properly but the scroll bars aren't appearing. I fiddle around for a few more hours and get the vertical scroll bar appearing, but now it refuses to alter in response to changes in the contents. So I take a deep breath and convert it to a JScrollPane. Poof! Now it refuses to display anything.
OK, back to AWT ScrollPanes. These are failing because I cannot get the scrollbar to adjust its range in response to the lengthening display of sentences. ScrollPanes do not permit access to their scrollbars. The only tool I have to work with is the height of the Panel that goes into the ScrollPane. Just to make sure, though, I tried lengthening the ScrollPane, lengthening the Panel, and lengthening both. None of these approaches accomplished anything.
I make a little more progress by setting up the Panel to be very long at the outset. Since I cannot alter it size in real time, I have to set it up so that it's huge right at the beginning. It strikes me as really stupid that I cannot adjust the size of the Panel after it has been created, but that's just my tough luck, I suppose.
And there's another problem: the damn thing draws only into the upper portion; it doesn't draw anything below that.
I'm giving up for now. I'm just beating my head against a wall. I'll go work in the forest and clear my mind.
Note: We recommend using a Scrollbar for value selection only. If you want to implement a scrollable component inside a container, we recommend you use a ScrollPane. If you use a Scrollbar for this purpose, you are likely to encounter issues with painting, key handling, sizing and positioning.
"Issues". Right. I definitely had issues. OK, so now I'm going to have to go back to the damn ScrollPane, which I have tried twice and never got working. But this time I'll be using an offscreen buffer, which might make it work this time.
OK, so I go back to the ScrollPane, and after some confusion end up reverting to the JScrollPane. Of course, it refuses to work -- just like both previous times. Perhaps I should try the AWT version (ScrollPane instead of JScrollPane). Hallelujah, something actually appears in the window (my little test drawing)! Now all I have to do is get the right content into it.
For some reason, the damn scroll pane insists on a default size of 100 x 100. This is obviously some default value. But I have setPreferredSize to much larger values. What gives? I add a Frame.pack() instruction because everybody else does that. I've never used it before. Sure enough, when I add it, the Frame simply disappears. Great.
As an experiment, I set the background color of the ScrollPane to red. Sure enough, it appears, red -- but it's still only 100 x 100.. Perhaps I need to be more emphatic. I add an explicit SetSize instruction. That does the trick: now the thing is the right size. So politely asking for a preferred size is a waste of time when there's only one component inside a ScrollPane. Lesson learned. No more Mr. Nice Guy. In the future, I don't ask, I bark.
But this STILL doesn't solve the problem. The thing displays the first sentence, and refuses to display any further sentences. "I'll work just a little bit for you, and then I'll fold my arms and refuse. Just because I'm so finicky." Right. I hate Java.
I found the mistake: I was asking the Frame to repaint when I should have asked the ScrollPane to repaint. I assumed that telling the Frame to repaint would cause it to issue orders to all its components to repaint. But no, it doesn't work that way.
So now I have it drawing properly but the scroll bars aren't appearing. I fiddle around for a few more hours and get the vertical scroll bar appearing, but now it refuses to alter in response to changes in the contents. So I take a deep breath and convert it to a JScrollPane. Poof! Now it refuses to display anything.
OK, back to AWT ScrollPanes. These are failing because I cannot get the scrollbar to adjust its range in response to the lengthening display of sentences. ScrollPanes do not permit access to their scrollbars. The only tool I have to work with is the height of the Panel that goes into the ScrollPane. Just to make sure, though, I tried lengthening the ScrollPane, lengthening the Panel, and lengthening both. None of these approaches accomplished anything.
I make a little more progress by setting up the Panel to be very long at the outset. Since I cannot alter it size in real time, I have to set it up so that it's huge right at the beginning. It strikes me as really stupid that I cannot adjust the size of the Panel after it has been created, but that's just my tough luck, I suppose.
And there's another problem: the damn thing draws only into the upper portion; it doesn't draw anything below that.
I'm giving up for now. I'm just beating my head against a wall. I'll go work in the forest and clear my mind.