![Page 1: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/1.jpg)
Engr. M. Fahad Khan
Lecturer, Software Engineering Department
University of Engineering & Technology, Taxila
![Page 2: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/2.jpg)
Example:
The First Swing Program
![Page 3: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/3.jpg)
Container: JFrame
Layout: BorderLayout North
Center
Components: JLabel JButton, containing an ImageIcon
![Page 4: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/4.jpg)
1. import package
2. set up top level container (e.g. JFrame)
3. apply layout (e.g. BorderLayout)
4. add components (e.g. Label, Button)
5. REGISTER listeners
6. show it to the world !
![Page 5: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/5.jpg)
CIS 068
1. import package
2. set up top level container (e.g. JFrame)
3. apply layout (e.g. BorderLayout)
4. add components (e.g. Label, Button)
5. REGISTER listeners
6. show it to the world !
![Page 6: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/6.jpg)
• Top Level Containers• General Purpose Containers• Special Purpose Containers• Basic Controls• Uneditable Information Displays• Interactive Displays of Highly Formatted
Information
![Page 7: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/7.jpg)
Top Level Containers
Your application usually extends one of these classes !
![Page 8: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/8.jpg)
Every program with a Swing GUI must contain at least one top-level Swing container.
A top-level Swing container provides the support that Swing components need to perform their painting and event handling.
There are three top-level Swing containers: JFrame, JDialog, and (for applets) JApplet. ◦ Each JFrame object implements a single main window,
and each JDialog implements a secondary window (a window that's dependent on another window).
◦ Each JApplet object implements an applet's display area within a browser window.
![Page 9: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/9.jpg)
JDialogJframe
JDialog
![Page 10: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/10.jpg)
General Purpose Containers
![Page 11: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/11.jpg)
General Purpose Containers• typically used to collect Basic Controls
(JButton, JChoiceBox…)• Added to layout of top-level containers
JPanel
JFrame
![Page 12: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/12.jpg)
Special Purpose Containers
![Page 13: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/13.jpg)
![Page 14: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/14.jpg)
The glass pane Hidden, by default. If you make the glass
pane visible, then it's like a sheet of glass over all the other parts of the root pane.
The layered pane Serves to position its contents, which
consist of the content pane and the optional menu bar. Can also hold other components in a specified Z order.
The content pane The container of the root pane's visible
components, excluding the menu bar.
The optional menu bar The home for the root pane's container's
menus. If the container has a menu bar, you generally use the container's setMenuBar or setJMenuBar method to put the menu bar in the appropriate place.
![Page 15: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/15.jpg)
Basic Controls
![Page 16: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/16.jpg)
Basic Controls• Unlike ‘passive’ containers, controls are
the ‘active’ part of your GUI Remark: containers aren’t only ‘passive’, they are also ‘active’ sources of
events, eg. Mouse-events.
• Being the visible part of your interface, controls bring your application to life
• Controls are event sources !• Objects of your application register to
controls to handle the events
![Page 17: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/17.jpg)
Uneditable Information Displays
![Page 18: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/18.jpg)
Interactive Displays of Highly Formatted Information
![Page 19: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/19.jpg)
How to glue it all together:
The Layout Management
![Page 20: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/20.jpg)
• The process of determining the size and position of components
• A layout manager is an object that performs layout management for the components within the container.
• Layout managers have the final say on the size and position of components added to a container
• Using the add method to put a component in a container, you must ALWAYS take the container's layout manager into account
![Page 21: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/21.jpg)
Java supplies five commonly used layout managers:
1. BorderLayout 2. BoxLayout 3. FlowLayout 4. GridBagLayout5. GridLayout
![Page 22: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/22.jpg)
BorderLayout
Position must be specified, e.g. add (“North”, myComponent)
![Page 23: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/23.jpg)
BoxLayout
The BoxLayout class puts components in a single row or column.
![Page 24: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/24.jpg)
FlowLayout
FlowLayout is the default layout manager for every JPanel.It simply lays out components from left to right, starting newrows if necessary
![Page 25: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/25.jpg)
GridBagLayout
GridBagLayout is the most sophisticated, flexible layout manager theJava platform provides.
![Page 26: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/26.jpg)
GridLayout
GridLayout simply makes a bunch of components equal in size anddisplays them in the requested number of rows and columns .
![Page 27: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/27.jpg)
Examples:
• Using a JButton• Using a JSlider• Using a JCheckBox
![Page 28: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/28.jpg)
Some Constructors:
JButton() Creates a button with no text or icon
JButton(Icon icon)
Creates a button with an icon
JButton(String text)
Creates a button with text
JButton(String text, Icon icon)
Creates a button with initial text and an icon
![Page 29: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/29.jpg)
Some Methods:
addActionListener(ActionListener a)
Registers ActionListener to JButtonInherited from AbstractButton
setFont(Font font) Specifies Font (Type, Style, Size)Inherited from JComponent
setBackground(
Color color)
Sets background colorInherited from JComponent
setActionCommand(String text)
Used to specify button if listener is registered to multiple buttons (see ActionEvent.getActionCommand())
![Page 30: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/30.jpg)
Some Constructors:
JSlider() Creates a horizontal slider with the range 0 to 100 and an initial value of 50
JSlider(int min, int max,
int value)
Creates a horizontal slider using the specified min, max and value.
JSlider(Int orientation
int min, int max,
int value)
Creates a slider with the specified orientation and the specified minimum, maximum, and initial values.
![Page 31: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/31.jpg)
Some Methods:
addChangeListener(ChangeListener cl)
Registers ChangeListener to slider
int getValue() Returns the slider’s value
setValue(int value) Sets the slider’s value
![Page 32: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/32.jpg)
Some Constructors:
JCheckBox() Creates an initially unselected check box button with no text, no icon.
JCheckBox(
String text)
Creates an initially unselected check box with text.
JCheckBox(String text, Icon icon, boolean selected)
Creates a check box with text and icon, and specifies whether or not it is initially selected.
![Page 33: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/33.jpg)
Some Methods:
addItemListener(ItemListener il)
Registers ItemListener to checkbox
Inherited from AbstractButton
setSelected(
boolean select)
Sets the state of checkbox
Inherited from AbstractButton
boolean getSeleted()
Gets the state of checkbox.calling method often saves from registering to the checkbox !
![Page 34: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/34.jpg)
Every top-level container indirectly contains an intermediate container known as a content pane.
the content pane contains, directly or indirectly, all of the visible components in the window's GUI.
The big exception to the rule is that if the top-level container has a menu bar, then by convention the menu bar goes in a special place outside of the content pane.
![Page 35: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/35.jpg)
Here is the code that adds the button and label to the panel, and the panel to the content pane
![Page 36: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/36.jpg)
Questions?
Question in my mind is ?
Should I ask this ?
hmmmmmmmmm?
Sorry I was sleeping sir !
![Page 37: Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f2a5503460f94c44ee0/html5/thumbnails/37.jpg)
If you have any query please feel free to ask
Phone: +92-51-9047-592
Fax: +92-51-9047-420
Email: [email protected]
University Of Engineering & Technology Taxila Pakistan