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

Swings: Made By: Chaya Malik Karan Narula

Swing provides more powerful and flexible GUI components than AWT. It includes familiar components like buttons and labels, as well as additional components like tabbed panes, scroll panes, trees and tables. Unlike AWT, Swing components are written entirely in Java and are therefore platform independent. Developing a GUI with Swing involves setting up a top level container, applying a layout, adding components, and registering listeners before displaying the GUI.

Uploaded by

Chaya Malik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Swings: Made By: Chaya Malik Karan Narula

Swing provides more powerful and flexible GUI components than AWT. It includes familiar components like buttons and labels, as well as additional components like tabbed panes, scroll panes, trees and tables. Unlike AWT, Swing components are written entirely in Java and are therefore platform independent. Developing a GUI with Swing involves setting up a top level container, applying a layout, adding components, and registering listeners before displaying the GUI.

Uploaded by

Chaya Malik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

SWINGS

Made by:
Chaya Malik
Karan Narula
Swing is a set of classes that provides more powerful and flexible
components than are possible with the AWT.

Is a part of the java foundation classes

In addition to the familiar components, such as buttons, check


boxes, and labels, Swing supplies several exciting additions,
including tabbed panes, scroll panes, trees, andtables.

Even familiar components such as buttons have more capabilities

in Swing

For example, a button may have both an image and a text string
associated with it.
Also,the image can be changed as the state of the button
changes.
Unlike AWT components, Swing components are not implemented
by platform specific code.

Instead, they are written entirely in Java and, therefore, are


platform-independent.

The term lightweight is used to describe such elements since

# they are drawn in their containers


# and don’t have an operating platform window of their own at all

so they use far fewer system resources


Fundamental to Swing is the JApplet class, which extends Applet.

Applets that use Swing must be subclasses of JApplet.

JApplet is rich with functionality that is not found in Applet.


Differences between Swing and AWT

Never mix Swing and AWT components

If you know AWT, put ‘J’ in front of everything


AWT: Button
Swing: JButton

Swing does all that AWT does, but better and there’s much more
of it.
Java Swing class hierarchy
CODING WITH SWINGS
At the start of your code - always
import javax.swing;
import javax.swing.event;
Most Swing programs also need
import java.awt.*;
import java.awt.event.*;
STEPS TO BUILD A GUI
1.import package

2.set up top level container


(e.g. JFrame)

3.apply layout

4.add components
(e.g. Label, Button)

5.REGISTER listeners

6.show it to the world !


Container: JFrame

Layout: BorderLayout

Components: JLabel Jbutton,containing


an ImageIcon
The Source
. import package

2. set up top level container


(e.g. JFrame)

3. apply layout
(e.g. BorderLayout)

4. add components
(e.g. Label, Button)

5. REGISTER listeners

show it to the world !


Containment Hierarchy

Top-level container:
place for other Swing components to paint themselves
e.g., JFrame, JDialog, Japplet
Intermediate container:
simplify positioning of atomic components
e.g., JPanel, JSplitPane, JTabbedPane
Atomic components:
-self-sufficient components that present information to and get input
from the user
-e.g., JButton, JLabel, JComboBox, JTextField, JTable
• Unlike ‘passive’ containers, controls are the ‘active’ part of your
GUI
• Controls are event sources !

• Objects of your application register to controls to


handle the events
Using a GUI Component
1. Create it
 Instantiate object: b = new JButton(“press me”);
2. Configure it
 Properties: b.text = “press me”; [avoided in java]
 Methods: b.setText(“press me”);
3. Add it
 panel.add(b);
4. Listen to it
 Events: Listeners
ANATOMY OF AN APPLICATION GUI

GUI Internal structure

JFrame

JPanel
containers
JPanel

JButton
JButton JLabel

JLabel
USING A GUI COMPONENT 2

1. Create it
2. Configure it
order
3. Add children (if container)
important
4. Add to parent (if not JFrame)
5. Listen to it
BUILD FROM BOTTOM UP

 Create: Listener
 Frame
 Panel

 Components

 Listeners JLabel JButton

 Add: (bottom up)


 listeners into components
 components into panel

 panel into frame


JPanel

JFrame
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(“pressme”);
p.add(b);// add button to panel
f.setContentPane(p);
// add panel to frame
f.show();
}
}

press me
Layout Manager Heuristics
Null Flow Layout Grid Layout

none,
Left to right,
programmer
Top to bottom
sets x,y,w,h

Border Layout Card Layout Grid Bag Layout

w c e One at a time JButton

s
COMPONENTS
Japplet

Fundamental to Swing is the JApplet class, which extends Applet.


Applets that use Swing must be subclasses of JApplet. JApplet is rich with
functionality that is not found in Applet.
For example, JApplet supports various “panes,” such as the content pane,
the glass pane, and the root pane.
One difference between Applet and JApplet is when adding a component
to an instance of JApplet, do not invoke the add( ) method of the applet.
Instead, call add( ) for the content pane of the JApplet object. The content
pane can be obtained via the method :
Container getContentPane( )
The add( ) method of Container can be used to add a component to a
content pane. Its form is:
void add(comp)
Here, comp is the component to be added to the content pane.
Icons
In Swing, icons are encapsulated by the ImageIcon class,
which paints an icon from an image.

Two of its constructors are shown here:


i. ImageIcon(String filename)
ii. ImageIcon(URL url)

The first form uses the image in the file named filename.
The second form uses the image in the resource identified by
url.
The ImageIcon class implements the Icon interface that
declares the methodsshown here:

Method Description
int getIconHeight( ) Returns the height of the icon in pixels.

int getIconWidth( ) Returns the width of the icon in pixels.

void paintIcon Paints the icon at position x, y on


(Component comp, the graphics context g. Additional
Graphics g,int x, int y) information about the paint
operation can be provided in comp
Labels
Swing labels are instances of the JLabel class, which extends JComponent.
It can display text and/or an icon.

Some of its constructors are shown here:


JLabel(Icon i)
Label(String s)
JLabel(String s, Icon i, int align)
Here, s and i are the text and icon used for the label.
The align argument is either LEFT,RIGHT, CENTER, LEADING, or
TRAILING.
These constants are defined in the SwingConstants interface, along with
several others used by the Swing classes.

The icon and text associated with the label can be read and written by the
following methods:
Icon getIcon( )
String getText( )
void setIcon(Icon i)
void setText(String s)
Here, i and s are the icon and text, respectively
Text Fields
The Swing text field is encapsulated by the JTextComponent class, which
extends JComponent.

It provides functionality that is common to Swing text components. One


of its subclasses is JTextField, which allows you to edit one line of text.

Some of its constructors are shown here:


JTextField( )
JTextField(int cols)
JTextField(String s, int cols)
JTextField(String s)
Here, s is the string to be presented, and cols is the number of columns in the text
field.
Buttons
 Swing buttons are subclasses of the AbstractButton class,
which extends JComponent.
 The following are the methods that control this behavior:
 void setDisabledIcon(Icon di)
 void setPressedIcon(Icon pi)
 void setSelectedIcon(Icon si)
 void setRolloverIcon(Icon ri)

The JButton Class

• The JButton class provides the functionality of a push button.


• JButton allows an icon, a string, or both to be associated with the push
button.
•Some of its constructors are:
JButton(Icon i)
JButton(String s)
JButton(String s, Icon i)
Check Boxes
The JCheckBox class, which provides the functionality of a
check box, is a concrete implementation of AbstractButton.
Its immediate superclass is JToggleButton, which provides
support for two-state buttons.
Some of its constructors are :
JCheckBox(Icon i)
JCheckBox(Icon i, boolean state)
JCheckBox(String s)
JCheckBox(String s, boolean state)
JCheckBox(String s, Icon i)
JCheckBox(String s, Icon i, boolean state)
Radio Buttons

Radio buttons are supported by the JRadioButton class, which is a concrete


implementation of AbstractButton.
Its immediate superclass is JToggleButton, which provides support for
two-state buttons.
Some of its constructors are :
JRadioButton(Icon i)
JRadioButton(Icon i, boolean state)
JRadioButton(String s)
JRadioButton(String s, boolean state)
JRadioButton(String s, Icon i)
JRadioButton(String s, Icon i, boolean state)
Combo Boxes
Swing provides a combo box (a combination of a text field and a drop-down list)
through the JComboBox class, which extends JComponent.
A combo box normally displays one entry.
However, it can also display a drop-down list that allows a user to select a
different entry.
You can also type your selection into the text field.
Two of JComboBox’s constructors are :
JComboBox( )
JComboBox(Vector v)
Tabbed Panes

A tabbed pane is a component that appears as a group of folders in a file


cabinet. Each folder has a title. When a user selects a folder, its contents
become visible. Only one of the folders may be selected at a time.
Tabbed panes are commonly used for setting configuration options.
Tabbed panes are encapsulated by the JTabbedPane class, which extends
JComponent.
Tabs are defined via the following method:
void addTab(String str, Component comp)
Here, str is the title for the tab, and comp is the component that should be
added to the tab.
Typically, a JPanel or a subclass of it is added.
The general procedure to use a tabbed pane in an applet is :
1. Create a JTabbedPane object.
2. Call addTab( ) to add a tab to the pane.
3. Repeat step 2 for each tab.
4. Add the tabbed pane to the content pane of the applet.
Scroll Panes
A scroll pane is a component that presents a rectangular area in which a
component may be viewed. Horizontal and/or vertical scroll bars may be

provided if necessary.

Scroll panes are implemented in Swing by the JScrollPane class, which


extends JComponent.

Some of its constructors are:


JScrollPane(Component comp)
JScrollPane(int vsb, int hsb)
JScrollPane(Component comp, int vsb, int hsb)

The steps that to use a scroll pane in an applet:


1. Create a JComponent object.
2. Create a JScrollPane object. (The arguments to the constructor
specify the component and the policies for vertical and horizontal scroll
bars.)
3. Add the scroll pane to the content pane of the applet.
Tables

A table is a component that displays rows and columns of data.


You can drag the cursor on column boundaries to resize columns. You can also
drag a column to a new position.
Tables are implemented by the JTable class, which extends JComponent.
One of its constructors is:
JTable(Object data[ ][ ], Object colHeads[ ])
Here, data is a two-dimensional array of the information to be presented, and
colHeads is a one-dimensional array with the column headings.
Here are the steps for using a table in an applet:
1. Create a JTable object.
2. Create a JScrollPane object. (The arguments to the constructor
specify the table and the policies for vertical and horizontal scroll bars.)
3. Add the table to the scroll pane.
4. Add the scroll pane to the content pane of the applet.

You might also like