SlideShare a Scribd company logo
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

PDF
The Ring programming language version 1.5.2 book - Part 9 of 181
DOCX
Final_Project
PPT
Progress Dialog, AlertDialog, CustomDialog
DOCX
Add tab jtabbedpane
DOCX
Sumsem2014 15 cp0399-13-jun-2015_rm01_programs
PDF
final project for C#
PDF
tensorflow/keras model coding tutorial 勉強会
PDF
Ordenara los vectores
The Ring programming language version 1.5.2 book - Part 9 of 181
Final_Project
Progress Dialog, AlertDialog, CustomDialog
Add tab jtabbedpane
Sumsem2014 15 cp0399-13-jun-2015_rm01_programs
final project for C#
tensorflow/keras model coding tutorial 勉強会
Ordenara los vectores

What's hot (6)

DOCX
Program klik sederhana
PDF
Getting started with GUI programming in Java_1
PDF
The Ring programming language version 1.5.3 book - Part 9 of 184
PDF
The Ring programming language version 1.5.1 book - Part 8 of 180
PDF
Flash auto play image gallery
Program klik sederhana
Getting started with GUI programming in Java_1
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
Flash auto play image gallery
Ad

Viewers also liked (12)

PPT
4 gu is-andinheritance
PDF
Lec 11 12_sept [compatibility mode]
PDF
The AWT and Swing
PPT
Centipetal force[1]
PPT
PDF
Rock Candy | Beauty & The Beast
PPTX
10.3 Android Video
PPT
PDF
JAVA GUI PART I
PDF
Java OOP Programming language (Part 7) - Swing
PPT
Java swing
PPSX
Centripetal Force
4 gu is-andinheritance
Lec 11 12_sept [compatibility mode]
The AWT and Swing
Centipetal force[1]
Rock Candy | Beauty & The Beast
10.3 Android Video
JAVA GUI PART I
Java OOP Programming language (Part 7) - Swing
Java swing
Centripetal Force
Ad

Similar to Oop lecture9 10 (20)

PPT
Chap1 1 4
PPT
Chap1 1.4
PPT
java swing
PPTX
ADVANCED JAVA PROGRAMME
PPT
Md10 building java gu is
PPTX
LAYOUT.pptx
PPTX
Abstract Window Toolkit_Event Handling_python
PPTX
Advanced Java programming
PPTX
Advanced Java programming
PPTX
Chapter 11.3
PPTX
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
PPT
24-BuildingGUIs Complete Materials in Java.ppt
PDF
Lecture 6.pdf
PPT
Swing basics
PPTX
09 gui
PPTX
09 gui
PPTX
09 gui
PDF
Module 2
DOCX
1 How do you create a frame (AWT or swing)How do you set th
Chap1 1 4
Chap1 1.4
java swing
ADVANCED JAVA PROGRAMME
Md10 building java gu is
LAYOUT.pptx
Abstract Window Toolkit_Event Handling_python
Advanced Java programming
Advanced Java programming
Chapter 11.3
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
24-BuildingGUIs Complete Materials in Java.ppt
Lecture 6.pdf
Swing basics
09 gui
09 gui
09 gui
Module 2
1 How do you create a frame (AWT or swing)How do you set th

More from Shahriar Robbani (14)

PPTX
PPT
Oop lecture9 13
PPT
Oop lecture9 12
PPT
Oop lecture8
PPT
Oop lecture9 11
PPT
Oop lecture4
PPT
Oop lecture2
PPT
Oop lecture9
PPT
Oop lecture7
PPT
Oop lecture5
PPT
Oop lecture3
PPT
Oop lecture1
PPT
Oop lecture6
Oop lecture9 13
Oop lecture9 12
Oop lecture8
Oop lecture9 11
Oop lecture4
Oop lecture2
Oop lecture9
Oop lecture7
Oop lecture5
Oop lecture3
Oop lecture1
Oop lecture6

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Insiders guide to clinical Medicine.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Lesson notes of climatology university.
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Insiders guide to clinical Medicine.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
Institutional Correction lecture only . . .
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
GDM (1) (1).pptx small presentation for students
human mycosis Human fungal infections are called human mycosis..pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?

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