Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old May 8th, 2008, 07:00 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 188 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 11 h 28 m 25 sec
Reputation Power: 5
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.

Reply With Quote
  #2  
Old May 8th, 2008, 07:41 AM
crownjewel82's Avatar
crownjewel82 crownjewel82 is online now
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,349 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 32039 Folding Title: Starter FolderFolding Points: 32039 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 16 h 9 m 31 sec
Reputation Power: 1935
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

Reply With Quote
  #3  
Old May 8th, 2008, 02:00 PM
jpwojcik jpwojcik is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 11 jpwojcik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 15 h 38 m 47 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old May 8th, 2008, 02:27 PM
crownjewel82's Avatar
crownjewel82 crownjewel82 is online now
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,349 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 32039 Folding Title: Starter FolderFolding Points: 32039 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 16 h 9 m 31 sec
Reputation Power: 1935
Quote:
If you want a easy border layout system, you could always write a library for it.
easy border layout?? What's easier than put it north south east west or center. Seriously it's really not that hard. And if it is then just use absolute positioning.

Reply With Quote
  #5  
Old May 8th, 2008, 02:45 PM
jpwojcik jpwojcik is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 11 jpwojcik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 15 h 38 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by crownjewel82
easy border layout?? What's easier than put it north south east west or center. Seriously it's really not that hard. And if it is then just use absolute positioning.

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.

Reply With Quote
  #6  
Old May 8th, 2008, 02:50 PM
fishtoprecords's Avatar
fishtoprecords fishtoprecords is online now
Contributing User
Click here for more information.
 
Join Date: Sep 2007
Location: outside Washington DC
Posts: 964 fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)fishtoprecords User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 22 h 58 m 13 sec
Reputation Power: 418
Quote:
Originally Posted by WebDunce
Anyone know why Java doesn't have something so easy for positioning? Or do they and I've missed it?


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.

Reply With Quote
  #7  
Old May 8th, 2008, 08:30 PM
crownjewel82's Avatar
crownjewel82 crownjewel82 is online now
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,349 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 32039 Folding Title: Starter FolderFolding Points: 32039 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 16 h 9 m 31 sec
Reputation Power: 1935
That's why you break up the task.

Reply With Quote
  #8  
Old May 9th, 2008, 04:42 AM
Hugh of Borg's Avatar
Hugh of Borg Hugh of Borg is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Switzerland
Posts: 568 Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 18 h 58 m 5 sec
Reputation Power: 82
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!

Reply With Quote
  #9  
Old May 9th, 2008, 06:01 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 188 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 11 h 28 m 25 sec
Reputation Power: 5
Quote:
Originally Posted by crownjewel82
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.


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

Reply With Quote
  #10  
Old May 9th, 2008, 06:18 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 188 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 11 h 28 m 25 sec
Reputation Power: 5
Quote:
Originally Posted by fishtoprecords
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.


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.

Reply With Quote
  #11  
Old May 9th, 2008, 06:23 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 188 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 11 h 28 m 25 sec
Reputation Power: 5
Quote:
Originally Posted by Hugh of Borg
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...


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.

Reply With Quote
  #12  
Old May 9th, 2008, 06:47 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 188 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 11 h 28 m 25 sec
Reputation Power: 5
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.

Reply With Quote
  #13  
Old May 9th, 2008, 07:11 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 188 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 11 h 28 m 25 sec
Reputation Power: 5
Quote:
Originally Posted by jpwojcik
...you could always write a library for it.


get ready for "swing reloaded"

Last edited by WebDunce : May 9th, 2008 at 08:28 AM.

Reply With Quote
  #14