0% found this document useful (0 votes)
43 views21 pages

Graphical User Interface: Layout, Look and Feel, Introduction To Eventhandling

The document discusses graphical user interfaces and event handling in Java. It describes common Swing components like JButton, JLabel, JTextField. It also covers layout managers like FlowLayout, BorderLayout, GridLayout that control component placement and sizing. The document discusses look and feel and how to set the look and feel. It also explains the basics of event handling in Swing - how events are generated by user interactions, the concepts of source, event and listener objects, and examples of event types like ActionEvent and KeyEvent.

Uploaded by

Asfand Bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views21 pages

Graphical User Interface: Layout, Look and Feel, Introduction To Eventhandling

The document discusses graphical user interfaces and event handling in Java. It describes common Swing components like JButton, JLabel, JTextField. It also covers layout managers like FlowLayout, BorderLayout, GridLayout that control component placement and sizing. The document discusses look and feel and how to set the look and feel. It also explains the basics of event handling in Swing - how events are generated by user interactions, the concepts of source, event and listener objects, and examples of event types like ActionEvent and KeyEvent.

Uploaded by

Asfand Bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Graphical User Interface

Layout, Look and Feel, Introduction


to EventHandling
Previously
• JButton: Clickable button
• Jlabel:Line of text
• TextField: Field into which the user can type
• JTextArea : Many-row field into which user can type
• JPanel: Used for graphics; to contain other components
• JCheckBox: Checkable box with a title
• JComboBox: Menu of items, one of which can be checked
• JRadioButton: Same functionality as JCheckBox
• Container: Can contain other components
Layout Managers

• A layout manager controls placement and


sizing of components in a container
• If you do not specify a layout manager, the
container will use a Default Layout:
– JPanel : FlowLayout by default
– JFrame :BorderLayout by default
Flow Layout
• Components placed from left to right in order
added
– When a row is filled, a new row is started
• Lines can be centered, left-justified or right
justified (see FlowLayout constructor)
• Add layout as below:
– JPanel p1=new JPanel();
– p1.setLayout(new FlowLayout (FlowLayout.CENTER)); //center justified
Border Layout
• Divides container in 5 portions :
– NORTH SOUTH, EAST, WEST and CENTER.
– Also called PAGE_START, PAGE_END, LINE_START, LINE_END and
CENTER.
• The center area gets as much of the available space as possible.
• The other areas expand only as much as necessary to fill all
available space
• Specify the component's location as one of the arguments to the
add method.
– JPanel p1=new JPanel(); p1.setLayout(new BorderLayout());
– P1.add(anyComponent, BorderLayout.LINE_END)
GridLayout
• Components are placed in grid pattern
• Number of rows & columns specified in
constructor
• Grid is filled left-to-right, then top-to- bottom
– JPanel p1=new JPanel();int row=2,col=5;
– p1.setLayout(new GridLayout(row,col,gap,gap));
Box Layout
• BoxLayout either stacks its components on top
of each other or places them in a row
– JPanel p1=new JPanel();
– //contentpane, layout type X_AXIS or Y_AXIS
– p1.setLayout(new BoxLayout (p1,BoxLayout.Y_AXIS));
Combination of Layouts
• E.G:
– GridLayout for left/right sections (each section a
panel)
• GridLayout for buttons 1-6
• BorderLayout for buttons 7-10 + panel in CENTER for..
– GridLayout for buttons 11-18
Look and Feel
• The architecture of Swing is designed with
changeable "look and feel" (L&F) of your
application's GUI
• "Look" refers to the appearance of GUI
widgets (more formally, JComponents) and
"feel" refers to the way the widgets behave.
Syntax
try {

UIManager.setLookAndFeel(UIManager.getSys
temLookAndFeelClassName());
//where name of look and feel is highlighted
}
catch (Exception e) {
e.printStackTrace();}
Built in Look and Feel
• UIManager.getCrossPlatformLookAndFeelClassName()
– Returns the look and feel that works on all platforms — the Java look and feel also
called metal.
• UIManager.getSystemLookAndFeelClassName()
– Specifies the look and feel for the current platform.
• "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
– Specifies the GTK+ look and feel
• "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
– Specifies the Windows look and feel.
• "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
– This look and feel can be used on any platform.

• Note: The GTK+ L&F will only run on UNIX or Linux systems with GTK+ 2.2 or later
installed, while the Windows L&F runs only on Windows systems. Like the Java (Metal)
L&F, the Motif L&F will run on any platform.
Third party look and feel
• Add package.
– Download the .jar file
– Go to project properties by right clicking on project.
– Then click on Libraries tab, You will see Compile, Run, Compile
Tests, Run Tests tabs.
– Click on Compile tab (the first tab, selected by default)
– Click on Add JAR/Folder button at right
– Then browse and select the jar file(not the folder) you want to
include. Included jar file will show on the following box of Compile
tab.
– Click on OK button.
– Finished.
Event Handling

• Events are generated when user do some


actions with the components (button click)
• Event handling are same for Swing and AWT
Source, Event and Listener Objects
• Three kinds of objects are involved in the event-handling:
• Source object : A source fires an event when triggered.
– E.g. clicking a source Button fires an ActionEvent object, clicking a
mouse button fires MouseEvent object, typing a key fires KeyEvent
object, and etc.
• Event object: The event object fired by the source is sent to
all its listener(s), and invoke an appropriate event handler of
the listener(s).
• The listener(s) : Registered with the source to activate on the
creation of event object.
• In other words, the listener(s) "subscribes" to a source's
event, and the source "publishes" the event to all its
subscribers upon activation.
Event Objects
• There are several types of events and listeners in Java
– getSource() The object on which the Event initially occurred.
– toString() Returns a String representation of this EventObject.
• Each type of event is tied to a corresponding listener.
– E.g. A common type of event, an object of class ActionEvent is
created, when a user clicks a graphical object.
– The ActionEvent class has two methods:
• getSource(): information of source
• getActionCommand(): The specific action taken by the user
– Components registered to handle event by
• addActionListener (ActionListener al)
Example: Step 1
• There should be a pre-built Listener for
MouseEvent (here it is provided by java)
Step 2
• Define your own methods for the Listener
Step 3:Through Implemented Class

• Add Listener to the Source


Step 3: Through Anonymous class
KeyListener
• Interface for key event handling – KeyListener
– The name of the functions are
• keyTyped(KeyEvent ke)
• keyPressed(KeyEvent ke)
• keyReleased(KeyEvent ke)
– The event name is KeyEvent
• getKeyChar(), getKeyCode()
• Example: KeyTester.java
Example Interface:
interface KeyListener{
abstract void KeyTyped(KeyEvent e);
abstract void keyPressed(KeyEvent e);
abstract void keyReleased(KeyEvent e);
}

You might also like