Java Swing
Java Swing is a GUI Framework that contains a set of classes to provide more powerful and flexible GUI components than AWT. Swing provides the look and feel of modern Java GUI. Swing library is an official Java GUI tool kit released by Sun Microsystems. It is used to create graphical user interface with Java.
Swing classes are defined in javax.swing package and its sub-packages.
Features of Swing
- Platform Independent
- Customizable
- Extensible
- Configurable
- Lightweight
- Rich Controls
- Pluggable Look and Feel
Swing and JFC
JFC is an abbreviation for Java Foundation classes which encompass a group of features for building Graphical User Interfaces(GUI) and adding rich graphical functionalities and interactivity to Java applications. Java Swing is a part of Java Foundation Classes (JFC).
Features of JFC
- Swing GUI components.
- Look and Feel support.
- Java 2D.
AWT and Swing Hierarchy
Introduction to Swing Classes
JPanel : JPanel is Swing’s version of AWT class Panel and uses the same default layout, FlowLayout. JPanel is descended directly from JComponent.
JFrame : JFrame is Swing’s version of Frame and is descended directly from Frame class. The component which is added to the Frame, is refered as its Content.
JWindow : This is Swing’s version of Window and has descended directly from Window class. Like Window it uses BorderLayout by default.
JLabel : JLabel has descended from JComponent, and is used to create text labels.
JButton : JButton class provides the functioning of push button. JButton allows an icon, string or both associated with a button.
JTextField : JTextFields allow editing of a single line of text.
Creating a JFrame
There are two ways to create a JFrame Window.
- By instantiating JFrame class.
- By extending JFrame class.
Creating JFrame window by Instantiating JFrame class
Creating JFrame window by extending JFrame class
Points To Remember
- Import the javax.swing and java.awt package to use the classes and methods of Swing.
- While creating a frame (either by instantiating or extending Frame class), following two attributes are must for visibility of the frame:
- Â
- When you create objects of other components like Buttons, TextFields, etc. Then you need to add it to the frame by using the method – add(Component’s Object);
- You can add the following method also for resizing the frame – setResizable(true);