This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
I have got a quesiton when I design a GUI using JScrollPane.
My quesiton is 1. The object scroll doesn't show its scroll bars when resizing the window. I want to know how to show those bars.
2. I use the setBounds method to other components such as JLabel and they don't resize automatically when the window's size changed. How to make those Components change size according to the window's size?
3. How to fix the windows size, e.g. make the Maximum button on the top right corner of the window disable if the 2nd question is more complex to implement?.
1 — how to show the scrollbars JTable is a little tricky because it resizes so readily. You do have some control of the resize behavior with the autoResizeMode property.
Adjusting scrollbars during resizing has to do with layout managers. The layout manager is responsible for finding out the size of the child components of its container, figuring out how much space is needed to lay them out and reporting this to to the container who then makes it available to any parent containers when they ask for it. When you 'setLayout(null)' you give the layout managers a holiday and there's no one home to do the layout work. So you must do all the layout work which can get to be very entertaining or tiresome depending on the mood.
There is some art in selecting the layout manager(s) for the results you want. A good start is to read the Lesson: Laying Out Components Within a Container in the swing tutorial. Also, the api for each class has a discussion section at the top with hints, advice and example code. Start with a few basics like FlowLayout, BorderLayout and GridLayout. Later you can take up the more complicated/specialized ones.
2 — using 'setBounds' and resizing. 'setBounds' works for null layout but not so well with some layout managers; it's like tying the feet of the layout manager and then asking it to dance. If you will allow the component to remain at its preferred size (swing will figure this out for you) then the layout manager will take care of changing the size (and location) according to the space available in the resized/resizing container.
3 — disable the maximize button To investigate this look in the JFrame api, scroll down to the section Methods inherited from class java.awt.Frame and in it find the 'setResizable' method. This means that a JFrame may use this method and that it is inherited from the Frame class. Follow the link for details.
Joined: Aug 10, 2005
Thank you for your answer. After reading this, now I understand the relationship between a Layout and its components.