Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

Hand-coding SpringLayout

Thread view: 
Simon - 14 Dec 2003 20:36 GMT
Hi,

The following code extract successfully creates a list box anchored to
the top-left, and that expands vertically, but has a fixed width.

public class EPoS3 extends JFrame {

 public EPoS3() {
   super("EPoS");
 
   this.setSize(600, 400);
   this.setLocation(50, 50);

   Container content = this.getContentPane();
   SpringLayout sl = new SpringLayout();
   content.setLayout(sl);

   //--"Messages" list box--
   String items [] = { "Alpha", "Beta", "Gamma" };
   JList lstMessages = new JList(items);  
   
   //--Current Message panel--
   JPanel pnlMessage = new JPanel();
   pnlMessage.setBackground(Color.blue);
   
   //--Spring layout--
   content.add(lstMessages);
 
   sl.getConstraints(lstMessages).setHeight(Spring.constant(10, 100,
32767));

   sl.getConstraints(lstMessages).setWidth(Spring.constant(100));
   
   sl.putConstraint(SpringLayout.WEST, lstMessages,
                    5,
                    SpringLayout.WEST, content);
                   
   sl.putConstraint(SpringLayout.NORTH, lstMessages,
                    5,
                    SpringLayout.NORTH, content);

   sl.putConstraint(SpringLayout.EAST, content,
                    5,
                    SpringLayout.EAST, lstMessages);
                   
   sl.putConstraint(SpringLayout.SOUTH, content,
                    5,
                    SpringLayout.SOUTH, lstMessages);
 }
}

Q1: For the first two constraints the anchor is the panel and the
dependant is the listbox. However for the last two, this is inverted.
I can't quite figure out why, this may explain why I'm having other
issues.

If the following is now added to the bottom...

   //--panel--
   content.add(pnlMessage);
 
   sl.getConstraints(pnlMessage).setHeight(Spring.constant(10, 100,
32767));

   sl.getConstraints(pnlMessage).setWidth(Spring.constant(10, 10,
32767));
   
   sl.putConstraint(SpringLayout.WEST, pnlMessage,
                    5,
                    SpringLayout.EAST, lstMessages);
                   
   sl.putConstraint(SpringLayout.NORTH, pnlMessage,
                    5,
                    SpringLayout.NORTH, content);

   sl.putConstraint(SpringLayout.EAST, content,
                    5,
                    SpringLayout.EAST, pnlMessage);
                   
   sl.putConstraint(SpringLayout.SOUTH, content,
                    5,
                    SpringLayout.SOUTH, pnlMessage);

Q2: This no longer works.  The list box is suddenly taking on the
preferred height instead of expanding with a 5 pixel margin. If I set
it to a very large value, the margin is not created.  I've tried
switching the south constraint anchor/dependency around, and both
combinations using the panel and listbox, i.e. linking the south of
the panel to the south of the listbox.

I've looked at the SUN + O'Reilly tutorials on SpringLayout, but still
can't get it. All Help gratefully received, as this is driving me
nuts.

I'm using 1.4.2_02 on Win XP.

Cheers

Simon
Simon - 16 Dec 2003 09:50 GMT
I've think I've figured it out, but have decided to abandon
SpringLayout, and use RelativeLayout (see O'Reilly web site). So for
anyone who was curious...

> Q1: For the first two constraints the anchor is the panel and the
> dependant is the listbox. However for the last two, this is inverted.
> I can't quite figure out why, this may explain why I'm having other
> issues.

I'm guessing this is because I'm using positive values, I think the
anchor value needs to be negative for the last two items, due to the
direction.

> Q2: This no longer works.  The list box is suddenly taking on the
> preferred height instead of expanding with a 5 pixel margin. If I set
> it to a very large value, the margin is not created.  I've tried
> switching the south constraint anchor/dependency around, and both
> combinations using the panel and listbox, i.e. linking the south of
> the panel to the south of the listbox.

This is because an edge can only support one Spring. When I use the
second object, the first Spring for the relevant Container edge is
automatically dropped.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.