Every application has some basic interactive interface for the user. For example, a button, check-box, radio-button, text-field, etc. These together form the components in Swing.
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
Advanced java programming
1. Nadar saraswathi college of arts &
science,theni.
Department of cs & it
ADVANCED JAVA PROGRAMMING
PRESENTED BY
G.KAVIYA
M.SC(IT)
TOPIC:SWING COMPONENTS.
3. SYNOPSIS
o WHAT IS SWING?
o WHAT IS COMPONENTS?
o WHAT IS SWING COMPONENTS?
o TYPES OF SWING COMPONENTS?
o ADVANTAGES AND DISADVANTAGE OF SWING
o DEFINITION OF JTEXT FIELD
o DEFINITION OF JCHECKBOX
o DEFINITION OF JRADIOBUTTON
4. CONTINOUS:
o DEFINITION OF JLIST
o DEFINITION OF JCOMBOBOX
o DEFINITION OF JLABLE
o CODING
o ADVANTAGES AND DISADVANTAGE OF SWING
o CONCLUSION
5. What is Swing?
Swing does not use native resources, so it is light weighted.
The swing user interface does not depend on any specific platform, so
the swing is platform-independent.
Swing used to develop a standalone desktop application.
Swing provides plat-independent and light weighted.
6. Why we called as a swing is an lightweight
component?
A Swing Component is said to be a lightweight Components
because it written entirely in java and does the high-level
display work itself, rather than relying on code provided by
your computer’s operating system.
7. What is components?
Swing components are derived from the JComponent class. The
only exceptions are the four top level containers: jframe, japplet,
jwindow, and jdialog.
JComponent inherits awt classes container and component.
All the swing components are represented by classes in the javax.
Swing package.
All the component classes start with J: JLabel, JButton,
JScrollBar,……
8. Swing Components
Swing components are the basic building blocks of an application.
Swing is GUI widget toolkit for java.
Every application has some basic interactive interface for the user.
Example: button, checkbox, radiobutton, textfield etc.,
Swing components are the interactive element in a java.
10. Commonly used methods of components class
METHODS 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.
public void setVisible(boolen b) Sets the visibility of the component. It is by
default false.
11. Types of components
o JScrollBar
o JTabbedPane
o JTree
o JToggleButton
o JTable
o JTextField
o JButton
o JCheckbox
o JComboBox
o JLabel
o JList
o JRadioButton
12. jtextfield
JTextField renders an editable single line text box.
A user can input non-formatted text in the box. To initialize the
text field, call its constructor and the pass an optional integer
parameter to it. This parameter sets the width of the box
measured by the columns.
JTextField class declaration
public class jtexfield extends jtext components to
swingConstants.
13. Commonly used Constructors of
JTextField
Constructor Description
JTextField() Creates a new TextField
JTextField(String text) Creates a new TextField initialized with
the specified text.
JTextField(String text, int columns) Creates a new TextField initialized with
the specified text and columns.
JTextField(int columns) Creates a new empty TextField with the
specified number and columns
14. Commonly used methods :
METHODS DESCRIPTION
void addActionListener (ActionListener l) It is used to add the specified action listener
to receive action events from this textfield.
Action getAction() It returns the currently set Action for this
ActionEvent source, or null if no Action is set.
void setFont(Font f) It is used to set the current font.
void removeActionListener(ActionListener l) It is used to remove the specified action
listener so that it no longer receives action
events from this textfield.
15. EXAMPLE:
It renders a textbox of 20 columns width
JTextField txtBox = new
JTextField(20);
16. Jcheckbox
The JCheckBox class used to create a checkbox it 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 JToggleButton
class.
JCheckBox class declaration
public class JCheckBox extends JToggleButton implements
Accessible
17. Commonly used Constructors of
JCheckBox
Constructor Description
JCheckBox() Creates an initially unselected check box
button with no text, no icon.
JCheckBox(String s) Creates an initially unselected check box
with text.
JCheckBox(String text, boolean selected) Creates a check box with text and
specifies whether or not it is initially
selected.
JCheckBox(Action a) Creates a check box-where properties, are
taken from the Action supplied.
18. Commonly used methods :
METHODS DESCRIPTION
AccessibleContext getAccessibleContent() Its used to get the AccessibleContext
associated with this JCheckBox.
protected String paramString() It returns a string representation of this
JCheckBox.
19. EXAMPLE:
Boolean value that indicates the default state of the check-box.
CheckBox chkBox = new
JCheckBox(“Show Help’, true)
20. 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 radio button only.
JRadioButton class declaration:
public class JRadioButton extends JToggleButton implements
Accessible.
21. Commonly used Constructors of
JRadioButton
Constructor Description
JRadioButton() Creates an unselected radio button
with no text.
JRadioButton(String s) Creates an unselected radio button
with specified text.
JRadioButton(String s, boolean
selected)
Creates a radio button with the
specified text and selected status.
22. Commonly used methods :
METHODS DESCRIPTION
void set Text(String s) Its used to set specified text on button.
String getText() Its used to return the text of the button.
void setEnabled(boolean b) Its used to enable or disable the button.
void setIcon(Icon b) Its used to specified Icon on the button.
Icon getIcon() Its used to get the Icon of the button.
void set Mnemonic(int a) Its used to set the mnemonic on the button.
void addActionListener(ActionListener a) Its used to add action listener to this object.
23. EXAMPLE:
ButtonGroup radioGroup = newButtonGroup();
JRadioButton rb1 = new JRadioButton(“Easy”, true);
JRadioButton rb2 = new JRadioButton(“medium”);
JRadioButton rb3 = new JRadioButton(“Hard”);
radioGroup.add(rb1);
radioGroup.add(rb2);
radioGroup.add(rb3);
24. Jlist
Jlist components renders a scrollable list of elements. A user
can select a value or multiple values from the list. The select
behaviour is define in the code by the developer . It inherits
JComponents class.
JList class declaration:
public class JList extends JComponent implements Scrollable
Accessible.
25. Commonly used Constructors of JList
Constructor Description
JList() Creates a JList with an empty, read-only,
model.
JList(ary[ ] listData) Creates a JList that displays the elements
in the specified array.
JList(ListModel<ary> dataModel) Creates a Jlist that displays elements from
the specified, non-null, model.
26. Commonly used methods :
METHODS DESCRIPTION
void addListSelectionListener
(ListSelectionListenerlistener)
It is used to add a listener to the list,to be
notified each time a change a selection occurs.
int getSelectedIndex() It is used to return the smallest selected cell
index.
ListModel getModel() It is used to be return data model that holds a
list of items displayed by the Jlist component.
Void setListData(Object[] listData) It is used to create a read-only ListModel from
an array of objects.
27. EXAMPLE:
DefaultListItem cityList = new DefaultListItem();
CityList.addElements (“Mumbai”):
CityList.addElements(“London”):
CityList.addElements(“New York”):
CityList.addElements(“Sydney”):
CityList.addElement(“Tokyo”):
JList cities = new Jlist(cityList);
Cities.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
28. Jcombobox
The object of Choice class is used to show popup menu of
choices. Choice selected by user in shown on the top of a menu.
It inherits JComponent class.
JComboBox class declaration:
public class JComboBox extends JComponent implements
ItemSelectable.
ListDataListener, ActionListener, Accessible.
29. Commonly used Constructors of
JComboBox
Constructor Description
JComboBox() Creates a JComboBox with a default
data model.
JComboBox(Object[] items) Creates a JComboBox that contains the
elements in the specified array.
JComboBox(Vertex<?> items) Creates a JComboBox that contains the
elements in the specified Vector.
30. Commonly used methods :
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.
void setEditable(Boolean b) It is used to determine whether the 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.
32. Jlabel
The object of JLabel class is a component for placing 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 Accessible.
33. Commonly used Constructors of JLable
Constructor Description
JLabel() Creates a JLabel instances with no image and
with an empty string for the title.
JLabel(String s) Creates a JLabel instances with the specified
text.
JLabel(Icon i) Creates a JLabel instances with the specified
image.
Jlabel(String s, Icon i, int
horizontalAlignment)
Creates a Jlabel instances with the specified
text, image and horizontal alignment.
34. Commonly used methods :
METHODS DESCRIPTION
String getText() It returns the text string that a label display.
void set Text(String text) It defines the single line of the text of this
component will display.
void setHorizontalAlignment(int alignment) It sets the alignment of the Label’s content along
the X-axis.
Icon getIcon() It returns the graphic image that the label
display.
int getHorizontalAlignment() It returns the alignment of the label’s contents
along the X-axis.
35. EXAMPLE:
It returns a label with a home icon.
JLabel imgLabel = new
JLabel(homeIcon);
36. CODING
import javax.swing.*;
public class SwingApp {
SwingApp(){
JFrame f = new JFrame();
JLabel firstName = new JLabel(“First Name”);
firstName.setBounds(20,50,80,20);
JLabel lastName = new JLabel(“Last Name”);
lastName.setBounds(20,80,80,20);
JLabel dob = new JLabel(“Date of Birth”);
dob.setBounds(20,110,80,20);
37. continuous
JTextField firstNameTF = new JTextField();
firstNameTF.setBounds(120,50,100,20);
JTextField lastNameTF = new JTextField();
lastNameTF.setBounds(120,80,100,20);
JTextField dobTF = new JTextField();
dobTF.setBounds(120,110,100,20);
JButton sbmt = new JButton(“Submit”);
sbmt.setBounds(20,160,100,30);
41. ADVANTAGES AND DISADVANTAGE OF SWING
ADVANTAGE DISADVANTAGE
Swing provides paint debugging support
for when you build your own
components.
Swing components that look like a native
components might not act exactly like
native components.
Swing components use the model-view-
controller paradigm(MVC) and thus can
provide a much more flexible UI.
It requires java 1.2 or a separate JAR file.
Swing provides built-in double buffering. It can be slower than AWT(all
components are drawn)as if we’re not
careful in programming.
42. Conclusion
The list of Components in Swing, it is highly
recommended to dive deeper and explore more.
Swing Components are fun to play around with and
can help create some real cool application.