0% found this document useful (0 votes)
4 views15 pages

Java GUI Basics and JOptionPane Guide

The document introduces Graphical User Interfaces (GUIs) in Java, explaining that GUIs provide a user-friendly visual experience through components like buttons and labels, with Swing and JavaFX being the primary libraries for creation. It discusses the complexity of GUIs, the use of JOptionPane for graphical input and output, and the limitations of JOptionPane in handling user input types. Additionally, it includes an assignment for further exploration of Java GUI history and its advantages and disadvantages.

Uploaded by

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

Java GUI Basics and JOptionPane Guide

The document introduces Graphical User Interfaces (GUIs) in Java, explaining that GUIs provide a user-friendly visual experience through components like buttons and labels, with Swing and JavaFX being the primary libraries for creation. It discusses the complexity of GUIs, the use of JOptionPane for graphical input and output, and the limitations of JOptionPane in handling user input types. Additionally, it includes an assignment for further exploration of Java GUI history and its advantages and disadvantages.

Uploaded by

Astraea
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Java

Graphical User
Interfaces
What is GUI?
-GUI stands for Graphical User Interface

- is a user-friendly visual experience builder for Java applications.

-It comprises graphical units like buttons, labels, windows, etc. via which
users can connect with an application.

-Swing and JavaFX are two commonly used applications to create GUIs in
Java.
GUI Basics
GUIs are potentially very complex entities because they
involve a large number of interacting objects and classes.
Each onscreen component and window is represented by
an object, so a programmer starting out with GUIs must
learn many new class, method, and package names.
GUI Basics
In addition, if the GUI is to perform sophisticated tasks,
the objects must interact with each other and call each
other’s methods, which raises tricky communication and
scoping issues.
Graphical Input and Output with Option Panes
The simplest way to create a graphical window in Java is to have an option pane
pop up. An option pane is a simple message box that appears on the screen and
presents a message or a request for input to the user.

The Java class used to show option panes is called JOptionPane. JOptionPane
belongs to the [Link] package, so you’ll need to import this package to use it.
(“Swing” is the name of one of Java’s GUI libraries.) Note that the package name
starts with javax this time, not java. The x is because, in Java’s early days, Swing
was an extension to Java’s feature set
Graphical Input and Output with Option Panes
JOptionPane can be thought of as a rough graphical equivalent of
[Link] output and Scanner console input. The following program
creates a “Hello, world!” message on the screen with the use of JOptionPane:
Graphical Input and Output with Option Panes
The program produces the following graphical “output”
Graphical Input and Output with Option Panes

The preceding program uses a static method in the JOptionPane class called
showMessageDialog. This method accepts two parameters:
1) a parent window and
2) a message string to display
We don’t have a parent window in this case, so we passed null.
Graphical Input and Output with Option Panes

JOptionPane can be used in three major ways:


● to display a message (as just demonstrated)
● to present a list of choices to the user
● to ask the user to type input.
The three methods that implement these three behaviors are called
showMessageDialog, showConfirmDialog, and showInputDialog,
respectively.
Graphical Input and Output with Option Panes

Useful Methods of the JOptionPane Class


The following program briefly demonstrates all three types of
option panes:
The graphical input and output of this program is a series of
windows, which pop up one at a time:
Graphical Input and Output with Option Panes

One limitation of JOptionPane is that its showConfirmDialog method always


returns the user’s input as a String. If you’d like to graphically request user
input that is a number instead, your program must convert the String using the
[Link] or [Link] method. These static methods accept
a String as a parameter and return an int or double value, respectively.
ACTIVITY: Code and Run the program that demonstrates the use of
JOptionPane to read numbers from the user:
Assignment:
Provide at least 5 sentences of discussion for each of the following
requirements:

1) Java GUI History


2) What are the two commonly used applications to create GUIs in Java?
3) What are the Advantages and Disadvantages of Java GUI?

You might also like