0% found this document useful (0 votes)
21 views

SWING

Swing provides GUI components that are lightweight and platform-independent. It implements components that build on AWT. Some key aspects of Swing include: 1. Swing components are lightweight as they are rendered using Java code instead of operating system calls. 2. Swing provides a rich set of advanced controls like trees, tabbed panes, sliders. 3. Components can be highly customized as the visual appearance is independent of the internal representation.

Uploaded by

isayashpbende26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

SWING

Swing provides GUI components that are lightweight and platform-independent. It implements components that build on AWT. Some key aspects of Swing include: 1. Swing components are lightweight as they are rendered using Java code instead of operating system calls. 2. Swing provides a rich set of advanced controls like trees, tabbed panes, sliders. 3. Components can be highly customized as the visual appearance is independent of the internal representation.

Uploaded by

isayashpbende26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

SWING

AWT and Swing


• Java provides two sets of components for GUI programming:
• AWT: classes in the java.awt package
• Swing: classes in the javax.swing package
Abstract Window Toolkit (AWT)
• The Abstract Window Toolkit is a portable GUI library.
• AWT provides the connection between your application and the native GUI.
• AWT provides a high-level abstraction since it hides you from the underlying details of
the GUI your program will be running on.
• AWT components depend on native code counterparts (called peers) to handle their
functionality. Thus, these components are often called heavyweight components.
Architecture (MVC)
Swing API architecture follows loosely based MVC architecture in the following manner.

• A Model represents component's data.

• View represents visual representation of the component's data.

• 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.

• Light Weight - Swing component are independent of native Operating


System's API as Swing API controls are rendered mostly using pure JAVA code
instead of underlying operating system calls.

• Rich controls - Swing provides a rich set of advanced controls like Tree,
TabbedPane, slider, colourpicker, table controls

• Highly Customizable - Swing controls can be customized in very easy way as


visual appearance is independent of internal representation.

• 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”);

p.add(b); // add button to panel


f.setContentPane(p); // add panel to frame

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”);

p.add(b); // add button to panel


f.setContentPane(p); // add panel to frame
press me
f.show();
}
}
Anatomy ofGUIan Application GUI
Internal structure

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 :-

• frame.setSize(300,100); :- set the size of the window in pixel.


• frame.setVisible(true); :- without this we cannot see into the screen
Create Label
import javax.swing.*;
class Label
{
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.",JLabel.CENTER);
// display lable on screen
l1.setBounds(50,50, 100,30);
l2=new JLabel("Second Label.");
l2.setBounds(50,100, 100,30);
f.add(l1);
f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
Adding other Component
• Button to GUI
• JButton b=new JButton("click");//creating instance of JButton
• b.setBounds(130,100,100, 40);//x axis, y axis, width, height
• f.add(b);//adding button in JFrame
• Text Box
• t1=new JTextField("Welcome to Javatpoint.");
• t1.setBounds(50,100, 200,30);
• t2=new JTextField("AWT Tutorial");
• t2.setBounds(50,150, 200,30);
• f1.add(t1); f1.add(t2); // where f1 is object of Jframe
• setBounds(x, y, width, height) to specify the position and size of a GUI
component if you set the layout to null. Then (x, y) is the coordinate of the
upper-left corner of that component.
• TextArea:-
• JTextArea area=new JTextArea("Welcome to javatpoint");
• area.setBounds(10,30, 200,200);
Other Components
• CheckBox:-
• JCheckBox checkBox1 = new JCheckBox("C++");
• checkBox1.setBounds(100,100, 50,50);
• JCheckBox checkBox2 = new JCheckBox("Java", true);
• checkBox2.setBounds(100,150, 50,50);
• f.add(checkBox1);
• f.add(checkBox2);
• JRadioButton
• JRadioButton r1=new JRadioButton("A) Male");
• JRadioButton r2=new JRadioButton("B) Female");
• r1.setBounds(75,50,100,30);
• r2.setBounds(75,100,100,30);
• ButtonGroup bg=new ButtonGroup();
• bg.add(r1);bg.add(r2);
• f.add(r1);f.add(r2);
Other Components
• Java JComboBox:-
• String country[]={"India","Aus","U.S.A","England","Newzealand"};
• JComboBox cb=new JComboBox(country);
• cb.setBounds(50, 50,90,20);
• f.add(cb);
• JTable class is used to display data in tabular form
• String data[][]={ {"101","Amit","670000"},
• {"102","Jai","780000"},
• {"101","Sachin","700000"}};
• String column[]={"ID","NAME","SALARY"};
• JTable jt=new JTable(data,column);
• jt.setBounds(30,40,200,300);
• JScrollPane sp=new JScrollPane(jt);
Hello World Example
import javax.swing.*;
public class HelloWorldSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("HelloWorldSwing");
final JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Top-level Containers
• There are three top-level Swing containers
• JFrame: window that has decorations, such as a border, a title, and buttons
for iconifying and closing the window
• JDialog: a window that's dependent on another window
• JApplet: applet's display area within a browser window
Jframe and JDialog
• 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.
• JDialog class, which is used to create dialog windows
• It can either be used directly to create a custom dialog, or we can
invoke methods provided by JOptionPane to create many dialogs.
• Dialog windows are usually used as error messages or warnings, but
may can have images, directory trees, or just about anything
compatible with the main Swing Application that manages them
Containment Hierarchy
• In the Hello World example, there was a content pane.
• Every top-level container indirectly contains an intermediate container known as
a content pane.
• As a rule, the content pane contains, directly or indirectly, all of the visible
components in the window's GUI.
• To add a component to a container, you use one of the various forms of the add
method.
Containment Hierarchy of the Hello World
Example
JFrame

content pane

JLabel
Containers - Layout
• Each container has a layout manager
• Determines the size, location of contained widgets.

• Setting the current layout of a container:


void setLayout(LayoutManager lm)

• LayoutManager implementing classes:


– BorderLayout
– BoxLayout
– FlowLayout
– GridLayout

29
Containers - Layout

31
Program for Layout Manager

You might also like