JAVA PROGRAMMING - SWING Prof. Navya Francis Asst. Professor Kristu Jayanti College
SWING • Java Swing is a popular and powerful Graphical User Interface (GUI) toolkit that is used for developing desktop applications. • It is a part of the Java Foundation Classes (JFC) and provides a rich set of components and layout managers for creating a variety of GUIs. • Java Swing is platform-independent and can be used on any operating system that supports Java.
SWING • It provides a set of lightweight components that are not only easy to use but also customizable. • Some of the commonly used components in Swing are buttons, text fields, labels, menus, and many more. • It also provides a robust event-handling mechanism that allows developers to handle events generated by the graphical components. • Some of the commonly used layout managers in Java Swing are BorderLayout, FlowLayout, GridLayout, CardLayout, and BoxLayout.
FEATURES OF JAVA SWING • Platform Independence: Platform independence is one of Java Swing’s most remarkable features. It can run on any platform that supports Java. Thus, Swing-based applications can run on Windows, Mac, Linux, or any other Java-compatible operating system. • Lightweight Components: Java Swing provides a set of lightweight components that are easy to use and customizable. These components are designed to consume less memory and use less processing power, making Swing-based applications run efficiently.
FEATURES OF JAVA SWING • Layout Managers: Java Swing provides a set of layout managers that can be used to organize the graphical components in a GUI. These layout managers enable developers to create flexible and responsive GUIs that adapt to different screen sizes and resolutions. • Robust Event Handling Mechanism: Java Swing provides a robust event handling mechanism that allows developers to handle events generated by the graphical components. Developers can register event listeners to detect and respond to user interactions with the GUI.
FEATURES OF JAVA SWING • Pluggable Look and Feel: Java Swing provides a pluggable look and feels that allows developers to customize the appearance of the GUI according to the user’s preferences. Developers can choose from several pre-built looks and feel themes or create their own custom themes.
THE MODEL-VIEW-CONTROLLER ARCHITECTURE • Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components. Essentially, MVC breaks GUI components into three elements. • Each of these elements plays a crucial role in how the component behaves.
The MVC pattern architecture consists of three layers: • Model: It represents the business layer of application. It is an object to carry the data that can also contain the logic to update controller if data is changed. • View: It represents the presentation layer of application. It is used to visualize the data that the model contains. • Controller: It works on both the model and view. It is used to manage the flow of application, i.e. data in the model object and to update the view whenever data is changed.
• The advantages of MVC architecture are as follows: • MVC has the feature of scalability that in turn helps the growth of application. • The components are easy to maintain because there is less dependency. • A model can be reused by multiple views that provides reusability of code. • The developers can work with the three layers (Model, View, and Controller) simultaneously. • Using MVC, the application becomes more understandable. • Using MVC, each layer is maintained separately therefore we do not require to deal with massive code. • The extending and testing of application is easier. ADVANTAGES OF MVC ARCHITECTURE
SWING APPLETS • So far, we have created the applets based on AWT(Abstract Window Toolkit) by extending the Applet class of the awt package. • We can even create applets based on the Swing package. In order to create such applets, we must extend JApplet class of the swing package
JAPPLET • JApplet is a top-level container class and you should never draw on it directly, instead you should draw on the component class like JPanel and then add this JPanel to the JApplet
CREATING A SWING APPLET In the upcoming code, first, we have created a swing applet by extending JApplet class and we have added a JPanel to it. Next, we have created a class B, which has extended JPanel class of Swing package and have also implemented ActionListener interace to listen to the button click event generated when buttons added to JPanel are clicked.
JAVA Programming : Topic JAVA Programming Swing

JAVA Programming : Topic JAVA Programming Swing

  • 1.
    JAVA PROGRAMMING -SWING Prof. Navya Francis Asst. Professor Kristu Jayanti College
  • 2.
    SWING • Java Swingis a popular and powerful Graphical User Interface (GUI) toolkit that is used for developing desktop applications. • It is a part of the Java Foundation Classes (JFC) and provides a rich set of components and layout managers for creating a variety of GUIs. • Java Swing is platform-independent and can be used on any operating system that supports Java.
  • 3.
    SWING • It providesa set of lightweight components that are not only easy to use but also customizable. • Some of the commonly used components in Swing are buttons, text fields, labels, menus, and many more. • It also provides a robust event-handling mechanism that allows developers to handle events generated by the graphical components. • Some of the commonly used layout managers in Java Swing are BorderLayout, FlowLayout, GridLayout, CardLayout, and BoxLayout.
  • 4.
    FEATURES OF JAVASWING • Platform Independence: Platform independence is one of Java Swing’s most remarkable features. It can run on any platform that supports Java. Thus, Swing-based applications can run on Windows, Mac, Linux, or any other Java-compatible operating system. • Lightweight Components: Java Swing provides a set of lightweight components that are easy to use and customizable. These components are designed to consume less memory and use less processing power, making Swing-based applications run efficiently.
  • 5.
    FEATURES OF JAVASWING • Layout Managers: Java Swing provides a set of layout managers that can be used to organize the graphical components in a GUI. These layout managers enable developers to create flexible and responsive GUIs that adapt to different screen sizes and resolutions. • Robust Event Handling Mechanism: Java Swing provides a robust event handling mechanism that allows developers to handle events generated by the graphical components. Developers can register event listeners to detect and respond to user interactions with the GUI.
  • 6.
    FEATURES OF JAVASWING • Pluggable Look and Feel: Java Swing provides a pluggable look and feels that allows developers to customize the appearance of the GUI according to the user’s preferences. Developers can choose from several pre-built looks and feel themes or create their own custom themes.
  • 7.
    THE MODEL-VIEW-CONTROLLER ARCHITECTURE • Swinguses the model-view-controller architecture (MVC) as the fundamental design behind each of its components. Essentially, MVC breaks GUI components into three elements. • Each of these elements plays a crucial role in how the component behaves.
  • 8.
    The MVC patternarchitecture consists of three layers: • Model: It represents the business layer of application. It is an object to carry the data that can also contain the logic to update controller if data is changed. • View: It represents the presentation layer of application. It is used to visualize the data that the model contains. • Controller: It works on both the model and view. It is used to manage the flow of application, i.e. data in the model object and to update the view whenever data is changed.
  • 10.
    • The advantagesof MVC architecture are as follows: • MVC has the feature of scalability that in turn helps the growth of application. • The components are easy to maintain because there is less dependency. • A model can be reused by multiple views that provides reusability of code. • The developers can work with the three layers (Model, View, and Controller) simultaneously. • Using MVC, the application becomes more understandable. • Using MVC, each layer is maintained separately therefore we do not require to deal with massive code. • The extending and testing of application is easier. ADVANTAGES OF MVC ARCHITECTURE
  • 12.
    SWING APPLETS • Sofar, we have created the applets based on AWT(Abstract Window Toolkit) by extending the Applet class of the awt package. • We can even create applets based on the Swing package. In order to create such applets, we must extend JApplet class of the swing package
  • 13.
    JAPPLET • JApplet isa top-level container class and you should never draw on it directly, instead you should draw on the component class like JPanel and then add this JPanel to the JApplet
  • 14.
    CREATING A SWINGAPPLET In the upcoming code, first, we have created a swing applet by extending JApplet class and we have added a JPanel to it. Next, we have created a class B, which has extended JPanel class of Swing package and have also implemented ActionListener interace to listen to the button click event generated when buttons added to JPanel are clicked.