0% found this document useful (0 votes)
72 views46 pages

Java Swing for Developers

Java Swing is a GUI toolkit that is used to create window-based applications. It provides platform-independent and lightweight components like buttons, text fields, labels, etc. Some key Swing components include JButton, JTextField, JTextArea, JLabel. Swing components are lightweight, platform independent and follow the MVC architecture.

Uploaded by

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

Java Swing for Developers

Java Swing is a GUI toolkit that is used to create window-based applications. It provides platform-independent and lightweight components like buttons, text fields, labels, etc. Some key Swing components include JButton, JTextField, JTextArea, JLabel. Swing components are lightweight, platform independent and follow the MVC architecture.

Uploaded by

Chalew Zeynu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link] 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 platform-
1)
dependent. independent.
2) AWT components are heavyweight. Swing components are lightweight.
AWT doesn't support pluggable look Swing supports pluggable look and
3)
and feel. feel.
Swing provides more powerful
AWT provides less components than components such as tables, lists,
4)
Swing. scrollpanes, colorchooser, tabbedpane
etc.
AWT doesn't follows MVC(Model View
Controller) where model represents
5) data, view represents presentation and Swing follows MVC.
controller acts as an interface between
model and view.
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.

public void setLayout(LayoutManager m) sets the layout manager for the 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 Access


ible

Commonly used Constructors of JButton:


Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified 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.
void setMnemonic(int a) It is used to set the mnemonic on the button.
It is used to add the action listener to this
void addActionListener(ActionListener a)
object.
Example of Java JButton
Java JPasswordField
The object of a JPasswordField class is a text component specialized for password entry. It allows the
editing of a single line of text. It inherits JTextField class.

JPasswordField class declaration


public class JPasswordField extends JTextField

Commonly used Constructors JPasswordField:

Constructor Description
Constructs a new JPasswordField, with a default
JPasswordField() document, null starting text string, and 0 column
width.
Constructs a new empty JPasswordField with the
JPasswordField(int columns)
specified number of columns.
Constructs a new JPasswordField initialized with
JPasswordField(String text)
the specified text.
Construct a new JPasswordField initialized with
JPasswordField(String text, int columns)
Java JPasswordField Example Java JPasswordField Example with ActionListener
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 JButton extends AbstractButton implements Acc


essible
Commonly used Constructors of JLabel:

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

Methods Description
String getText() t returns the text string that a label displays.
It defines the single line of text this
void setText(String text)
component will display.
It sets the alignment of the label's contents
void setHorizontalAlignment(int alignment)
along the X axis.
It returns the graphic image that the label
Icon getIcon()
displays.
It returns the alignment of the label's contents
int getHorizontalAlignment()
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 SwingCo


nstants
Commonly used Constructors of JTextField :

Constructor Description
JTextField() Creates a new TextField
Creates a new TextField initialized with the
JTextField(String text)
specified text.
Creates a new TextField initialized with the
JTextField(String text, int columns)
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 listener
void addActionListener(ActionListener l)
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 l) listener so that it no longer receives action
events from this textfield.
Java JTextField Example
Java JTextArea
The object of a JTextArea class is a multi line region that displays text. It allows the editing
of multiple line text. It inherits JTextComponent class

JTextArea class declaration


public class JTextArea extends JTextComponent

Commonly used Constructors of JTextArea:


Constructor Description
JTextArea() Creates a text area that displays no text initially.
Creates a text area that displays specified text
JTextArea(String s)
initially.
Creates a text area with the specified number of
JTextArea(int row, int column)
rows and columns that displays no text initially.
Creates a text area with the specified number of
JTextArea(String s, int row, int column)
rows and columns that displays specified text.
Commonly used Methods of JTextArea:

Methods Description
void setRows(int rows) It is used to set specified number of rows.
void setColumns(int cols) It is used to set specified number of columns.
void setFont(Font f) It is used to set the specified font.
It is used to insert the specified text on the
void insert(String s, int position)
specified position.
It is used to append the given text to the end
void append(String s)
of the document.
Java JTextArea Example Java JTextArea Example with
ActionListener
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 Accessible


Commonly used Constructors:
Constructor Description
Creates an initially unselected check box
JJCheckBox()
button with no text, no icon.
Creates an initially unselected check box with
JChechBox(String s)
text.
Creates a check box with text and specifies
JCheckBox(String text, boolean selected)
whether or not it is initially selected.
Creates a check box where properties are taken
JCheckBox(Action a)
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 ItemListene
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 implements A


ccessible

Commonly used Constructors of JRadioButton:

Constructor Description
Creates an unselected radio button with no
JRadioButton()
text.
Creates an unselected radio button with
JRadioButton(String s)
specified text.
Creates a radio button with the specified text
JRadioButton(String s, boolean selected)
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.
void setMnemonic(int a) It is used to set the mnemonic on the button.
It is used to add the action listener to this
void addActionListener(ActionListener a)
object.
Java JRadioButton Java JRadioButton Example with ActionListener
Example
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, ActionListen
er, 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.
void removeItem(Object anObject) It is used to delete an item to the item list.
void removeAllItems() It is used to remove all the items from 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 ActionListene
Example
Java JTable
The JTable class is used to display data in tabular form. It is composed of rows and
columns.

Commonly used Constructors of JTable:

Constructor Description
JTable() Creates a table with empty cells.
JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.
Java JTable Example Java JTable Example with ListSelectionListene

If you select an element in column NAME, name of the element will be


displayed on the console:

Table element selected is: Sachin


Java JList
The object of JList class represents a list of text items. The list of text items can be set
up so that the user can choose either one item or multiple items. It inherits
JComponent class.
JList class declaration
public class JList extends JComponent implements Scrollable, Accessibl
e
Commonly used Constructors of JList :

Constructor Description
Creates a JList with an empty, read-only,
JList()
model.
Creates a JList that displays the elements in
JList(ary[] listData)
the specified array.
Creates a JList that displays elements from
JList(ListModel<ary> dataModel)
the specified, non-null, model.
Commonly used Methods of JList :

Methods Description
Void It is used to add a listener to the list, to be
addListSelectionListener(ListSelectionListener notified each time a change to the selection
listener) occurs.
It is used to return the smallest selected cell
int getSelectedIndex()
index.
It is used to return the data model that holds
ListModel getModel() a list of items displayed by the JList
component.
It is used to create a read-only ListModel from
void setListData(Object[] listData)
an array of objects.
Java JList Example Java JList Example with ActionListen
Java JPanel
The JPanel is a simplest container class. It provides space in which an
application can attach any other component. It inherits the JComponents
class.
It doesn't have title bar.

JPanel class declaration


public class JPanel extends JComponent implements Accessible
Commonly used Constructors of JPanel :
Constructor Description
It is used to create a new JPanel with a double
JPanel()
buffer and a flow layout.
It is used to create a new JPanel with
JPanel(boolean isDoubleBuffered) FlowLayout and the specified buffering
strategy.
It is used to create a new JPanel with the
JPanel(LayoutManager layout)
specified layout manager.

Java JPanel Example


Java JProgressBar
The JProgressBar class is used to display the progress of the task. It inherits
JComponent class.
JProgressBar class declaration
public class JProgressBar extends JComponent implements SwingConstants, Ac
cessible
Commonly used Constructors:

Constructor Description
It is used to create a horizontal progress bar but no
JProgressBar()
string text.
It is used to create a horizontal progress bar with
JProgressBar(int min, int max)
the specified minimum and maximum value.
It is used to create a progress bar with the
specified orientation, it can be either Vertical or
JProgressBar(int orient)
Horizontal by using [Link] and
[Link] constants.
It is used to create a progress bar with the
JProgressBar(int orient, int min, int max) specified orientation, minimum and maximum
Commonly used Methods:
Method Description
void setStringPainted(boolean b) It is used to determine whether string should be displayed.
void setString(String s) It is used to set value to the progress string.
It is used to set the orientation, it may be either vertical or
void setOrientation(int orientation) horizontal by using [Link] and
[Link] constants.
void setValue(int value) It is used to set the current value on the progress bar.

Java JProgressBar Example


Java JSlider
The Java JSlider class is used to create the slider. By using JSlider, a user can select a value
from a specific range.

Commonly used Constructors of JSlider class

Constructor Description
creates a slider with the initial value of 50 and
JSlider()
range of 0 to 100.
creates a slider with the specified orientation
set by either [Link] or
JSlider(int orientation)
[Link] with the range 0 to 100 and
initial value 50.
creates a horizontal slider using the given min
JSlider(int min, int max)
and max.
creates a horizontal slider using the given min,
JSlider(int min, int max, int value)
max and value.
JSlider(int orientation, int min, int max, int creates a slider using the given orientation,
value) min, max and value.
Commonly used Methods of JSlider class

Method Description
is used to set the minor tick spacing to the
public void setMinorTickSpacing(int n)
slider.
is used to set the major tick spacing to the
public void setMajorTickSpacing(int n)
slider.
is used to determine whether tick marks are
public void setPaintTicks(boolean b)
painted.
is used to determine whether labels are
public void setPaintLabels(boolean b)
painted.
Java
public JSlider
void Example
setPaintTracks(boolean b) is used to determine whether track is painted.
Java JSlider Example: painting ticks
How to change TitleBar icon in Java Swing
Some Java Swing Apps

Online Exam
Calculator
IP Finder

You might also like