Now-a-days, no one just wants to do work on command prompt for getting plain textual output as plain text is not much attractive neither user-friendly nor easy to understand. Graphical environment gives a perfect interface to interact with user for quick and proper response. Graphical Environment has greater impact and nice impression on the user.
Unlike C and C++, Java supports and provides complete support to graphical interfaces. You can use images as buttons, or background for your software designed in Java, so how would you get started with the Java GUI programming ? In this Java tutorials for beginners with examples I will show your how to created and run your first GUI program with Java JDK.
Swing GUI widget toolkit
Java provides you a complete set of GUI toolkit named “SWING” it is an Application Programming Interface (API) for creating a graphical interface in Java programming. Swing toolkit was developed to replace the Abstract Window Toolkit (AWT) which was the older version of graphics toolkit.
Swing came with a more sophisticated set of GUI components that could be used to emulate the look and feel of multiple operating systems, swing package made it possible for the softwares to have completely different visual than underlying platform.
Basic Components of Swing
JOptionPane pop ups the dialog box in which it may display a message to the user or requests input. It is not automatically available to program, we need to add this statement before program’s class header by which compiler can find the JOptionPane class.
JFrame is the container component, it holds all the other components of the swing architecture. It initially creates an invisible frame.
A display area for a short text string or an image, or both. A label does not react to input events. As a result, it cannot get the keyboard focus. A label can, however, display a keyboard alternative as a convenience for a nearby component that has a keyboard alternative but can’t display it.
A JLabel object can display either text, an image, or both.
A button is a highly required component of any software, it can be either Submit button, clear button, menu button etc.
JButton is an implementing of a “push” button. Buttons can be configured, and to some degree controlled, by Actions. Using an Action with a button has many benefits beyond directly configuring a button.
Basic Types of Output/Dialogue Boxes
In Java programming, there are two basic types of dialog boxes are available, that are:
- Message Box
- Input Box
It is used to show a message or alert a notification, such as errors or success messages.
An input box will request for the input from the user, it displays a prompt window and then waits for the user to input text or click a button, and then returns a value of the input box.
Taking input from the pop up box is simple and quick.
Sentences written in double quotes (” “) are arguments and they are just to be displayed on screen.
Java Swing Examples, First:
Here is the program to implement the above functions together to get a proper interface, it will just ask for 2 inputs from the user and then show the sum of them as output:
public static void main(String args)
a=JOptionPane.showInputDialog("Enter First Number:");
b=JOptionPane.showInputDialog("Enter Second Number:");
Second Example of Java Swing Toolkit
In this program we will take 3 numbers from the user and check for the largest number and show it to the user, it is a program to check largest number in Java using GUI Swing Toolkit.
public static void main(String  arg)
s1=JOptionPane.showInputDialog("Enter First Number:");
s2=JOptionPane.showInputDialog("Enter Second Number:");
s3=JOptionPane.showInputDialog("Enter Third Number:");
if(n1>n2 && n1>n3)
JOptionPane.showMessageDialog(null, n1+" is greatest");
else if(n2>n1 && n2>n3)
JOptionPane.showMessageDialog(null, n2+" is greatest");
JOptionPane.showMessageDialog(null, n3+" is greatest");
You can run this program as you would run any other Java program, let us know if you face any issue with it.
There are many messageType arguments which can be assigned to one of these static finals, such as:
5. JOptionPane.PLAIN_MESSAGE (no icon will be used)
For example, the following code displays three different JOptionPane dialogs.
|JOptionPane.showMessageDialog (null, "EXAMPLE", "Title", JOptionPane.INFORMATION_MESSAGE);|
|JOptionPane.showMessageDialog (null, "Warning Message", "Title", JOptionPane.WARNING_MESSAGE);|
|JOptionPane.showMessageDialog (null, "Error Message", "Title", JOptionPane.ERROR_MESSAGE);|
It is very easy to build a GUI program in Java, it is not much different than command line programming, so if you have the basic knowledge of the command line, then now is the time to switch to GUI programming because that is the future. If you are new to Java programming I would highly recommend you to read What is JAVA And How To Write & Run Your First Program.
If you found this Java tutorials for beginners with examples helpful to you, don’t forget to share it with your friends and classmates.