SlideShare a Scribd company logo
1 of 34
Download to read offline
Lecture 6
Getting GUIer
GUIS
▪ Recall from the previous lecture: create a frame and
then get the content pane for that frame
▪ Now we’re going to place some stuff on the pane.
▪ Recall that the different elements on the frame are
going to be objects
• Frame objects
• Button objects
• TextField objects
• Label objects
• Menu objects
• Checkbox objects
• List objects
• Radio objects
GUIS
Recall from the last lecture:
▪ Many of the GUI components are from the javax.swing
package
▪ Also java.awt package
GUIS
▪ Generally, these objects have methods that we can use
to interact with them:
– A.add(B) – adds component B to component A
– A.setSize(width, height)
– A.setLayout(…)
– A.setVisible(…)
▪ Will see examples of this in the next lecture
▪ This lecture will focus on placement.
Adding objects to frames
Adding objects to frames
Object placement
▪ Components on our frames are objects created from different
classes – e.g. button objects, checkbox objects etc.
▪ We can place them on different parts of the frame, for now,
placing them on content pane
Adding objects to frames
▪ Recall from previous lecture, the part of the frame that
we put stuff on is called the content pane – everything
but the title, menu bars and border.
▪ When we add stuff to our frame, we actually add it to
the content pane
Frame
Content pane
Buttons
Adding objects to frames
Object placement
▪ So we need to get hold of the content pane before we can add
stuff to it:
Container cp = jf.getContentPane();
▪ Once we have something to add to the content pane…
okButton = //Code for creating buttons – see later
▪ … we use the content pane’s add() method.
cp.add(okButton);
Object Placement
Object Placement
▪ We need a way to determine where objects are placed
▪ Two ways:
– Layout manager
– Absolute positioning (doesn’t use a Layout Manager)
▪ Either way, done through the setLayout(…) method.
Object Placement
▪ Have to decide what we giving a Layout Manager e.g.
content pane.
▪ If it is the content pane, we get need get it:
Container cp = jf.getContentPane();
Object Placement
▪ Absolute positioning: each GUI object’s position is
specified explicitly.
▪ Use the setLayout() method of the content pane object.
Container cp = jf.getContentPane();
...
cp.setLayout(null);
▪ Then, when objects are added, need to specify their
size and position – will see later.
Object Placement
Layout manager: object that controls
the placement of GUI objects.
1 2 3
4 5 6
NORTH
SOUTH
WEST EAST
CENTER
GUIS – to be omitted
Example:
▪ FlowLayout puts the objects left to
right, top to bottom
▪ GridLayout puts the objects in a
grid
▪ BorderLayout, CardLayout…
1 2 3
4 5 6
NORTH
SOUTH
WEST EAST
CENTER
Object Placement
Setting the Layout Manager:
▪ Again, assume we are giving the content pane a Layout
Manager
Container cp = jf.getContentPane();
▪ Then call the setLayout(…) method and pass it an instance of
the layout class
FlowLayout LM = new FlowLayout();
cp.setLayout(LM);
Object Placement
▪ Alternatively…
cp.setLayout(new FlowLayout());
▪ Or, if we want to be really fancy:
jf.getContentPane().setLayout(new FlowLayout());
▪ If we want to use a different layout manager:
cp.setLayout(new BorderLayout());
cp.setLayout(new GridLayout());
Different Layout Managers
GUIs ‐ FlowLayout
▪ FlowLayout: GUI components are
placed left to right, top to bottom.
GUIs ‐ FlowLayout
▪ We can specify some options when we create layout manager
objects by passing class constants to the constructor:
▪ Recall: class constants are a way to give meaningful names to
certain values.
▪ So when creating a new FlowLayout object, we can pass it a
class constant from the FlowLayout class to specify e.g. centre
alignment
cp.setLayout(new FlowLayout(FlowLayout.CENTER));
cp.setLayout(new FlowLayout(FlowLayout.LEFT));
▪ Default is FlowLayout.CENTER
GUIs ‐ FlowLayout
▪ Can also specify the gap between components
cp.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
GUIs – BorderLayout
▪ BorderLayout: GUI components are
arranged in five regions: North, South,
East, West and Centre.
▪ Call the setLayout() method but pass it a
BorderLayout object:
cp.setLayout(new BorderLayout());
NORTH
SOUTH
WEST EAST
CENTER
• Can also change the gaps:
cp.setLayout(new BorderLayout(5, 10));
GUIs – BorderLayout
▪ BorderLayout:
▪ North, South, East and West regions take
as much space as they need, Centre
region takes the rest.
NORTH
SOUTH
WEST EAST
CENTER
• When we add components now, the second argument is the
region, again specified with class constants:
cp.add(okButton, BorderLayout.WEST);
cp.add(cancelButton, BorderLayout.EAST);
GUIS ‐ GridLayout
▪ GridLayout: GUI components are arranged
in a grid, specified on creation
▪ Each “cell” on the grid is the same size
▪ Call the setLayout() method, pass it a
GridLayout object, with no of rows and cols…
cp.setLayout(new GridLayout(2, 3));
1 2 3
4 5 6
• … or can also specify the gaps
cp.setLayout(new GridLayout(2, 3, 5, 5));
GUIS ‐ GridLayout
1 2 3
4 5 6
• Added components are then added to fill
row by row:
cp.add(Button1);
cp.add(Button2);
cp.add(Button3);
...
• Also, GridBayLayout – more control
Panels
Panels
▪ So far we have set the layout of content panes…
Container cp = jf.getContentPane();
cp.setLayout(new FlowLayout());
▪ …but now we are going to set the layout of panels
▪ Panels are JPanel objects which are like mini
content panes
– Can add components to panels
– Set panel’s layout
▪ Enable the sectioning of the frame into different logical
sections.
Panels
▪ Content pane has a
layout manager, its
components are
arranged accordingly.
▪ But if a component is a
panel, it will have its
own layout manager
for the components
inside of it.
▪ We can even have
nested panels – panels
within panels
Frame
Content pane
Buttons
Panel
Panel
Back to Layout Managers
GUIS ‐ BoxLayout
▪ BoxLayout: like an advanced FlowLayout
▪ Components are arranged in either vertical or horizontal
orientation – horizontal can be left-to-right or right-to-left
GUIS ‐ BoxLayout
▪ Can also add invisible components to help
with the spacing:
▪ Rigid area – creates a fixed space between
components
▪ Box class has a class method to return a
rigid area:
▪ This method takes a Dimension object –
specifies the height and width in pixels
Box.createRigidArea(new Dimension(10, 0))
GUIS ‐ BoxLayout
▪ Can also add invisible components to help
with the spacing:
▪ Glue – creates space as much horizontal or
vertical space as necessary to shove the
components to the border.
▪ Box class has a class method to return glue:
Box.createHorizontalGlue();
Box.createVerticalGlue();
GUIS ‐ BoxLayout
▪ Can also add invisible components to help
with the spacing:
▪ Filler – creates a flexible space with a
maximum, minimum and preferred size.
▪ Box class can be used to create Filler
objects, constructor takes Dimension objects:
new Box.Filler( , , )
new Dimension(10,10)
new Dimension(10,20)
new Dimension(10,30)
GUIS ‐ BoxLayout
▪ So let’s put this altogether – let’s imagine we have a
panel on our form that contains the buttons:
▪ Create a panel:
JPanel buttonPanel = new JPanel();
▪ Set the panel’s layout, by passing it a BoxLayout object
– takes:
– Object it is being applied to and the axis (top-down vs left-right)
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
OR
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
GUIS ‐ BoxLayout
▪ So let’s put this altogether – let’s imagine we have a panel on our
form that contains the buttons:
▪ Create buttons (don’t worry about this for now):
JButton okButton = new JButton("OK");
JButton cancelButton = new JButton("Cancel");
▪ Now add the buttons with some “invisible components”
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(okButton);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(cancelButton);

More Related Content

Similar to Lecture 6.pdf

Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
sotlsoc
 
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxasmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
fredharris32
 
13 gui development
13 gui development13 gui development
13 gui development
APU
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
arihantcomp1008
 
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
Muhammad Shebl Farag
 

Similar to Lecture 6.pdf (20)

Java GUI Programming for beginners-graphics.pdf
Java GUI Programming for beginners-graphics.pdfJava GUI Programming for beginners-graphics.pdf
Java GUI Programming for beginners-graphics.pdf
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxasmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
 
Oop bai10
Oop bai10Oop bai10
Oop bai10
 
swingbasics
swingbasicsswingbasics
swingbasics
 
Evergreen websites for Evergreen browsers
Evergreen websites for Evergreen browsersEvergreen websites for Evergreen browsers
Evergreen websites for Evergreen browsers
 
Initial UI Mockup - Part 3 - Transcript.pdf
Initial UI Mockup - Part 3 - Transcript.pdfInitial UI Mockup - Part 3 - Transcript.pdf
Initial UI Mockup - Part 3 - Transcript.pdf
 
Frontend United: Start using CSS Grid Layout today!
Frontend United: Start using CSS Grid Layout today!Frontend United: Start using CSS Grid Layout today!
Frontend United: Start using CSS Grid Layout today!
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Advanced Silverlight
Advanced SilverlightAdvanced Silverlight
Advanced Silverlight
 
28 awt
28 awt28 awt
28 awt
 
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
13 gui development
13 gui development13 gui development
13 gui development
 
Lecture 5.pdf
Lecture 5.pdfLecture 5.pdf
Lecture 5.pdf
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
 
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
 
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window Toolkit
 
AI_Planning.pdf
AI_Planning.pdfAI_Planning.pdf
AI_Planning.pdf
 

More from SakhilejasonMsibi (9)

Lecture 4 part 2.pdf
Lecture 4 part 2.pdfLecture 4 part 2.pdf
Lecture 4 part 2.pdf
 
Lecture 11.pdf
Lecture 11.pdfLecture 11.pdf
Lecture 11.pdf
 
Lecture2.pdf
Lecture2.pdfLecture2.pdf
Lecture2.pdf
 
Lecture3.pdf
Lecture3.pdfLecture3.pdf
Lecture3.pdf
 
Lecture 8.pdf
Lecture 8.pdfLecture 8.pdf
Lecture 8.pdf
 
Lecture 9.pdf
Lecture 9.pdfLecture 9.pdf
Lecture 9.pdf
 
Lecture 4 part 1.pdf
Lecture 4 part 1.pdfLecture 4 part 1.pdf
Lecture 4 part 1.pdf
 
Lecture 10.pdf
Lecture 10.pdfLecture 10.pdf
Lecture 10.pdf
 
Lecture1.pdf
Lecture1.pdfLecture1.pdf
Lecture1.pdf
 

Recently uploaded

Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
BalamuruganV28
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 

Recently uploaded (20)

Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Working Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdfWorking Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdf
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUUNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 

Lecture 6.pdf

  • 2. GUIS ▪ Recall from the previous lecture: create a frame and then get the content pane for that frame ▪ Now we’re going to place some stuff on the pane. ▪ Recall that the different elements on the frame are going to be objects • Frame objects • Button objects • TextField objects • Label objects • Menu objects • Checkbox objects • List objects • Radio objects
  • 3. GUIS Recall from the last lecture: ▪ Many of the GUI components are from the javax.swing package ▪ Also java.awt package
  • 4. GUIS ▪ Generally, these objects have methods that we can use to interact with them: – A.add(B) – adds component B to component A – A.setSize(width, height) – A.setLayout(…) – A.setVisible(…) ▪ Will see examples of this in the next lecture ▪ This lecture will focus on placement.
  • 6. Adding objects to frames Object placement ▪ Components on our frames are objects created from different classes – e.g. button objects, checkbox objects etc. ▪ We can place them on different parts of the frame, for now, placing them on content pane
  • 7. Adding objects to frames ▪ Recall from previous lecture, the part of the frame that we put stuff on is called the content pane – everything but the title, menu bars and border. ▪ When we add stuff to our frame, we actually add it to the content pane Frame Content pane Buttons
  • 8. Adding objects to frames Object placement ▪ So we need to get hold of the content pane before we can add stuff to it: Container cp = jf.getContentPane(); ▪ Once we have something to add to the content pane… okButton = //Code for creating buttons – see later ▪ … we use the content pane’s add() method. cp.add(okButton);
  • 10. Object Placement ▪ We need a way to determine where objects are placed ▪ Two ways: – Layout manager – Absolute positioning (doesn’t use a Layout Manager) ▪ Either way, done through the setLayout(…) method.
  • 11. Object Placement ▪ Have to decide what we giving a Layout Manager e.g. content pane. ▪ If it is the content pane, we get need get it: Container cp = jf.getContentPane();
  • 12. Object Placement ▪ Absolute positioning: each GUI object’s position is specified explicitly. ▪ Use the setLayout() method of the content pane object. Container cp = jf.getContentPane(); ... cp.setLayout(null); ▪ Then, when objects are added, need to specify their size and position – will see later.
  • 13. Object Placement Layout manager: object that controls the placement of GUI objects. 1 2 3 4 5 6 NORTH SOUTH WEST EAST CENTER
  • 14. GUIS – to be omitted Example: ▪ FlowLayout puts the objects left to right, top to bottom ▪ GridLayout puts the objects in a grid ▪ BorderLayout, CardLayout… 1 2 3 4 5 6 NORTH SOUTH WEST EAST CENTER
  • 15. Object Placement Setting the Layout Manager: ▪ Again, assume we are giving the content pane a Layout Manager Container cp = jf.getContentPane(); ▪ Then call the setLayout(…) method and pass it an instance of the layout class FlowLayout LM = new FlowLayout(); cp.setLayout(LM);
  • 16. Object Placement ▪ Alternatively… cp.setLayout(new FlowLayout()); ▪ Or, if we want to be really fancy: jf.getContentPane().setLayout(new FlowLayout()); ▪ If we want to use a different layout manager: cp.setLayout(new BorderLayout()); cp.setLayout(new GridLayout());
  • 18. GUIs ‐ FlowLayout ▪ FlowLayout: GUI components are placed left to right, top to bottom.
  • 19. GUIs ‐ FlowLayout ▪ We can specify some options when we create layout manager objects by passing class constants to the constructor: ▪ Recall: class constants are a way to give meaningful names to certain values. ▪ So when creating a new FlowLayout object, we can pass it a class constant from the FlowLayout class to specify e.g. centre alignment cp.setLayout(new FlowLayout(FlowLayout.CENTER)); cp.setLayout(new FlowLayout(FlowLayout.LEFT)); ▪ Default is FlowLayout.CENTER
  • 20. GUIs ‐ FlowLayout ▪ Can also specify the gap between components cp.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
  • 21. GUIs – BorderLayout ▪ BorderLayout: GUI components are arranged in five regions: North, South, East, West and Centre. ▪ Call the setLayout() method but pass it a BorderLayout object: cp.setLayout(new BorderLayout()); NORTH SOUTH WEST EAST CENTER • Can also change the gaps: cp.setLayout(new BorderLayout(5, 10));
  • 22. GUIs – BorderLayout ▪ BorderLayout: ▪ North, South, East and West regions take as much space as they need, Centre region takes the rest. NORTH SOUTH WEST EAST CENTER • When we add components now, the second argument is the region, again specified with class constants: cp.add(okButton, BorderLayout.WEST); cp.add(cancelButton, BorderLayout.EAST);
  • 23. GUIS ‐ GridLayout ▪ GridLayout: GUI components are arranged in a grid, specified on creation ▪ Each “cell” on the grid is the same size ▪ Call the setLayout() method, pass it a GridLayout object, with no of rows and cols… cp.setLayout(new GridLayout(2, 3)); 1 2 3 4 5 6 • … or can also specify the gaps cp.setLayout(new GridLayout(2, 3, 5, 5));
  • 24. GUIS ‐ GridLayout 1 2 3 4 5 6 • Added components are then added to fill row by row: cp.add(Button1); cp.add(Button2); cp.add(Button3); ... • Also, GridBayLayout – more control
  • 26. Panels ▪ So far we have set the layout of content panes… Container cp = jf.getContentPane(); cp.setLayout(new FlowLayout()); ▪ …but now we are going to set the layout of panels ▪ Panels are JPanel objects which are like mini content panes – Can add components to panels – Set panel’s layout ▪ Enable the sectioning of the frame into different logical sections.
  • 27. Panels ▪ Content pane has a layout manager, its components are arranged accordingly. ▪ But if a component is a panel, it will have its own layout manager for the components inside of it. ▪ We can even have nested panels – panels within panels Frame Content pane Buttons Panel Panel
  • 28. Back to Layout Managers
  • 29. GUIS ‐ BoxLayout ▪ BoxLayout: like an advanced FlowLayout ▪ Components are arranged in either vertical or horizontal orientation – horizontal can be left-to-right or right-to-left
  • 30. GUIS ‐ BoxLayout ▪ Can also add invisible components to help with the spacing: ▪ Rigid area – creates a fixed space between components ▪ Box class has a class method to return a rigid area: ▪ This method takes a Dimension object – specifies the height and width in pixels Box.createRigidArea(new Dimension(10, 0))
  • 31. GUIS ‐ BoxLayout ▪ Can also add invisible components to help with the spacing: ▪ Glue – creates space as much horizontal or vertical space as necessary to shove the components to the border. ▪ Box class has a class method to return glue: Box.createHorizontalGlue(); Box.createVerticalGlue();
  • 32. GUIS ‐ BoxLayout ▪ Can also add invisible components to help with the spacing: ▪ Filler – creates a flexible space with a maximum, minimum and preferred size. ▪ Box class can be used to create Filler objects, constructor takes Dimension objects: new Box.Filler( , , ) new Dimension(10,10) new Dimension(10,20) new Dimension(10,30)
  • 33. GUIS ‐ BoxLayout ▪ So let’s put this altogether – let’s imagine we have a panel on our form that contains the buttons: ▪ Create a panel: JPanel buttonPanel = new JPanel(); ▪ Set the panel’s layout, by passing it a BoxLayout object – takes: – Object it is being applied to and the axis (top-down vs left-right) buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); OR buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
  • 34. GUIS ‐ BoxLayout ▪ So let’s put this altogether – let’s imagine we have a panel on our form that contains the buttons: ▪ Create buttons (don’t worry about this for now): JButton okButton = new JButton("OK"); JButton cancelButton = new JButton("Cancel"); ▪ Now add the buttons with some “invisible components” buttonPanel.add(Box.createHorizontalGlue()); buttonPanel.add(okButton); buttonPanel.add(Box.createRigidArea(new Dimension(10, 0))); buttonPanel.add(cancelButton);