SlideShare a Scribd company logo
1 of 14
Lecture 10
Swing layouts




        Object Oriented Programming
         Eastern University, Dhaka
                 Md. Raihan Kibria
Specifying no layout in JFrame
public class DefaultLayoutDemo {

     public static void main(String[] args) {
       JFrame frame = new JFrame("DefaultLayoutDemo");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         //Set up the content pane.
         frame.add(new JButton("a"));
         frame.add(new JButton("b"));

         //Display the window.
         frame.pack();
         frame.setVisible(true);
     }
}
Specifying positions of controls
public class DefaultLayoutDemo {

     public static void main(String[] args) {
       JFrame frame = new JFrame("DefaultLayoutDemo");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         //Set up the content pane.
         frame.add(new JButton("a"), BorderLayout.NORTH);
         frame.add(new JButton("b"), BorderLayout.SOUTH);

         //Display the window.
         frame.pack();
         frame.setVisible(true);
     }
}




     The default layout for JFrame is
     BorderLayout
BorderLayout
public class BorderLayoutDemo {

   public static boolean RIGHT_TO_LEFT = false;
   public static void addComponentsToPane(Container pane) {

       if (RIGHT_TO_LEFT) {
           pane.setComponentOrientation(
                   java.awt.ComponentOrientation.RIGHT_TO_LEFT);
       }

       JButton button = new JButton("Button 1 (PAGE_START)");
       pane.add(button, BorderLayout.PAGE_START);

       button = new JButton("Button 2 (CENTER)");
       button.setPreferredSize(new Dimension(200, 100));
       pane.add(button, BorderLayout.CENTER);

       button = new JButton("Button 3 (LINE_START)");
       pane.add(button, BorderLayout.LINE_START);

       button = new JButton("Long-Named Button 4 (PAGE_END)");
       pane.add(button, BorderLayout.PAGE_END);

       button = new JButton("5 (LINE_END)");
       pane.add(button, BorderLayout.LINE_END);
   }
public static void main(String[] args) {
           JFrame frame = new JFrame("BorderLayoutDemo");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           addComponentsToPane(frame.getContentPane());
           frame.pack();
           frame.setVisible(true);
     }
 }




If you stretch the frame the center part gets filled by the control in it
BoxLayout
public class BoxLayoutDemo {
    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        addAButton("Button 1", pane);
        addAButton("Button 2", pane);
        addAButton("Button 3", pane);
        addAButton("Long-Named Button 4", pane);
        addAButton("5", pane);
    }

    private static void addAButton(String text, Container container) {
        JButton button = new JButton(text);
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(button);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("BoxLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }
}
Result



There is no resizing when the frame is stretched out
FlowLayout
public class FlowLayoutDemo extends JFrame{
    FlowLayout experimentLayout = new FlowLayout();

   public FlowLayoutDemo(String name) {
       super(name);
   }

   public void addComponentsToPane(final Container pane) {
       JPanel compsToExperiment = new JPanel();
       compsToExperiment.setLayout(experimentLayout);
       experimentLayout.setAlignment(FlowLayout.LEADING);

       //Add buttons to the experiment layout
       compsToExperiment.add(new JButton("Button 1"));
       compsToExperiment.add(new JButton("Button 2"));
       compsToExperiment.add(new JButton("Button 3"));
       compsToExperiment.add(new JButton("Long-Named Button 4"));
       compsToExperiment.add(new JButton("5"));
       //Left to right component orientation is selected by default
       compsToExperiment.setComponentOrientation(
               ComponentOrientation.LEFT_TO_RIGHT);

       pane.add(compsToExperiment, BorderLayout.CENTER);
   }
public static void main(String[] args) {
        FlowLayoutDemo frame = new FlowLayoutDemo("FlowLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
      }
}




    If we stretch the controls are still pinned to the left because
    of this:
     experimentLayout.setAlignment(FlowLayout.LEADING);
GridLayout
public class GridLayoutDemo {

 public static void addComponentsToPane(Container pane) {
   pane.setLayout(new GridLayout(3, 2));
   JButton b1 = new JButton("First");
   pane.add(b1);
   b1 = new JButton("Second");
   pane.add(b1);
   b1 = new JButton("Third");
   pane.add(b1);
   b1 = new JButton("Fourth");
   pane.add(b1);
   b1 = new JButton("Fifth");
   pane.add(b1);
   b1 = new JButton("Sixth");
   pane.add(b1);
 }

 public static void main(String[] args) {

     JFrame frame = new JFrame("GridBagLayoutDemo");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     addComponentsToPane(frame.getContentPane());
     frame.pack();
     frame.setVisible(true);
 }
Result




3 columns and 2 rows as set in:
pane.setLayout(new GridLayout(3, 2));
GridBagLayout
public class GridBagLayoutDemo {

 public static void addComponentsToPane(Container pane) {

   JButton button;
   pane.setLayout(new GridBagLayout());
   GridBagConstraints c = new GridBagConstraints();

   button = new JButton("Button 1");
   c.gridx = 0;
   c.gridy = 0;
   pane.add(button, c);

   button = new JButton("Button 2");
   c.gridx = 1;
   c.gridy = 0;
   pane.add(button, c);

   button = new JButton("Button 3");
   c.gridx = 2;
   c.gridy = 0;
   pane.add(button, c);

   button = new JButton("Long-Named Button 4");
   c.gridx = 0;
   c.gridy = 1;
   pane.add(button, c);
button = new JButton("5");
        c.gridx = 1;
        c.gridy = 2;
        pane.add(button, c);
    }

    public static void main(String[] args) {
      JFrame frame = new JFrame("GridBagLayoutDemo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      addComponentsToPane(frame.getContentPane());
      frame.pack();
      frame.setVisible(true);
    }
}




    GridBagLayout is very flexible layout
Questions
   What is the most flexible of these layouts
   Whichone is easy to implement
   Which layout to choose when you want
    your control to take up as much space as
    possible

   Interested in more? -
      GroupLayout
      SpringLayout

More Related Content

What's hot (6)

Program klik sederhana
Program klik sederhanaProgram klik sederhana
Program klik sederhana
 
Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1
 
Action script
Action scriptAction script
Action script
 
The Ring programming language version 1.5.3 book - Part 9 of 184
The Ring programming language version 1.5.3 book - Part 9 of 184The Ring programming language version 1.5.3 book - Part 9 of 184
The Ring programming language version 1.5.3 book - Part 9 of 184
 
The Ring programming language version 1.5.1 book - Part 8 of 180
The Ring programming language version 1.5.1 book - Part 8 of 180The Ring programming language version 1.5.1 book - Part 8 of 180
The Ring programming language version 1.5.1 book - Part 8 of 180
 
Flash auto play image gallery
Flash auto play image galleryFlash auto play image gallery
Flash auto play image gallery
 

Viewers also liked

4 gu is-andinheritance
4 gu is-andinheritance4 gu is-andinheritance
4 gu is-andinheritance
notshoaib
 
Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]
Palak Sanghani
 

Viewers also liked (12)

4 gu is-andinheritance
4 gu is-andinheritance4 gu is-andinheritance
4 gu is-andinheritance
 
Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
Centipetal force[1]
Centipetal force[1]Centipetal force[1]
Centipetal force[1]
 
UCM 6
UCM 6UCM 6
UCM 6
 
Rock Candy | Beauty & The Beast
Rock Candy | Beauty & The BeastRock Candy | Beauty & The Beast
Rock Candy | Beauty & The Beast
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android Video
 
Swing
SwingSwing
Swing
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
 
Java swing
Java swingJava swing
Java swing
 
Centripetal Force
Centripetal ForceCentripetal Force
Centripetal Force
 

Similar to Oop lecture9 10

Md10 building java gu is
Md10 building java gu isMd10 building java gu is
Md10 building java gu is
Rakesh Madugula
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
venkt12345
 
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
mohammedfootwear
 
Tugas Praktikum Java 2
Tugas Praktikum Java 2Tugas Praktikum Java 2
Tugas Praktikum Java 2
azmi007
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
apexcomputer54
 

Similar to Oop lecture9 10 (20)

Nouveau document texte-_-_
Nouveau document texte-_-_Nouveau document texte-_-_
Nouveau document texte-_-_
 
ch20.pptx
ch20.pptxch20.pptx
ch20.pptx
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 
Md10 building java gu is
Md10 building java gu isMd10 building java gu is
Md10 building java gu is
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
 
Oop lecture9
Oop lecture9Oop lecture9
Oop lecture9
 
Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
 
011 more swings_adv
011 more swings_adv011 more swings_adv
011 more swings_adv
 
Chap1 1 4
Chap1 1 4Chap1 1 4
Chap1 1 4
 
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
 
culadora cientifica en java
culadora cientifica en javaculadora cientifica en java
culadora cientifica en java
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
09 gui
09 gui09 gui
09 gui
 
09 gui
09 gui09 gui
09 gui
 
09 gui
09 gui09 gui
09 gui
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 
Tugas Praktikum Java 2
Tugas Praktikum Java 2Tugas Praktikum Java 2
Tugas Praktikum Java 2
 
Creating a Facebook Clone - Part XVI.pdf
Creating a Facebook Clone - Part XVI.pdfCreating a Facebook Clone - Part XVI.pdf
Creating a Facebook Clone - Part XVI.pdf
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 

More from Shahriar Robbani (13)

Group111
Group111Group111
Group111
 
SQL
SQLSQL
SQL
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Oop lecture9 12
Oop lecture9 12Oop lecture9 12
Oop lecture9 12
 
Oop lecture8
Oop lecture8Oop lecture8
Oop lecture8
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 
Oop lecture4
Oop lecture4Oop lecture4
Oop lecture4
 
Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
Oop lecture3
Oop lecture3Oop lecture3
Oop lecture3
 
Oop lecture1
Oop lecture1Oop lecture1
Oop lecture1
 
Oop lecture6
Oop lecture6Oop lecture6
Oop lecture6
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

Oop lecture9 10

  • 1. Lecture 10 Swing layouts Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • 2. Specifying no layout in JFrame public class DefaultLayoutDemo { public static void main(String[] args) { JFrame frame = new JFrame("DefaultLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. frame.add(new JButton("a")); frame.add(new JButton("b")); //Display the window. frame.pack(); frame.setVisible(true); } }
  • 3. Specifying positions of controls public class DefaultLayoutDemo { public static void main(String[] args) { JFrame frame = new JFrame("DefaultLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. frame.add(new JButton("a"), BorderLayout.NORTH); frame.add(new JButton("b"), BorderLayout.SOUTH); //Display the window. frame.pack(); frame.setVisible(true); } } The default layout for JFrame is BorderLayout
  • 4. BorderLayout public class BorderLayoutDemo { public static boolean RIGHT_TO_LEFT = false; public static void addComponentsToPane(Container pane) { if (RIGHT_TO_LEFT) { pane.setComponentOrientation( java.awt.ComponentOrientation.RIGHT_TO_LEFT); } JButton button = new JButton("Button 1 (PAGE_START)"); pane.add(button, BorderLayout.PAGE_START); button = new JButton("Button 2 (CENTER)"); button.setPreferredSize(new Dimension(200, 100)); pane.add(button, BorderLayout.CENTER); button = new JButton("Button 3 (LINE_START)"); pane.add(button, BorderLayout.LINE_START); button = new JButton("Long-Named Button 4 (PAGE_END)"); pane.add(button, BorderLayout.PAGE_END); button = new JButton("5 (LINE_END)"); pane.add(button, BorderLayout.LINE_END); }
  • 5. public static void main(String[] args) { JFrame frame = new JFrame("BorderLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } } If you stretch the frame the center part gets filled by the control in it
  • 6. BoxLayout public class BoxLayoutDemo { public static void addComponentsToPane(Container pane) { pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); addAButton("Button 1", pane); addAButton("Button 2", pane); addAButton("Button 3", pane); addAButton("Long-Named Button 4", pane); addAButton("5", pane); } private static void addAButton(String text, Container container) { JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); } public static void main(String[] args) { JFrame frame = new JFrame("BoxLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } }
  • 7. Result There is no resizing when the frame is stretched out
  • 8. FlowLayout public class FlowLayoutDemo extends JFrame{ FlowLayout experimentLayout = new FlowLayout(); public FlowLayoutDemo(String name) { super(name); } public void addComponentsToPane(final Container pane) { JPanel compsToExperiment = new JPanel(); compsToExperiment.setLayout(experimentLayout); experimentLayout.setAlignment(FlowLayout.LEADING); //Add buttons to the experiment layout compsToExperiment.add(new JButton("Button 1")); compsToExperiment.add(new JButton("Button 2")); compsToExperiment.add(new JButton("Button 3")); compsToExperiment.add(new JButton("Long-Named Button 4")); compsToExperiment.add(new JButton("5")); //Left to right component orientation is selected by default compsToExperiment.setComponentOrientation( ComponentOrientation.LEFT_TO_RIGHT); pane.add(compsToExperiment, BorderLayout.CENTER); }
  • 9. public static void main(String[] args) { FlowLayoutDemo frame = new FlowLayoutDemo("FlowLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } } If we stretch the controls are still pinned to the left because of this: experimentLayout.setAlignment(FlowLayout.LEADING);
  • 10. GridLayout public class GridLayoutDemo { public static void addComponentsToPane(Container pane) { pane.setLayout(new GridLayout(3, 2)); JButton b1 = new JButton("First"); pane.add(b1); b1 = new JButton("Second"); pane.add(b1); b1 = new JButton("Third"); pane.add(b1); b1 = new JButton("Fourth"); pane.add(b1); b1 = new JButton("Fifth"); pane.add(b1); b1 = new JButton("Sixth"); pane.add(b1); } public static void main(String[] args) { JFrame frame = new JFrame("GridBagLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); }
  • 11. Result 3 columns and 2 rows as set in: pane.setLayout(new GridLayout(3, 2));
  • 12. GridBagLayout public class GridBagLayoutDemo { public static void addComponentsToPane(Container pane) { JButton button; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); button = new JButton("Button 1"); c.gridx = 0; c.gridy = 0; pane.add(button, c); button = new JButton("Button 2"); c.gridx = 1; c.gridy = 0; pane.add(button, c); button = new JButton("Button 3"); c.gridx = 2; c.gridy = 0; pane.add(button, c); button = new JButton("Long-Named Button 4"); c.gridx = 0; c.gridy = 1; pane.add(button, c);
  • 13. button = new JButton("5"); c.gridx = 1; c.gridy = 2; pane.add(button, c); } public static void main(String[] args) { JFrame frame = new JFrame("GridBagLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } } GridBagLayout is very flexible layout
  • 14. Questions  What is the most flexible of these layouts  Whichone is easy to implement  Which layout to choose when you want your control to take up as much space as possible  Interested in more? -  GroupLayout  SpringLayout