Ajp Microproject
Ajp Microproject
A STUDY ON
Swing and Swing Components In Java
SUBMITTED ON Nov BY THE GROUP OF 3 STUDENT
This is to certify that Mr. Sai Konda Roll No.02 of Fourth Semester of Diploma
in Information Technology at institute Shivajirao S Jondhle Polytechnic
Asangaon (Code:-0935) has completed the micro project satisfactorily in
subject Advanced Java Programming(22517) for the academic year 2023 to
2024 as prescribed in the curriculum I Scheme.
Date : Seat No :
This is to certify that Mr. Gourav Bhatia Roll No.04 of Fourth Semester of
Diploma in Information Technology at institute Shivajirao S Jondhle
Polytechnic Asangaon (Code:-0935) has completed the micro project
satisfactorily in subject Advanced Java Programming(22517) for the academic
year 2023 to 2024 as prescribed in the curriculum I Scheme.
Date : Seat No :
This is to certify that Mr. Nayan Vishe Roll No.07 of Fourth Semester of
Diploma in Information Technology at institute Shivajirao S Jondhle
Polytechnic Asangaon (Code:-0935) has completed the micro project
satisfactorily in subject Advanced Java Programming(22517) for the academic
year 2023 to 2024 as prescribed in the curriculum I Scheme.
Date : Seat No :
No Description Page No
1 What is Swing
2 Introduction to Swing
3 Features of Swing
4 Example of Swing
6 Advantage
7 Disadvantages
8 Conclusion
11 Conclusion
ABSTRACT
• Swing framework is a part of Java Foundation Classes (JFC). JFC is used to create
window-based applications in a real-time environment. It is built on the top of
the
AWT API. Swing is completely developed by using Java language only.
• Swing mainly works on the principle of MVC (Model View Controller).
• The model gives the state of information about the components of swing like
JButton, JPassword Field, JLabel, JTextField, etc. Example: In the case of a combo
box the model consisting a field that determines if the box is multi-selected or
single selected or not selected from the drop-down list. A view shows how the
component or container is displayed on the screen.
• The controller tells you how the specified component reacts to the
user. Example: If there is a frame consists of a username, password, labels, etc.
with the login button as in the below swing example. Once we click on the login
button where the user has to go decides(means where the next action has to go)
by this controller.
Introduction to Swing
Swing is the collection of user interface components for Java programs. It is part of Java
Foundation classes that are referred to as JFC. In simple words, Swing is the graphical
user interface toolkit that is used for developing windows based java applications or
programs. It is the successor of AWT, known as the Abstract window toolkit API for
Java,and AWT components are mainly heavyweight.
The components are lightweight as compared to AWT components. It provides an
excellent interface to the user for all the platforms. It is not specifically for one platform.
The components are written in Java and platform-independent as well. The Java
foundation classes first appeared in 1997, then later called Swing. To use the swing in
java, javax. A swing package needs to be used or imported. It is also known as Java
Swing
Features of Swing
The features of the Swing are as follows:
1. Platform Independent: It is platform-independent; the swing components used
to build the program are not platform-specific. It can be used on any platform
and anywhere.
2. Lightweight: Swing components are lightweight, which helps in creating the UI
lighter. The swings component allows it to plug into the operating system user
interface framework, including the mappings for screens or devices and other
user interactions like keypress and mouse movements.
3. Plugging: It has a powerful component that can be extended to provide support
for the user interface that helps in a good look and feel to the application. It
refers to the highly modular-based architecture that allows it to plug into other
customized implementations and frameworks for user interfaces. Its components
are imported through a package called java.swing.
4. Manageable: It is easy to manage and configure. Its mechanism and composition
pattern also allows changing the settings at run time. The uniform changes can
be provided o the user interface without any changes to the application code.
5. MVC: They mainly follow the concept of MVC, which is the Model View
Controller. With the help of this, we can make changes in one component
without impacting or touching other components. It is known as loosely coupled
architecture as well.
6. Customizable: Swing controls can be easily customized. It can be changed, and the
visual appearance of the component application is independent of its internal
representation.
Examples of Swing,
The component class is mainly used, and some of the methods are frequently used, like
adding a component in another component (add (Component a)) and setting the size,
layout, and visibility of components accordingly.
Below is the example:
importjavax.swing.*;
publicclassTestextendsJFrame{
publicTest(){
super("Test");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(newJLabel("Test, Application!"));
pack();
setVisible(true);
}
publicstaticvoidmain(final String[]args){
newTest();
}
}
importjavax.swing.*;
publicclassSwing{ publicstaticvoidmain
(String[]args){
JFrame f=newJFrame();//creating instance of JFrame
JButton b=newJButton("Submit Button");//creating instance of JButton
b.setBounds(120,90,90,35);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
f.setSize(400,500);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible }
}
Difference between Swing and AWT
ImageIcon.
JButton.
JLabel. ...
JTextField. ...
JTextArea. ...
JPasswordField. ...
JCheckBox. ...
JRadioButton.
JList
JcomboBox
JfileChooser
JTabbedPane
JSlider
ImageIcon
The ImageIcon component creates an icon sized-image from an image residing at the
source URL.
Example:
ImageIconhomeIcon =newImageIcon("src/images/home.jpg");
This returns an icon of a home button. The string parameter is the path at which the
source image is present.
Note: We would be using this image icon in further examples.
1. JButton
JButton class is used to create a push-button on the UI. The button can contain some
display text or image. It generates an event when clicked and double-clicked.
A JButton can be implemented in the application by calling one of its constructors.
Example:
JButtonokBtn =newJButton("Ok");
JButtonhomeBtn =newJButton(homeIcon);
2. JLabel
JLabel class is used to render a read-only text label or images on the UI. It does not
generate any event.
Example:
JLabelimgLabel =newJLabel(homeIcon);
3. 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 pass an optional integer
parameter to it. This parameter sets the width of the box measured by the number of
columns. It does not limit the number of characters that can be input in the box.
Example:
JTextFieldtxtBox =newJTextField(20);
4. JTextArea
JTextArea class renders a multi-line text box. Similar to the JTextField, a user can input
non-formatted text in the field. The constructor for JTextArea also expects two integer
parameters which define the height and width of the text-area in columns. It does not
restrict the number of characters that the user can input in the text-area.
Example:
5. JPasswordField
JPasswordField is a subclass of JTextField class. It renders a text-box that masks the user
input text with bullet points. This is used for inserting passwords into the application.
Example:
JPasswordFieldpwdField =newJPasswordField(15);
var pwdValue=pwdField.getPassword();
It returns a password field of 15 column width. The getPassword method gets the value
entered by the user.
6. JCheckBox
JCheckBox renders a check-box with a label. The check-box has two states – on/off.
When selected, the state is on and a small tick is displayed in the box.
Example:
It returns a checkbox with the label Show Help. Notice the second parameter in the
constructor. It is a boolean value that indicates the default state of the check-box. True
means the check-box is defaulted to on state.
7. JRadioButton
JRadioButton is used to render a group of radio buttons in the UI. A user can select one
choice from the group.
Example:
ButtonGroupradioGroup =newButtonGroup(); JRadioButton rb1 =newJRadioButton("Easy",true); JRadio
radioGroup .add(rb2);
radioGroup .add(rb3);
The above code creates a button group and three radio button elements. All three
elements are then added to the group. This ensures that only one option out of the
available options in the group can be selected at a time. The default selected option is
set to Easy.
8. JList
JList component renders a scrollable list of elements. A user can select a value ormultiple
values from the list. This select behavior is defined in the code by the developer.
Example:
DefaultListItemcityList =newDefaultListItem(); cityList .addElement("Mumbai"):
cityList .addElement("London"): cityList .addElement("New York"):
cityList .addElement("Sydney"):
cityList .addElement("Tokyo"):
The above code renders a list of cities with 5 items in the list. The selection restriction is
set to SINGLE_SELECTION. If multiple selections is to be allowed, set the behavior to
MULTIPLE_INTERVAL_SELECTION.
9. JComboBox
JComboBox class is used to render a dropdown of the list of options.
Example:
String []cityStrings={"Mumbai","London","New York","Sydney","Tokyo"};
cities .setSelectedIndex(3);
The default selected option can be specified through the setSelectedIndex method. The
above code sets Sydney as the default selected option.
10. JFileChooser
JFileChooser class renders a file selection utility. This component lets a user select a file
from the local system.
Example:
JFileChooserfileChooser =newJFileChooser(); JButtonfileDialogBtn =newJButton("Select File"); fileDialog
})
var selectedFile=fileChooser.getSelectedFile();
The above code creates a file chooser dialog and attaches it to the button. The button
click would open the file chooser dialog. The selected file is returned through the
getSelectedFile method.
11. JTabbedPane
JTabbedPane is another very useful component that lets the user switch between tabs
in an application. This is a highly useful utility as it lets the user browse more content
without navigating to different pages.
Example:
JTabbedPanetabbedPane =newJTabbedPane();
tabbedPane .addTab("Tab 1",newJPanel()); tabbedPane .addTab("Tab 2",newJPanel());
The above code creates a two tabbed panel with headings Tab 1 and Tab 2.
12. JSlider
JSlider component displays a slider which the user can drag to change its value. The
constructor takes three arguments – minimum value, maximum value, and initial value.
Example:
JSlidervolumeSlider =newJSlider(0,100,50); var volumeLevel=volumeSlider.getValue();
The above code creates a slider from 0 to 100 with an initial value set to 50. The value
selected by the user is returned by the getValue method.
CONCLUSION
It is the framework used for building windows based applications for Java. It was
developed to solve the issues that are in AWT. It provides more components to work
and uses extensible components to develop the applications. There are many
components in the package or library to perform and define the look and feel of the
project or application.
It is referred to as the next-generation GUI developed for Java programs. Java Swing is a
library of the GUI controls, and classes are not platformed dependent and are lighter in
weight because they don’t create the peer components. It mainly provides a consistent
appearance or the look and feel of the application across all the platforms.
Now that you have got the gist 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 applications. So, get your hands-on on these components by
including them in your Swing application.
Introduction to Swing Components in Java