You are on page 1of 3

GUI input and output:

The simplest way to create a graphical interface in java is using the “option pane” pop up.

JOptionPane can be used in three major ways: to display a message, to present a list of choice
to the user and to ask to the user to type input. (ShowMessageDialog, showConfirmDialog,

One limitation one showConfirmDialog always returns the user’s input as a String.

We must convert the String using Integer.parseInt or Double.parseDouble method.

Integer.parseInt and Double.parseDouble throw exceptions of type NumberFormatException

if you pass strings that cannot be converted into valid numbers, such as “abc” or “2x2”. To
make your code robust against invalid input, you can enclose the code in a try/catch

An onscreen window is called a frame. The graphical widgets inside a frame, such as buttons
or text inputs fields are collectively called components. The frame forms the physical windows
that are seen on the screen.

Frames are represented as objects of the JFrame class. You can display it on the screen by
calling its setVisible method and passing it Boolean value true.

Some of java graphical components.

A frame’s proprieties control features like the size of the windows, you can set or examine
these properties by calling methods on the frame.

Buttons, Text Fields and labels:

Our empty frames are not very interesting. Buttons are represented by the JButton class. Each
JButton object represent one button on the screen.

A java object that determines the positions, sizes, and resizing behavior of the components
within a frame or other container on the screen are called Layout Manager. To position the
components in a diferent way, we must set a new layout manager for the frame and use it to
position the components.

The default type of layout manager is called BorderLayout.

Handling an event:

In order to create useful interactive GUI’s, you must learn how to handle Java events. Java’s
GUI system creates a special kind of object called event to represent this action.


An object that represents a user’s interaction with a GUI component and that can be handled
by your programs to create interactive components. By default, if you don’t specify how to
react to an event, it goes unnoticed by your program.

You can cause the program to respond to a particular event by using an object called a listener.

Listener: An object that is notified when an event occurs and attach it to the component of

To handle an event, create a listener object and attach it to the component of interest. The
listener object contains the code you want to run when the appropriate event occurs.

The interface for handling action events in Java is called ActionListener.

A panel is a container is an onscreen container component that acts solely as a container for
other components and generally cannot be seen. A panel has its own layout manager that
controls position and sizes of any component.

Panels are implemented as objects of the Panel class.

You can create a JPanel with no parameters or can specify the layout manager to use. Once
you’ve constructed the panel, you can add components to it using its add method.
A common strategy is to use a BorderLayout for the frame, then add panels to some or all its
five regions.

We can put a BorderLayou in the Center and a GridLayout in the South for example.
(Telephone and MailMessage example).

Interaction Between Components:

BMI GUI example, in this program we want to calculate the Body Mass Index.