8/3/2019 Session 3 - Layout Manager and Swing Menu Component
1/23
Session 3
Layout Managers
Swing Menu Component
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
2/23
Slide 2/23
Introduce to advance swing control
Jslider
JProgessBar
Timer
Introduce to Dialog
Type of Dialog
Working with Dialog
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
3/23
Slide 3/23
Module 1:
Introduction to Layout Manager
FlowLayout Manager
BorderLayout Manager GridLayoutManager
CardLayout Manager
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
4/23
Slide 4/23
All Containers in Java whether top-level or general-purpose
are based on the concept of Layout managers
A layout manager is a Java object associated with a container
which governs the placement and size of the component
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
5/23
Slide 5/23
Steps Invoke setLayout()
Pass a layout manager class
setPreferredSize()
setM
inimumS
ize() setMaximumSize()
Working of a Layout Manager
When GUI-based application in Java is executed, the pack() methodis invoked. The pack() method invokes the getPreferredSize()
method of the frame to determine the best size for laying out thecomponent
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
6/23
Slide 6/23
Java supports different types of Layout manager are:
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
7/23
Slide 7/23
Benefits and Drawbacks
Simple to use
Components placed in the center of the container by default
Suitable for JApplet which runs as part of a Web Page.
If the Container is resized of looses the original alignment, some
component may go up or down depending upon the width
Complex alignment cannot be achieved
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
8/23
Slide 8/23
The BorderLayout manager allows to add components in the east,
west, north, south and center of a container. Benefits and Drawbacks
Allows to specify the direction where the component is to placed
By default the component is placed in the center
Component can added in only four to five location in a container
Absolute positioning of component is not possible
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
9/23
Slide 9/23
The GridLayout Manager places the
components in terms of rows and columns.
Benefits and Drawbacks
Simple to use
Useful for grouping componets in a rectanglar
pattern
Each component is of same size
Cannot create a complex layout like
GridBagLayout where a cell can span more than
one row or column
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
10/23
Slide 10/23
The CardLayoutManager allows you to stack components
one behind another like deck of cards. Only one component
is visible at a time and that is the component on top. To
display other components, one has to flip them based on.
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
11/23
Slide 11/23
Some methods
first(Container parent)
next(Container parent)
previouse(Container parent)
last(Container parent)
Benefits and Drawbacks
Allows several containers and their associated component to sharethe same space in the container.
Not visually appealing as a tabbed pane
Requires other components like buttons or drop-down to flip through
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
12/23
Slide 12/23
Module 2
Menu Components
File Dialog box
JToolBar
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
13/23
Slide 13/23
A menu system provides a menu bar below the windowstitle bar. This bar contains items called a menu. When youclick a menu it drops down and display various choicescalled menu items. Each of these menu items is associatedwith actions to perform desired action. You can choose thetask by clicking a menu item
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
14/23
Slide 14/23
JMenuBar
JMenu
JMenu with Separator
JMenu with Memonic
JMenu with icon
JMenu with Accelerator
JMenu with Disable
JMenuItem
JCheckBoxMenuItem
JRadioMenuItem
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
15/23
Slide 15/23
Event Handling
actionPerformed
Methods of J
MenuItem
isSelected()
setSelected(boolean state)
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
16/23
Slide 16/23
A JPopupMenu is menu which pops up when you right click
on a container. The popup menu appears exactly at the
location where you right clicked.
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
17/23
Slide 17/23
A JFileChooser is a standard dialog box that is used to navigate
the file system to:
Accept a directory or file name to create a new one
Perform operations like open and save, with different labels and buttons.
Provide previews and display appropriate icons
Display the fiel with required criteria by applying filters.
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
18/23
Slide 18/23
Constructors
Methods of JFileChooser
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
19/23
Slide 19/23
Return value of methods
FileChooser.CANCEL_OPTION if you cancel the dialog box
JFileChooser.APPROVE_OPTION if you acceptthe file
JFileChooser.ERROR_OPTION if an error occurs or the dialog is
dismissed.
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
20/23
Slide 20/23
The JFileChooser by default displays all the files in the
current directory. However, at times it is requires to display
only those files which match certain criteria.
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
21/23
Slide 21/23
JToolBar is similar to a menu bar and contains buttons with
icons. These buttons generate events when clicked and can
be associated with actions.
Dockable JToolBar
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
22/23
Slide 22/23
Constructors
Demo
Demo Code
8/3/2019 Session 3 - Layout Manager and Swing Menu Component
23/23
Slide 23/23
Module 1:
Introduction to Layout Manager
FlowLayout Manager
BorderLayout Manager
GridLayoutManager
CardLayout Manager
Module 2:
Menu Components
File Dialog box
JToolBar