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

javaswing1-170324224205

Java Swing is a part of Java Foundation Classes (JFC) used for creating window-based applications, built on AWT and providing platform-independent components. It includes various components like JButton, JLabel, JTextField, JCheckBox, JRadioButton, and JComboBox, each with specific methods and constructors for functionality. The document also highlights the differences between AWT and Swing, emphasizing Swing's lightweight nature and support for pluggable look and feel.

Uploaded by

HOD AN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

javaswing1-170324224205

Java Swing is a part of Java Foundation Classes (JFC) used for creating window-based applications, built on AWT and providing platform-independent components. It includes various components like JButton, JLabel, JTextField, JCheckBox, JRadioButton, and JComboBox, each with specific methods and constructors for functionality. The document also highlights the differences between AWT and Swing, emphasizing Swing's lightweight nature and support for pluggable look and feel.

Uploaded by

HOD AN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

JAVA SWING

Created by Apurbo Datta


OVERVIEW:

What is Swing?

Java swing components

How to change TitleBar icon


in Java Swing

Some Java Swing Apps


What is java swing

 Java Swing is a part of Java Foundation Classes (JFC)


that is used to create window-based applications. It is
built on the top of AWT (Abstract Windowing Toolkit) API
and entirely written in java.
 Java Swing provides platform-independent and
lightweight components.
 The javax.swing package provides classes for java swing
API such as JButton, JTextField, JTextArea, JRadioButton,
JCheckbox, JMenu, JColorChooser etc.
Difference between AWT and Swing
No. Java AWT Java Swing
AWT components are platform- Java swing components are
1)
dependent. platform-independent.
AWT components are Swing components are
2)
heavyweight. lightweight.
Swing supports pluggable look and
AWT doesn't support pluggable
3) feel. (can change visual
look and feel.
appearance )
Swing provides more powerful
AWT provides less components components such as tables, lists,
4)
than Swing. scrollpanes, colorchooser,
tabbedpane etc.
AWT doesn't follows MVC(Model
View Controller) where model
represents data, view represents
5) Swing follows MVC.
presentation and controller acts as
Hierarchy of Java Swing classes
Commonly used Methods of
Component class

Method Description

public void add(Component c) add a component on another component.

public void setSize(int width,int height) sets size of the component.


sets the layout manager for the
public void setLayout(LayoutManager m)
component.

sets the visibility of the component. It is by


public void setVisible(boolean b)
default false.
Java JButton

Declaration of JButton class.

public class JButton extends AbstractButton implements A


ccessible

Commonly used Constructors of JButton:


Constructor Description
JButton() It creates a button with no text and icon.
It creates a button with the specified
JButton(String s)
text.
It creates a button with the specified icon
JButton(Icon i)
object.
Commonly used Methods of AbstractButton class:

Methods Description
void setText(String s) It is used to set specified text on button
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
It is used to set the specified Icon on the
void setIcon(Icon b)
button.
Icon getIcon() It is used to get the Icon of the button.
It is used to set the mnemonic on the
void setMnemonic(int a)
button.
It is used to add the action listener to this
void addActionListener(ActionListener a)
object.
a mnemonic defines a key that lets you select an item from an active menu by typing the key.
Thus, a mnemonic allows you to use the keyboard to select an item from a menu that is already
being displayed.

A mnemonic can be specified for both JMenuItem and JMenu objects.


There are two ways to set the mnemonic for JMenuItem
First, it can be specified when an object is constructed using this constructor:

JMenuItem(String name, int mnem)

In this case, the name is passed in name and the mnemonic is passed in mnen. Second, you can
set the mnemonic by calling setMnemonic( ).
Example of Java JButton
Java JLabel
The object of JLabel class is a component for placing text in a
container. It is used to display a single line of read only text. The text
can be changed by an application but a user cannot edit it directly. It
inherits JComponent class.

JLabel class declaration

public class Jlable extends JComponenet


implements Accessible
Commonly used Constructors of JLabel:

Constructor Description
Creates a JLabel instance with no image
JLabel()
and with an empty string for the title.
Creates a JLabel instance with the
JLabel(String s)
specified text.
Creates a JLabel instance with the
JLabel(Icon i)
specified image.
Creates a JLabel instance with the
JLabel(String s, Icon i, int
specified text, image, and horizontal
horizontalAlignment)
alignment.
Commonly used Methods of JLabel:

Methods Description
t returns the text string that a label
String getText()
displays.
It defines the single line of text this
void setText(String text)
component will display.
void setHorizontalAlignment(int It sets the alignment of the label's
alignment) contents along the X axis.
It returns the graphic image that the
Icon getIcon()
label displays.
It returns the alignment of the label's
int getHorizontalAlignment()
contents along the X axis.
Java JLabel Example
Java JTextField

The object of a JTextField class is a text component that allows the editing
of a single line text. It inherits JTextComponent class.

JTextField class declaration

public class JTextField extends JTextComponent implements Swing


Constants
Commonly used Constructors of JTextField :

Constructor Description

JTextField() Creates a new TextField

Creates a new TextField initialized with


JTextField(String text)
the specified text.

Creates a new TextField initialized with


JTextField(String text, int columns)
the specified text and columns.

Creates a new empty TextField with the


JTextField(int columns)
specified number of columns
Commonly used Methods:

Methods Description
It is used to add the specified action
void addActionListener(ActionListener l) listener to receive action events from this
textfield.
It returns the currently set Action for this
Action getAction() ActionEvent source, or null if no Action is
set.
void setFont(Font f) It is used to set the current font.

It is used to remove the specified action


void removeActionListener(ActionListener
listener so that it no longer receives action
l)
events from this textfield.
Java JTextField Example
Java JCheckBox

The JCheckBox class is used to create a checkbox. It is used to turn an option on


(true) or off (false). Clicking on a CheckBox changes its state from "on" to "off"
or from "off" to "on ".It inherits JToggleButton class.

JCheckBox class declaration

public class JCheckBox extends JToggleButton implements Accessib


le
Commonly used Constructors:
Constructor Description
Creates an initially unselected check box
JCheckBox()
button with no text, no icon.
Creates an initially unselected check box
JCheckBox(String s)
with text.
Creates a check box with text and
JCheckBox(String text, boolean selected) specifies whether or not it is initially
selected.
Creates a check box where properties are
JCheckBox(Action a)
taken from the Action supplied.
Commonly used Methods:
Methods Description
It is used to get the AccessibleContext
AccessibleContext getAccessibleContext()
associated with this JCheckBox.
It returns a string representation of this
protected String paramString()
JCheckBox.
Java JCheckBox Example Java JCheckBox Example with
ItemListener
Java JRadioButton
The JRadioButton class is used to create a radio button. It is used to choose one option from
multiple options. It is widely used in exam systems or quiz.

 It should be added in ButtonGroup to select one radio button only.

JRadioButton class declaration:

public class JRadioButton extends JToggleButton impleme


nts Accessible

Commonly used Constructors of JRadioButton:

Constructor Description
Creates an unselected radio button with
JRadioButton()
no text.
Creates an unselected radio button with
JRadioButton(String s)
specified text.
Creates a radio button with the specified
JRadioButton(String s, boolean selected)
text and selected status.
Commonly used Methods:

Methods Description
void setText(String s) It is used to set specified text on button.
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
It is used to set the specified Icon on the
void setIcon(Icon b)
button.
Icon getIcon() It is used to get the Icon of the button.
It is used to set the mnemonic on the
void setMnemonic(int a)
button.
It is used to add the action listener to this
void addActionListener(ActionListener a)
object.
Java JRadioButton Java JRadioButton Example with
Example ActionListener
Java JComboBox
The object of Choice class is used to show popup menu of choices.
Choice selected by user is shown on the top of a menu. It inherits
JComponent class.
JComboBox class declaration
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, Acti
onListener, Accessible

Commonly used Constructors Of JComboBox:

Constructor Description
Creates a JComboBox with a default data
JComboBox()
model.
Creates a JComboBox that contains the
JComboBox(Object[] items)
elements in the specified array.
Creates a JComboBox that contains the
JComboBox(Vector<?> items)
elements in the specified Vector.
Commonly used Methods Of JComboBox :

Methods Description
void addItem(Object anObject) It is used to add an item to the item list.
It is used to delete an item to the item
void removeItem(Object anObject)
list.
It is used to remove all the items from
void removeAllItems()
the list.
It is used to determine whether the
void setEditable(boolean b)
JComboBox is editable.
void addActionListener(ActionListener a) It is used to add the ActionListener.
void addItemListener(ItemListener i) It is used to add the ItemListener.
Java JComboBox Java JComboBox Example with ActionListen
Example

You might also like