In early versions of Java, there was a GUI (Graphical User Interface) measure using AWT (Abstract Windowing Toolkit). However, it was quickly replaced with Swing, part of the JFC (Java Foundation Classes) for a couple of reasons.
Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.
Java AWT | Java Swing |
---|---|
AWT components are platform-dependent because they are heavyweight. This means that it relies on native source code for that platform. | Java swing components are platform-independent, because Swing components are lightweight – meaning they are pure Java code, and don’t have dependencies with the native platform. |
AWT doesn’t support pluggable look and feel. | Swing supports pluggable look and feel. The means it is easy to change the look of your interface, and be consistent between different operating systems. |
AWT provides less components than Swing. | Swing provides both more, and more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc. |
AWT doesn’t follows MVC (Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view. | Swing follows MVC design pattern. |
Here is the diagram of the hierarchy of Java Swing components. If you’re familiar with languages and APIs like Dart/Flutter, you’ll notice how there are a variety of component types which organize your classes, compared to Flutter’s “everything is a widget” mentality. There are pros and cons to both approaches.
While everything extends Container
at one point or another, you can see some clear differences. However, there are some common methods which Container
provides which will make it easy to build GUI apps.
add(Component c)
– allows you to add a child component to another component.
setSize(int width,int height)
– allows you to set the size of the component.
setLayout(LayoutManager m)
– define the layout manager
setVisible(boolean b)
– display/hide the componet
Know that most applications will start with a Window, which will then potentially contain one or more panels. With there you will utilize layouts to display JComponents of the required types.
Let’s now look at some sample code of how to build a Quick Sample of Swing GUI in Java.
An Introduction to GUI Design in Java was originally found on Access 2 Learn