From the course: Learn Java with Swing

Learn about Swing - Java Tutorial

From the course: Learn Java with Swing

Start my 1-month free trial

Learn about Swing

- [Narrator] Let's learn about Swing. The Swing API or Application Programming Interface can be used to create graphical user interface applications in Java such as forms. It is often referred to as a widget toolkit for Java. The components are used with the JFC or the Java Foundation Classes. The Swing API contains an extensive list of GUI components where a component represents an item on the page such as a button, a text box, a checkbox, a label, and so on. Prior to the Swing API, Java had something called the AWT, the Abstract Windowing Toolkit. Swing components follow a model-view-controller architecture. The graphical user interface is made up of three parts. We have the user interface elements where these are the components the end user can see and interact with such as a button, a text field, a label, a menu. We have layouts. Swing provides several layouts that control the placement of your components. And then we have behavior. These are the events that occur when the user interacts with the UI element, when the user clicks a button, when they type into a text field. All Swing applications use a system of containers. The top-level container can be a JFrame, a JDialog, or even a JApplet. Once we establish our top-level container, we can build a hierarchy of components. Think of this top-level component as an empty picture frame. Before we start painting, we need to add a canvas. In this case, the canvas is what we call a content pane, usually a JPanel. We can use one pane for our entire canvas or maybe we want to create a type of collage with several panes put together to be our canvas. Remember, the frame is used to determine the size of our window, but it does not actually contain the individual components. They must be added through another panel. To start programming graphical user interface applications in Java, we have two choices. We can use an IDE that contains a WYSIWYG editor where WYSIWYG stands for What You See Is What You Get, or we can add the components directly through the code. Each option has its benefits and drawbacks, but I will say that the GUI editor makes arranging our components much, much easier. On the bad side, it does add a lot of code behind the scenes making it difficult to change if you're not familiar with each component. By using Java, you have a system that is very flexible and runs on all platforms. By adding a graphical user interface, it allows your application to interact with the end users. Maybe your application is a front-end for a data entry system, or you just want to create a front-end for a contact list. Java and Swing are here to help. Java Swing creates your GUI, your Graphical User Interface, and then the information can be stored in a database, a text file, or some other data storage.

Contents