The Artima Developer Community
Sponsored Link

Java Buzz Forum
Desktop Java Live: Introducing EdgeLayout

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Scott Delap

Posts: 1154
Nickname: scottdelap
Registered: Sep, 2004

Client / Server application developer.
Desktop Java Live: Introducing EdgeLayout Posted: Nov 1, 2004 9:51 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Scott Delap.
Original Post: Desktop Java Live: Introducing EdgeLayout
Feed Title: ClientJava.com
Feed URL: http://www.clientjava.com/archives/wireless_mobile.rdf
Feed Description: Client/Desktop Related Java Development
Latest Java Buzz Posts
Latest Java Buzz Posts by Scott Delap
Latest Posts From ClientJava.com

Advertisement

As part of the chapter on layout managers, I decided it would be a good idea to have an example of a custom layout manager. Personally, I've always found that the need for a custom layout manager is driven more by form than function. You can almost always nest enough panels to get the layout you need with the managers included in the JDK. However, what results is not always the most elegant solution.

Lets take this layout as an example:

Basically you have a layout that is defined by the order that components are added to the container. The main characteristic is that as each component is added it gets all the space on its edge unlike BorderLayout. Multiple components can be added to each edge as well. Accomplishing this with only BorderLayout results in:

JPanel outerPanel = new JPanel();
outerPanel.setLayout(new BorderLayout());
outerPanel.add(new JButton("West1"), BorderLayout.WEST);

JPanel innerPanel1 = new JPanel();
innerPanel1.setLayout(new BorderLayout());
innerPanel1.add(new JButton("West2"), BorderLayout.WEST);
innerPanel1.add(new JButton("North1"), BorderLayout.NORTH);

JPanel innerPanel2 = new JPanel();
innerPanel2.setLayout(new BorderLayout());
innerPanel2.add(new JButton("North2"), BorderLayout.NORTH);

JPanel innerPanel3 = new JPanel();
innerPanel3.setLayout(new BorderLayout());
innerPanel3.add(new JButton("East1"), BorderLayout.EAST);

JPanel innerPanel4 = new JPanel();
innerPanel4.setLayout(new BorderLayout());
innerPanel4.add(new JButton("South1"), BorderLayout.SOUTH);

JPanel innerPanel5 = new JPanel();
innerPanel5.setLayout(new BorderLayout());
innerPanel5.add(new JButton("West3"), BorderLayout.WEST);

JPanel innerPanel6 = new JPanel();
innerPanel6.setLayout(new BorderLayout());
innerPanel6.add(new JButton("West4"), BorderLayout.WEST);

JPanel innerPanel7 = new JPanel();
innerPanel7.setLayout(new BorderLayout());
innerPanel7.add(new JButton("Sout3"), BorderLayout.SOUTH);

JPanel innerPanel8 = new JPanel();
innerPanel8.setLayout(new BorderLayout());
innerPanel8.add(new JButton("Sout4"), BorderLayout.SOUTH);

outerPanel.add(innerPanel1, BorderLayout.CENTER);
innerPanel1.add(innerPanel2, BorderLayout.CENTER);
innerPanel2.add(innerPanel3, BorderLayout.CENTER);
innerPanel3.add(innerPanel4, BorderLayout.CENTER);
innerPanel4.add(innerPanel5, BorderLayout.CENTER);
innerPanel5.add(innerPanel6, BorderLayout.CENTER);
innerPanel6.add(innerPanel7, BorderLayout.CENTER);
innerPanel7.add(innerPanel8, BorderLayout.CENTER);
innerPanel8.add(new JButton("Center1"), BorderLayout.CENTER);

return outerPanel;

Now let me introduct EdgeLayout. I'll mention that I haven't searched to see if a layout manager implementing similar functionality already exists. EdgeLayout is writt 1f40 en specifically to the requirements above. Using it for our example results in:

JPanel outerPanel = new JPanel();
outerPanel.setLayout(new EdgeLayout());
outerPanel.add(new JButton("West1"), EdgeLayout.WEST);
outerPanel.add(new JButton("North1"), EdgeLayout.NORTH);
outerPanel.add(new JButton("West2"), EdgeLayout.WEST);
outerPanel.add(new JButton("North2"), EdgeLayout.NORTH);
outerPanel.add(new JButton("East1"), EdgeLayout.EAST);
outerPanel.add(new JButton("South1"), EdgeLayout.SOUTH);
outerPanel.add(new JButton("West3"), EdgeLayout.WEST);
outerPanel.add(new JButton("West4"), EdgeLayout.WEST);
outerPanel.add(new JButton("South2"), EdgeLayout.SOUTH);
outerPanel.add(new JButton("South3"), EdgeLayout.SOUTH);
outerPanel.add(new JButton("Center1"), EdgeLayout.CENTER);

return outerPanel;

Our example went from 38 lines to 14. Not bad if I do say so myself. This is a first draft on the code. Suggestions, comments, and questions are always welcome. While this layout manager is more of a proof of concept, I see no reason why it couldn't be used to assist day to day development.

EdgeLayout Source and Example

Read: Desktop Java Live: Introducing EdgeLayout

Topic: ABOUT / HTTP/1.2 Previous Topic   Next Topic Topic: Announcing Desktop Java Live

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use