I have a huge program that uses a number of ScrollPanes with no problems at all. However, I decided that I wanted to replace the list of buttons in one ScrollPane with a big image. I implemented the changes and ran it. It failed in a most odd manner: the image is correctly drawn, but the vertical scroll bar is deactivated, so I can only see the upper portion of the image. I puttered around trying to find the problem and got nowhere. I re-read the Oracle pages on using ScrollPanes, checked that my code was essentially the same as theirs, and was still getting nowhere. So I did a web search on the problem. Everybody says the same thing, everybody does it the same way. That's the way I do it (I think), but that ScrollBar won't activate.
So I thought I should come here. I set up a new project and copied the code from my monster program into the tiny demo program. This time it doesn't draw anything! The JFrame is drawn, but there's no image and no ScrollBar. I used the debugger to verify that the image had been correctly read by the program. I went through at least 20 variations to see if I'd left something out or done something wrong. No joy.
So here is the complete code listing for the demo program that doesn't draw anything other than the window. Can you play "Catch the stupid mistake?"
Kemal, thank you; that solved the problem. I'm surprised that using BoxLayout was part of the solution; the Oracle documents state that you have to use a null Layout if you want to explicitly position JComponents by hand, which is what I need to do in this case.
Oops -- I found an ancillary problem with the solution previously provided. The basic idea I'm driving at here is to have a large background image over which I can place a group of JButtons. The previously offered solution accomplishes that, but it ALSO introduces a new problem: if I scroll the background image, the foreground JButtons all disappear. I've spent the last few hours trying to figure my way out of it. I tried to use the simple code presented on this site at "Background Image on JPanel", but that didn't solve the problem. I thought it had something to do with redrawing the elements of the JPanel in the wrong order (I want the background image drawn first, then all the JButton images on top of it) but I've had no luck fiddling with order. I didn't try writing a custom paint() method because the superclass would just substitute its own ordering.
Perhaps I need to break into the Event handler for the vertical scroll bar and refresh the JPanel whenever it activates, but I fear that would result in clunky scrolling. Is this the best strategy to pursue?
Interestingly, if the entire JFrame is redrawn (by, say, covering it with another JFrame then bringing it to the front again), the missing JButtons are redrawn -- but they disappear again when the JPanel is scrolled.
Joined: Jun 09, 2003
why do you need the buttons inside the scrollpane?
would be much easier to place them underneath, then they won't move.
if you 'need' the scrollbar to be alongside the buttons, that's also easy to do
Michael, I suppose that I have to step back and explain my goal here. Here's a rough mockup of the image I want to display: . Ignore the curvy arrows. This display shows the causal connections in a complex system. It is meant to be dynamic in two ways: first, the rectgrid arrows will vary in width, and second, each of the variables presented in the rounded rects needs to function as a button: you can click on it and it will jump to a new display. I had intended to superimpose the JButtons onto the roundrects; perhaps this is the wrong approach. Perhaps it would be better to attach a mousebutton handler to the entire JPanel and then do my own determination of which roundrect is clicked on. I'll be exploring that latter strategy today. Any suggestions?
I've been going round and round for the last hour and a half, getting myself more and more confused. First, here's code that draws the image properly and scrolls properly:
I need to be able to draw into the JFrame. In fact, I really don't need the image "testImage" because my ultimate goal is to draw it directly. But if I attempt to override the paint() method, I still run into the same problem: the drawn image is erased as soon as I scroll. Here's the code that does that:
My thinking just now is that perhaps I should implement my own ScrollPane using a regular Frame plus a ScrollBar.
One other question: I have always used Swing because everyone seems to thing that Swing is greatly superior to AWT. However, I get the feeling that Swing is only useful if you're going to do tricky layouts that must gracefully handle window resizing. I don't need to be able to handle window resizing. Therefore, would I be better off staying with AWT because it's simpler?
I think I've found the solution to my problem. I replace testImage with a modified JPanel that has its own paint method. I had failed using this previously, but this time I had the bright idea to use setPreferredSize for the modified JPanel, setting the size to that of the desired end image. Since that's the only Container inside the JPanel, it fills the entire space. So the whole thing works.
What'll you bet I come back within 24 hours whining that it didn't work out right? ;-)