|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Why did they make positioning so hard?
Java is a mighty fine language in a lot of ways, but it is a real headache (for me) to try to position components on a form so that they stretch or move appropriately as the window is resized.
Some layout methods are simple, like FlowLayout. But the layout methods that allow complex layouts (GroupLayout, SpringLayout, and GridBagLayout) seem difficult to master. After much study, I was able to get GroupLayout to work. But the very simple layout (6 components) required much thought and coding to get right. In contrast, .NET's anchor property is easy to use, easy to understand, and easy to code (even easier to do in the visual editor). Below is a code snippet that shows positioning a textbox and a button under it so that the textbox will stretch in all directions and the button will maintain its position just under the textbox no matter how tall or short (within certain limits on the short side) the form gets. Code:
// assume we're placing things on a form here TextBox tb = new TextBox(); tb.Size = new Size(200, 100); tb.Location = new Point(10, 10); tb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right; Button btn= new Button(); btn.Size = new Size(75, 23); btn.Location = new Point(10, 110); // 10 pixels under the textbox btn.Anchor = AnchorStyles.Left | AnchorStyles.Bottom Anyone know why Java doesn't have something so easy for positioning? Or do they and I've missed it? (I'm not losing sleep over this...just curious) Cheers, --WebDunce note: .NET has some complicated layout methods, too, and I could barely understand them either...but this anchor thing seems to work really well. Last edited by WebDunce : May 8th, 2008 at 07:43 AM. |
|
#2
|
||||
|
||||
|
The trick is in planning. If you break up your layout into smaller panels you can get everything you want normally with just the simpler layout tools.
I usually can do everything I want with FlowLayout, BorderLayout, and BoxLayout.
__________________
The day I get my hands on the cookbook it's all over. -nicky |
|
#3
|
|||
|
|||
|
If you want a easy border layout system, you could always write a library for it.
The thing is, with most programming languages, writing a GUI isn't an easy task. The only reason you usually would write a GUI is if you have to for school project, otherwise just use a GUI builder. (Or you could develop some layouts and use them in a few projects. |
|
#4
|
||||
|
||||
|
Quote:
|
|
#5
|
|||
|
|||
|
Quote:
BorderLayout gets to be a bit long when you do more complicated layouts. I'd rather do the logic to a program than program the GUI. |
|
#6
|
||||
|
||||
|
Quote:
I think most folks let their IDE's gui tool handle it. Doing it by hand is, as you say, a PITA. I have used Eclipse, and now use NetBeans, and the tools make it painless. |
|
#7
|
||||
|
||||
|
That's why you break up the task.
|
|
#8
|
||||
|
||||
|
Funny, the first thing I really missed in C# were my trusty Java LayoutManagers...
There's not much I can add to the given tipps except maybe a hint towards the GridBagLayout. It requires a bit more typing but it's very versatile and flexible. If your layout isn't too complex and you dislike nesting panels then that's worth a look. The SpringLayout could probably be something for you too. I've never used it but it seems to work on similar principles as you are accustomed from .NET. There's an official Factory class floating about on the sun website somewhere that makes using the layout much easier...
__________________
- Hugh of Borg The first thing young borg are taught: Keep away from Microsoft software! |
|
#9
|
|||
|
|||
|
Quote:
i had forgotten about breaking things down into smaller bits to use the simpler layout tools...thanks for the tip / reminder. I had once known this concept, but as I hardly use Java, had forgotten about it. The last project I did in Java, I forced myself to use the GroupLayout manager because I wanted to understand the code NetBeans was producing for my GUIs. Cheers, --WebDunce |
|
#10
|
|||
|
|||
|
Quote:
You're right, the IDE does take the pain out of it. Interestingly, I did the (very) little project (6 visible components on a form and maybe 2 additional classfiles) both in NetBeans and manually in notepad...the netbeans jar was just under 300KB...the notepad jar was 9KB. I originally did it in Visual C# (Express)...it compiled to 24KB, i think. Cheers, --WebDunce Last edited by WebDunce : May 9th, 2008 at 06:29 AM. |
|
#11
|
|||
|
|||
|
Quote:
I checked out the SpringLayout...it was awhile back but i seem to recall it being the most complex of them all...after hours of reading I gave up trying to understand it. The GridBag escaped my comprehension, too. I was finally able to understand the GroupLayout (but don't ask me now...that was a year ago). DotNet has the familiar FlowLayout panels (which come in very handy) and a TableLayoutPanel that I could never figure out how to use...but it might be similar to Java's GridBagLayout. Have you ever used the anchor property in any of your C# projects? It is available for all controls (sfaik), and "anchors" the control's four edges (independently) relative to it's parent's four edges...it's very easy and intuitive (and can be done programmatically or via the control's editable properties in the visual editor). All controls default to having anchor properties set to "top & left" Sometimes issues can arise from sizing the window too small to contain all the objects and you may need to contain the objects in a scrollable panel or something...or set a minimum size for the main form. Cheers, --WebDunce Last edited by WebDunce : May 9th, 2008 at 07:08 AM. |
|
#12
|
|||
|
|||
|
But, the question is, WHY doesn't Java have something comparable to the anchor property? It's like they went out of their way not to put such functionality in the awt/swing libraries...was it an oversight, was it done on purpose due to technical limitations...why?
(I don't actually expect anyone to be able to answer the question) cheers, --WebDunce Last edited by WebDunce : May 9th, 2008 at 08:29 AM. |
|
#13
|
|||
|
|||
|
Quote:
get ready for "swing reloaded" ![]() Last edited by WebDunce : May 9th, 2008 at 08:28 AM. |
|
#14
|