| | |
Layout Managers... Can I use more than one?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
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
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
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.
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
•
•
•
•
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
This should give you a pretty good idea on how to set them up.
java Syntax (Toggle Plain Text)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*; public class FrameExample extends JFrame { JPanel panNorth; JPanel panWest; JPanel panCenter; public FrameExample() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // set up top panel panNorth = new JPanel(); panNorth.setLayout(new FlowLayout()); panNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); JLabel lblUpper = new JLabel("Upper Panel"); panNorth.add(lblUpper); add(panNorth, BorderLayout.NORTH); // set up left panel panWest = new JPanel(); panWest.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); panWest.setLayout(new GridLayout(2, 1)); JButton btnFirst = new JButton("First"); panWest.add(btnFirst); JButton btnSecond = new JButton("Second"); panWest.add(btnSecond); add(panWest, BorderLayout.WEST); // set up center panel panCenter = new JPanel(); panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); panCenter.setLayout(new BorderLayout()); JLabel lblMiddle = new JLabel("Middle thing"); lblMiddle.setHorizontalAlignment(JLabel.CENTER); panCenter.add(lblMiddle, BorderLayout.CENTER); add(panCenter); setSize(300, 300); setVisible(true); } public static void main(String args[]) { new FrameExample(); } }
![]() |
Similar Threads
- Creating first GUI Java Program (Java)
- programming language recommendation (Computer Science)
- Beware the GUI builder (Java)
- GridBag layout problems. (Java)
- Determining correct size of JDesktopPane (Java)
- Set Size for JSlider (Java)
- Panel Layouts (Java)
Other Threads in the Java Forum
- Previous Thread: Java File I/O parsing help
- Next Thread: need help create an argorithm to calculate money change
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






