aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Scroll around main JFrame? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Scroll around main JFrame?" Watch "Scroll around main JFrame?" New topic
Author

Scroll around main JFrame?

Ramses Tutoli
Greenhorn

Joined: Sep 05, 2003
Posts: 26
Is there a way to do this? Right now if my program is resized to be a bit smaller, I lose view of some of the components and have no way of accessing them. For example, all the buttons may disappear and only the JTable is visible. I'd like to compensate for this by having a scroll appear if the window is too small to fit all the components.
Alternatively, is there a way to get a listener to repaint the whole gui if someone changes the size of the window? I looked at WindowListener, but I don't see how any of those functions would do what I want.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Right now if my program is resized to be a bit smaller, I lose view of some of the components and have no way of accessing them. For example, all the buttons may disappear and only the JTable is visible.
This indicates to me that you are not using the right LayoutManager, or you are not using LayoutManager at all.
I'd like to compensate for this by having a scroll appear if the window is too small to fit all the components.
This is not the way to handle it. It's OK to to have a scroll for the components within the frame or a dialog, but never on the frame itself. Take a look how well designed Java apps behave, -- when you resize the frame, it should not obscure any components. Here is an example from JBuilder.
Minimum size of the frame:

Normal size of the frame:

No matter how you resize it, all controlls will be visible at all times, and no scrollbar will apear at any time on the frame.
Ramses Tutoli
Greenhorn

Joined: Sep 05, 2003
Posts: 26
I think I am using a layout manager. Doesn't a JFrame have a BorderLayout by default? Anyways, I put everything into the frame using the following code:

So I must be using the BorderLayout or the code wouldn't have compiled right? Maybe the problem is that I should disallow the option of resizing the window to make it smaller. Is that possible? Or how would i set the minimum size of the frame like you showed in your example? I can't find find that in the API.
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
Have you looked at JScrollPane ?
Tony
[ September 10, 2003: Message edited by: Tony Collins ]
Ramses Tutoli
Greenhorn

Joined: Sep 05, 2003
Posts: 26
Tony,
No, JScrollPane doesn't work. But Eugene is right that I shouldn't even consider it if it did because of the points he made.
I did find ComponentListener though which I can use to resize my window to the preferred size in the event that the user tries to make it smaller. However, that only seems to mask the problem. It works in 1024x768 mode, but when I change to 640x480, I get the same problem as I originally described. Somehow, I need my JTable and other components to resize automatically if the frame is too small.
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
I think you're JTable should be in a JScrollPane though.
Tony
Ramses Tutoli
Greenhorn

Joined: Sep 05, 2003
Posts: 26
my JTable IS wrapped in a JScrollPane (which is then wrapped in a JPanel because I want to put a nice-looking border around it). That's not the problem. The problem is when the JFrame is resized smaller, and the table doesn't resize accordingly or buttons become obscured.
I'm using a BorderLayout. The Table pane is added to the North, "search" pane added to the Center, and the "book" button added to the South. If I resize it smaller vertically, the search pane becomes obscured. The hack I used was to make the search pane itself a JScrollPane so that at least you can scroll down to see the components.
Will switching to another LayoutManager do any good?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
[b]If I resize it smaller vertically, the search pane becomes obscured.[b]
No matter what layout manager you use, if you resize the frame to make it too small, some controls will get obscured, for obvious reasons. If that is your case, then there is a standard solution to set the minimum dimensions of the frame and to force it programmatically. Something like this:
Ramses Tutoli
Greenhorn

Joined: Sep 05, 2003
Posts: 26
Eugene, thanks for your advice. I actually tried something like you suggested, but I didn't like it, aesthetically.
I just really like the idea of wrapping all the components in the frame around a JScrollPane. I think I'm just going to go with that. It looks better to me. Screw the guidelines . Anyways, there's really no right or wrong answer, right?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scroll around main JFrame?
 
Similar Threads
Reading a file in ScrollPane
Is there any way to bring the a Vertical Scroll Bars of the Scroll Pane at the Bottom by default???
JFrame decorations aren't working
how to draw lines across components
Problems when using JScrollPane to scroll JTextArea