Layout Managers... Can I use more than one?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

Layout Managers... Can I use more than one?

 
0
  #1
Apr 3rd, 2008
Hi there everyone,

Is it possible to use more than one Layout Manager in a single GUI interface? I have classes (with event handlers) for various buttons, jtext menus, etc... but so far I've only built simple programs that use a single layout manager. When I call the setLayout() method on my container object I can only specify 1 layout manager. What I'm wondering is if its possible to combine layout managers in a single GUI. The setLayout method only takes one layout manager as a parameter.

Many Thanks!

...Tyster
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Layout Managers... Can I use more than one?

 
0
  #2
Apr 3rd, 2008
Each container can only have one layout manager, however you can easily create any layout you wish by nesting containers with the layout that you want for each one. This could include using a few JPanels inside a JFrame with each JPanel containing a few components. Breaking up the layout like that gives you a lot of control over the grouping and resizing behavior of complex layouts.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

Re: Layout Managers... Can I use more than one?

 
0
  #3
Apr 3rd, 2008
Originally Posted by Ezzaral View Post
Each container can only have one layout manager, however you can easily create any layout you wish by nesting containers with the layout that you want for each one. This could include using a few JPanels inside a JFrame with each JPanel containing a few components. Breaking up the layout like that gives you a lot of control over the grouping and resizing behavior of complex layouts.

Thanks, Ezzaral!

Does anyone know where there might be a code sample for this sort of thing, just so I can get some guidance?

Many Thanks!

...Tyster
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Layout Managers... Can I use more than one?

 
0
  #4
Apr 4th, 2008
This should give you a pretty good idea on how to set them up.
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.FlowLayout;
  4. import java.awt.GridLayout;
  5. import javax.swing.*;
  6.  
  7. public class FrameExample extends JFrame {
  8.  
  9. JPanel panNorth;
  10. JPanel panWest;
  11. JPanel panCenter;
  12.  
  13. public FrameExample() {
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.  
  16. setLayout(new BorderLayout());
  17.  
  18. // set up top panel
  19. panNorth = new JPanel();
  20. panNorth.setLayout(new FlowLayout());
  21. panNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  22. JLabel lblUpper = new JLabel("Upper Panel");
  23. panNorth.add(lblUpper);
  24. add(panNorth, BorderLayout.NORTH);
  25.  
  26. // set up left panel
  27. panWest = new JPanel();
  28. panWest.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  29. panWest.setLayout(new GridLayout(2, 1));
  30. JButton btnFirst = new JButton("First");
  31. panWest.add(btnFirst);
  32. JButton btnSecond = new JButton("Second");
  33. panWest.add(btnSecond);
  34. add(panWest, BorderLayout.WEST);
  35.  
  36. // set up center panel
  37. panCenter = new JPanel();
  38. panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  39. panCenter.setLayout(new BorderLayout());
  40. JLabel lblMiddle = new JLabel("Middle thing");
  41. lblMiddle.setHorizontalAlignment(JLabel.CENTER);
  42. panCenter.add(lblMiddle, BorderLayout.CENTER);
  43. add(panCenter);
  44.  
  45. setSize(300, 300);
  46. setVisible(true);
  47. }
  48.  
  49. public static void main(String args[]) {
  50. new FrameExample();
  51. }
  52. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

Re: Layout Managers... Can I use more than one?

 
0
  #5
Apr 6th, 2008
Thanks a million Ezzaral! That helps alot!

Cheers!

...Tyster
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC