Getting Started with Graphics Programming and JAVA Swing Examples

Graphical User Interface (GUI) Programming in Java is Very Essential

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.

Must Read: What is JAVA And How To Write & Run Your First Program

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

1. JOptionPane

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.

2. JFrame

JFrame is the container component, it holds all the other components of the swing architecture. It initially creates an invisible frame.

3. JLabel

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.

4. JButton

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

Message Box

It is used to show a message or alert a notification, such as errors or success messages.

example output message dialogue java

Input Box

example input dialogue java

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.

Also Check: JAVA Programs – 3 Ways of Assigning Value to Variables

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:

OUTPUT:

input dialogue 1

input dialogue 2

output dialogue

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.

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:

1. JOptionPane.ERROR_MESSAGE
2. JOptionPane.INFORMATION_MESSAGE
3. JOptionPane.WARNING_MESSAGE
4. JOptionPane.QUESTION_MESSAGE
5. JOptionPane.PLAIN_MESSAGE (no icon will be used)

For example, the following code displays three different JOptionPane dialogs.

example output message dialogue java JOptionPane.showMessageDialog (null, "EXAMPLE", "Title", JOptionPane.INFORMATION_MESSAGE);
Warning Message JOptionPane.showMessageDialog (null, "Warning Message", "Title", JOptionPane.WARNING_MESSAGE);
error 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.

0 Responses on Getting Started with Graphics Programming and JAVA Swing Examples|

    Pin It on Pinterest