SWING
SWING
• Controller takes the input from the user on the view and reflects the changes in
Component's data.
• Swing component have Model as a separate element and View and Controller part are
clubbed in User Interface elements. Due to this, Swing has pluggable look-and-feel
architecture.
Swing
• Swing implements GUI components that build on AWT technology.
• Rich controls - Swing provides a rich set of advanced controls like Tree,
TabbedPane, slider, colourpicker, table controls
• Pluggable look-and-feel- SWING based GUI Application look and feel can be
changed at run time based on available values.
Swing Java Hierarchy
What is container
• A container is a root element for an Application. All the other
components are added to that root and it forms a hierarchy.
• A container in Swing is something that holds other components.
• A container is responsible to displaying the components according
to layout.
• If a button is placed on the top left corner, every time the window
is re-sized, the button will still be on the top left corner. This is
because the container is containing the button and at the same
time containing its layout position.
What is Component ?
• Swing components are basic building blocks of an application.
• Swing has a wide range of various components,
• buttons,
• check boxes,
• sliders, and
• list boxes.
• Text Field
• Password etc
Swing Components
9
Swing Components
10
Main Steps in GUI Programming
To make any graphic program work we must be able to create
windows and add content to them.
To make this happen we must:
1. Import the awt or swing packages.
2. Set up a top-level container.
3. Fill the container with GUI components.
4. Install listeners for GUI Components.
5. Display the container.
Build from bottom up
Listener
• Create:
• Frame
• Panel JLabel JButton
• Components
• Listeners
• Add: (bottom up)
JPanel
• listeners into components
• components into panel
• panel into frame
JFrame
Code
JFrame f = new JFrame(“title”);
JPanel p = new JPanel( );
JButton b = new JButton(“press me”);
press me
f.show();
Application Code
import javax.swing.*;
class hello {
public static void main(String[] args){
JFrame f = new JFrame(“title”);
JPanel p = new JPanel();
JButton b = new JButton(“press me”);
JFrame JFrame
JPanel
containers
JPanel
JButton
JButton JLabel
JLabel
Using a GUI Component 2
1. Create it
order
2. Configure it important
3. Add children (if container)
4. Add to parent (if not JFrame)
5. Listen to it
What is Jframe and Panel ?
• Whenever we create a graphical user interface with Java Swing
functionality, we will need a container for your application.
• In the case of Swing, this container is called a JFrame. All GUI
applications require a JFrame.
• The class Panel is the simplest container class. It provides space in
which an application can attach any other component, including
other panels.
Java program for creating frame
import javax.swing.*;
public class helloswing
{
public static void main(String args[])
{
JFrame frame=new JFrame("Hello Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
}
setDefaultCloseOperation()
• JFrame.EXIT_ON_CLOSE — Exit the application.
• JFrame.HIDE_ON_CLOSE — Hide the frame, but keep the application running.
• JFrame.DISPOSE_ON_CLOSE — Dispose of the frame object, but keep the
application running.
• JFrame.DO_NOTHING_ON_CLOSE — Ignore the click.
• Methods used for creation :-
content pane
JLabel
Containers - Layout
• Each container has a layout manager
• Determines the size, location of contained widgets.
29
Containers - Layout
31
Program for Layout Manager