Swing In Java

GUI: Swing In Java

Introduction: 

GUI stands for Graphical User Interface. We interact with the screen in two ways one is console-based and the second one is a Graphical user interface. In console based we interact with help of text command eg. command prompt whereas in GUI we interact with the help of graphical components which is also known as visual indicators eg. Calculator.

For Graphical User Interface there are two types of Application Programming Interface (API’s) and they are AWT (Abstract Windowing Toolkit) and Swing.

Swing In Java:

Swing in Java Java provides a rich set of libraries to create a Graphical User Interface. so, its API is used to make GUI applications. therefore, it is extending from the AWT package.

As swing is a child of AWT Swing provides functions that are in AWT with or without some modification and also some new functions.

For importing all classes of swing packages we use extension  javax. swing.*.

In the applet, we can not make the main() function because the applet is embedded in the browser(HTML) to run. Whereas swing makes a java standalone application program that can run on a computer. Swing can run independently so therefore we make a main() function in a swing.

Swing features: 

however, Swing has mainly four features and they are as follows;

  1. Light Weight: Swing components are however not dependent on the operating system.
  2. Rich Controls: Swing also gives a set of advanced controls like Tree, color picker, and table controls.
  3. Highly Customizable: Swing can be easily customized according to our needs.
  4. Pluggable look-and-feel: Look and feel of GUI applications that are based on a swing can be easily at the run time.

Swing classes:

Swing contains many classes. however, The class name starts with capital ‘J’ and the class name follows pascal case notation means the first letter of every word is capitalized and the rest is small.

 The following are some classes of swing:-
  1. JFrame: JFrame is a top-level window or container which contains all components with a title and a border.
  2. JButton: This creates a button for the user side to make some interactions with the GUI application.
  3. JLabel: A JLabel is a component for taking and displaying a text in a container.
  4. JCheckBox: A JCheckBox is a graphical component that acts like a boolean if it is checked then the value is true if not then false.
  5. JRadioButton: The JRadioButton class is the same as a check box like a true or false state.
  6. JTextField: A JTextField provides a text component that gives a single line of text.
  7. JPasswordField: A JPasswordField provides a text component specialized only for password entry.
Example:

In this example, we will see how to create a graphical user interface which is created with the help of swing and that will give us multiplication of two numbers. Let us divide this problem into two steps. In the first step, we will create a basic structure layout to set the element and in the second step, we will do event handling in a swing. 

Step1: This step is further divided into five substeps:
  1. The basic structure of a swing: We create a container with the help of JFrame.
  2. Creating component reference variables: In java create reference variables before creating an object. l1, l2, l3, and l4 are the reference variables of the Label and similarly t1 and t2 for Textfeild and b1 for a button.
  3. Initialize components: Here components are initialized in setComponents() method.
  4. Adding components to the swing: for adding components in the swing frame/container we use the add() function.
  5. Setting layout: If we set the setting layout to null then we have to design the setting layout then components are arranged according to our setting layout. We can do the layout with the help of the setLayout() function. 

  b1.setLayout(left,top,width,height);

b1 from the left side, topside, a width of b1, and height of b1 in  pixels.

Output:

Step2: In this step, we make our swing responsive with help of event handling. We create our method called ActionListener() where we can do our event and our event is to multiply two numbers. In the following code, we can do the multiplication of two numbers.

Output:  

Advantages:

  • Swing provides an ease to the developer’s life to create JAVA based front end GUI applications.
  • As swing is lightweight therefore processing is fast.

Disadvantages:

  • Swing can be slower than AWT (all components are drawn) as if we’re not careful in programming.
  • It requires Java 1.2 or a separate JAR file.

Conclusion:

Swing in Java is thus used to make GUI applications as we created using an abstract window toolkit but swing provides some additional features and some modified features of abstract window toolkit(AWT) as the swing is a child of AWT.  however, Processing of swing is faster and it is a stand- alone program.  

written by: Yash Zadap

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *