Quick Enquiry Form

×

    EnquiryEnquire Now

    Quick Enquiry

      Java Swing: Creating graphical user interfaces (GUI) in Java

      Java GUI
      Blog Java

      Java Swing: Creating graphical user interfaces (GUI) in Java

      The GUI components are part of the Java toolkit known as Swing for Graphical User Interfaces (GUI). Java programs can use a wide range of widgets and packages to build powerful GUI components using Swing. Swing is a component of the Java Foundation Classes (JFC), an API for Java GUI programming that offers GUI. 

      The Java Swing library serves as the foundation for the more dated, platform-specific Java Abstract Widget Toolkit (AWT). You can use the straightforward GUI programming components from the library, such as buttons, textboxes, etc., rather than constructing the components from scratch. Get started with the Best Java Training in Chennai at SLA Institute to gain expertise in Swing concepts. 

      The diagram of a Swing Class

      Java Swing

      What does "Container Class" mean?

      Classes called container classes are classes that can hold other parts. Thus, we require at least one container object in order to create a Java Swing GUI. Java Swing containers are available in three different types.

      Panel: It is only a container and not a window in and of itself. The only purpose of a Panel is to arrange the window’s components.

      Frame: The window is fully functioning and contains a title and icons.

      Dialog: When a message needs to be displayed, it functions similarly to a pop-up window. It is not a completely working window, just like the frame.

      What is a GUI in Java?

      The Graphical User Interface (GUI) of Java is a straightforward tool for developing visual experiences for Java programs. It is mostly formed of graphical elements that allow the user to interact with a program, such as buttons, labels, windows, etc. A GUI is crucial in creating user-friendly interfaces for Java programs.

      An Example of a Java GUI’s Creation

      Let’s learn how to develop a GUI in Java using examples from Swings in this Java GUI tutorial. Go through our blog that contains many simple and sample Java programs.

      Step 1: Paste the code into an editor.

      Copy the following code into an editor as a first step.

      import javax.swing.*;

      class gui{

          public static void main(String args[]){

             JFrame frame1 = new JFrame(“My First GUI”);

             frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

             frame1.setSize(300,300);

             JButton button = new JButton(“Press”);

             frame1.getContentPane().add(button); // Adds Button to content pane of frame

             frame1.setVisible(true);

          }

      }

      Step 2: Execute the code

      The code must then be saved, compiled, and executed.

      Step 3: Paste the code below into an editor.

      Add a button to our frame now. Copy the following code from the provided Java UI Example into an editor.

      import javax.swing.*;

         class gui{

            public static void main(String args[]){

              JFrame frame1 = new JFrame(“My First GUI”);

              frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frame1.setSize(300,300);

             JButton button1 = new JButton(“Press”);

             frame1.getContentPane().add(button1);

             frame1.setVisible(true);

           }

      }

      Step 4: Run the program.

      “Paraphrase” is a code. You’ll receive a sizable button.

      Java Swing

      Step 5: Provide two buttons.

      Consider including two buttons. Into an editor, copy the code below.

      import javax.swing.*;

      class gui{

            public static void main(String args[]){

                 JFrame frame1 = new JFrame(“My First GUI”);

                 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 frame1.setSize(300,300);

                JButton button1 = new JButton(“Button 1”);

                JButton button2 = new JButton(“Button 2”);

                frame1.getContentPane().add(button1);

                frame1.getContentPane().add(button2);

                frame1.setVisible(true);

           }

      }

       

      Step 6: Save the application and run it.

      Save, compile, and run the program after that.

      Step 7: Verify the results

      Unanticipated results =? Overlapping buttons are happening.

      Layout Manager in Java

      The layout manager is used to arrange the GUI Java components inside a container. Know the importance of learning Java through our recent blog. There are more layout managers, but these are the most widely used ones:

      BorderLayout in Java

      A BorderLayout allows for the placement of components in the top, bottom, left, right, and center of the layout. Java JFrame is every java JFrame manager.

      Java Swing

      FlowLayout in Java

      FlowLayout is the default layout manager used by all JPanels. It merely organizes the components in a single row, sequentially.

      Java Swing

      GridBagLayout in Java

      That is the most sophisticated layout of all. When components are arranged within a grid of cells, they can cover a wide range of cells and yet be correctly aligned.

      SLA Images

      Step 8: Build a conversation frame

      Why not try making a chat frame like the one below?

      SLA Images

      Sample Code to Try

      import javax.swing.*;

      import java.awt.*;

      class gui {

          public static void main(String args[]) {

              //Creating the Frame

              JFrame frame1 = new JFrame(“Chat Frame”);

              frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frame1.setSize(400, 400);

              //Creating the MenuBar and adding components

              JMenuBar mb = new JMenuBar();

              JMenu m1 = new JMenu(“FILE”);

              JMenu m2 = new JMenu(“Help”);

              mb.add(m1);

              mb.add(m2);

              JMenuItem m11 = new JMenuItem(“Open”);

              JMenuItem m22 = new JMenuItem(“Save as”);

              m1.add(m11);

              m1.add(m22);

              //Creating the panel at bottom and adding components

              JPanel panel = new JPanel(); // the panel is not visible in output

              JLabel label = new JLabel(“Enter Text”);

              JTextField tf = new JTextField(10); 

              JButton send = new JButton(“Send”);

              JButton reset = new JButton(“Reset”);

              panel.add(label); 

              panel.add(tf);

              panel.add(send);

              panel.add(reset);

              // Text Area at the Center

              JTextArea ta = new JTextArea();

              frame1.getContentPane().add(BorderLayout.SOUTH, panel);

              frame1.getContentPane().add(BorderLayout.NORTH, mb);

              frame1.getContentPane().add(BorderLayout.CENTER, ta);

              frame1.setVisible(true);

          }

      }

      Bottom Line

      Java’s Swing is a simple GUI toolkit with a large selection of widgets for creating efficient window-based applications. It belongs to the JFC ( Java Foundation Classes). It is entirely constructed using the AWT API and written in Java. It has lightweight components and is platform neutral, in contrast to AWT. Gain expertise with GUI concepts using Java Swing by enrolling in our Java Course in Chennai at SLA Institute.

      For Online & Offline Training

      Have Queries? Ask our Experts

      +91 88707 67784 Available 24x7 for your queries

      Quick Enquiry Form

        1
        Softlogic-Academy