A Sample User Interface

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

A sample User Interface

Introduction
• Contains the classes for creating user interfaces and for painting
graphics and images .

• Java AWT and Java Swing are both part of a group of Java class
libraries called the Java Foundation Classes (JFC).

• AWT existed before swings. Swings is built on AWT.

• Few benefits of choosing Java Swings compared to Java AWT are:


• Swing components are light weight :
The look and feel and behavior of Java Swing components is
consistent across platforms but AWT components' provides
different look and feel and behavior for different platform.
• Event model is more efficient in Java Swing as compared to AWT
which means Swing components can run more quickly than their
AWT counterparts.

• All class names in the swing package begin with a capital letter ‘J’.
JFC is short for Java Foundation Classes, which encompass a group of features for building
graphical user interfaces (GUIs) and adding rich graphics functionality and interactivity to Java
applications.

The Swing API is powerful, flexible — and immense. The Swing API has 18 public
packages:

javax.accessibility javax.swing.plaf javax.swing.text

javax.swing javax.swing.plaf.basic javax.swing.text.html

javax.swing.border javax.swing.plaf.metal javax.swing.text.html.parser

javax.swing.colorchooser javax.swing.plaf.multi javax.swing.text.rtf

javax.swing.event javax.swing.plaf.synth javax.swing.tree

javax.swing.filechooser javax.swing.table javax.swing.undo


• To appear onscreen, every GUI component must be part of a
containment hierarchy. A containment hierarchy is a tree of
components that has a top-level container as its root.

• Each GUI component can be contained only once. If a


component is already in a container and you try to add it to
another container, the component will be removed from the
first container and then added to the second.

• Top level Containers:


• Swing provides three generally useful top-level container
classes: JFrame, JDialog, and JApplet.
Heirarchy
Component

Container

JComponent Window

Frame

JFrame
Component
• A component is an object having a graphical
representation that can be displayed on the screen and
that can interact with the user. Examples of components
are the buttons, checkboxes, and scrollbars of a typical
graphical user interface.

• The Component class is the abstract superclass of the


nonmenu-related Abstract Window Toolkit components.
Class Component can also be extended directly to create
a lightweight component.
• Container :
– A generic Abstract Window Toolkit(AWT) container object is a
component that can contain other AWT components.
• JComponent :
– The base class for all Swing components except top-level
containers.
• Window :
– A Window object is a top-level window with no borders and no
menubar. Directly sits on the desktop. The default layout for a
window is BorderLayout
• Frame :
– A Frame is a top-level window with a title and a border.
• JFrame :
– An extended version of java.awt.Frame that adds support for the
JFC/Swing component architecture.
– Like all other JFC/Swing top-level containers, a JFrame contains a
JRootPane as its only child.
JComponent
• The base class for all Swing components except top-
level containers.
• To use a component that inherits from JComponent,
you must place the component in a containment
hierarchy whose root is a top-level Swing container.
• Top-level Swing containers -- such as JFrame, JDialog,
and JApplet -- are specialized components that provide
a place for other Swing components to paint
themselves.
• Each top-level container relies on a reclusive
intermediate container called the root pane.
• Like all other JFC/Swing top-level containers, a
JFrame contains a JRootPane as its only child.
• A root pane provides to a frame (and to every other
top-level container):
– The layered pane which manages the menu bar and
content pane.
– layered pane : is a Swing container that provides a 3 rd
dimension for positioning components: depth/ Z order.
When adding a component to a layered pane, you specify
its depth. Frames at a higher depth always overlap
frames at a lower depth
– The glass pane which is often used to intercept input
events occurring over the top-level container, and can
also be used to paint over multiple components.
• The first thing that all Swing applications must
do is import the Swing packages
• import javax.swing.*;
• Create a frame by extending from JFrame class.
• Set the frame’s visibility to true coz its false by
default.
• setSize() method takes width and height and it is
in pixels
• Default layout of content pane is BorderLayout
Managing the layout of components
• Several AWT and Swing classes provide layout managers
for general use:
• They handle the rearranging of components when the
frame is resized.
• And also they take account and adjust for differences of
h/w and OS.
• BorderLayout
• BoxLayout
• CardLayout
• FlowLayout
• GridBagLayout
• GridLayout
• GroupLayout
• SpringLayout
BorderLayout
• Every content pane is initialized to use a
BorderLayout.
• A BorderLayout places components in up to five
areas: top, bottom, left, right, and center.
Box Layout
• The BoxLayout class puts components in a single row or column.
• It respects the components' requested maximum sizes and also lets
you align components.

BoxLayout bl = new BoxLayout(getContentPane(),BoxLayout.Y_AXIS);


setLayout(bl);
add(jlabel);
FlowLayout
• It simply lays out components in a single row,
starting a new row if its container is not
sufficiently wide.
• FlowLayout is the default layout manager for
every JPanel.
GridLayout
• GridLayout simply makes a bunch of components
equal in size and displays them in the requested
number of rows and columns.
• GridLayout experimentLayout = new GridLayout(0,2);
• Above constructor creates an instance that has two
columns and as many rows as necessary.
• When both the number of rows and the number of
columns have been set to non-zero values, the no of
columns specified is ignored.
• The number of columns is determined from the
specified number of rows and the total number of
components in the layout
Components
• JButton b1 = new JButton(“submit”);
– Where submit is the caption.
• JLabel lbl 1 = new JLabel();
• JLabel lbl1 = new Jlabel(“submit”);
• JLabel lbl1 = new JLabel(“submit”,Jlabel.LEFT);
– LEFT,RIGHT,CENTER defined in SwingsConstants
– Can have multiple constructor
• JCheckBox
• JRadioButton rb 1 = new JRadioButton(“yes”,true);
– True indicates intially selected
• ButtonGroup g 1 = new ButtonGroup();

• JTextField()
• JTextField(int cols)
• JTextField(String s, int cols)
• JTextField(String s)

• JComboBox com = new JComboBox();


• com.addItem("france");
• com.addItem("india");
• com.addItem("thailand");
• com.addItem("UK");
• JPanel : is a component that is used to store
the other components in turn.
– Default layout is FlowLayout.
• JMenu
• JTabbedPane
• JFileChooser
• JColorChooser
Event handling
• events are caused by user interaction with your
gui.
• Clicking a button is an user action and it triggers
some event.
• We have to provide a behavior for every event
that occurs.
• For doing so, we require Listeners : a code that
can detect and react to events.
Listeners
• All listeners in java are interfaces.
• Java has given listener interfaces for various kinds of
events.
• Any class containing a listener must implement the
appropriate listener interface.
• It implies that the listener has to provide definition to
empty interface methods.
• It is in these methods that the code to react to events
can be placed.
• We need to then associate that listener object with the
gui component whose events the listener wishes to
handle.
Registering a listener
Listener
Button object

• When we associate a listener to an gui object we say that we are


registering an listener.
• After a listener is registered with the object, JVM will by default call the
method of the corresponding listener interface.
• Different swing objects support different listeners.
• Example :
• ActionListener : is an listener interface which listens for ActionEvent to
occur.
• The gui component whose event the listener is supposed to handle/listen
for, needs to register this listener by calling the addActionListener()
method.
• When that event occurs on that gui object, the actionPerformed method of
this interface is called by default.
• Every time the event occurs the actionPerformed method is called
• public void actionPerformed(ActionEvent ae)
• ActionEvent object holds a reference to the object that triggered the event
• On every event of a component there is
listener.
• One listener can be for three buttons but on
the same event.
• Multiple listeners can register to be notified
of events of a particular type from a particular
source
Some other events and listeners
• WindowListener : listens for any events relate to the
window like window opened, closed, iconified etc.
• WindowEvent : is the event object created when there is
any change in the status of the window.
• WindowListener contains 7 different methods for seven
different types events occurring on a window.
– windowActivated(WindowEvent e)
– windowClosed(WindowEvent e)
– windowClosing(WindowEvent e)
– windowDeactivated(WindowEvent e)
– windowDeiconified(WindowEvent e)
– windowIconified(WindowEvent e)
– windowOpened(WindowEvent e)
    
WindowListener
• If you extend the above interface, you will have
to define(may be blank) all the seven methods of
that interface.
• To avoid this there are adapter classes for every
listener interface.
• You can use the WindowAdapter class which
already implements the WindowListener
interface for you.
Content Pane
• The top level containers provide a framework
within which the light weight components can
draw themselves.
• This framework is implemented by content pane.
• Without content pane, light weight components
such as JButton could not be drawn.
• content pane is a link to the host platform. It is a
heavy weight awt component in fact an instance
of Container, which can render itself on the host
platforms windowing system.

You might also like