Introduction to Swing in Java
Introduction to Swing in Java
What is Swing?
Swing is a GUI (Graphical User Interface) toolkit in Java that provides a rich set of components for
building user-friendly desktop applications. It is a part of Java Foundation Classes (JFC) and is used to
create windows, buttons, text fields, menus, tables, and other UI elements.
Swing is an extension of AWT (Abstract Window Toolkit), providing more powerful, flexible, and
customizable components. It is built on top of AWT but operates independently of native platform
GUI elements, making it platform-independent.
Feature Description
Pluggable Look and Swing allows changing the UI theme dynamically (e.g., Windows, Nimbus,
Feel Metal, etc.).
Rich Set of
Provides advanced UI elements like JTree, JTable, JTabbedPane, etc.
Components
Event-Driven Uses listeners to handle user interactions like button clicks and keyboard
Programming input.
Customizable
Components can be customized using colors, fonts, borders, and images.
Components
Uses Java2D API Enables smooth rendering of components with graphics and animations.
Supports
Swing supports multi-threaded execution for better UI performance.
Multithreading
Difference Between AWT and Swing
Support for Provides basic UI elements like Button, Provides rich UI elements like JTable,
Advanced UI Checkbox, TextField JTree, JTabbedPane
Conclusion:
• Swing is more powerful, flexible, and customizable, making it suitable for modern
applications.
Icon:
Key Points:
2. TextField (JTextField)
JTextField:
Key Points:
Key Points:
4. Button (JButton)
JButton:
Key Points:
5. CheckBox (JCheckBox)
JCheckBox:
Key Points:
6. RadioButton (JRadioButton)
JRadioButton:
Key Points:
Summary Table:
Components:
3. Tree (JTree)
4. Table (JTable)
6. Tool Tip
JTabbedPane is a GUI(Graphical User Interface) component in the Java Swing library that allows you
to create a tabbed pane interface. A tabbed pane is a container that can store and organize multiple
components into distinct tabs. It contains a collection of tabs. When you click on a tab, only data
related to that tab will be displayed. JTabbedPane comes under the Java Swing package.
Constructor Description
Method Description
• java.awt.Container
• javax.swing.JComponent
• javax.swing.JTabbedPane
• javax.swing.JContainer
• Output:
Key Methods:
Java JScrollPane is a component in the Java Swing library that provides a scrollable view of another
component, usually a JPanel or a JTextArea. it provides a scrolling functionality to the display for
which the size changes dynamically. It is useful to display the content which exceeds the visible area
of the window. In this article, we are going to see some constructors, methods, and examples
of JScrollPane.
Methods of JScrollPane
Methods Description
Key Methods:
The JTree is a type of GUI(Graphic User Interface) that displays information in a hierarchical way. This
intricate component part provides a quite elegant substance of representing relationships among
elements in a tree-like structure. In this exploration, we'll delve into the essence of the JTree class,
examining its declaration, normally used constructors and examples.
This constructor creates a JTree with a sample model. It serves as a quick way
JTree() to initialize a tree structure without specifying a custom model.
JTree is created with each element of the specified array becoming a child of a
JTree(Object[] new root node. This constructor is useful when you want to build a tree
value) structure based on an array of values.
JTree(TreeNode JTree is created with the specified TreeNode as its root. This allows you to
root) define a custom structure for your tree by providing a root node explicitly.
4. Table (JTable)
Description:
Key Methods:
The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional
data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular
form.
Constructors in JTable:
3. JTable(Object[][] data, Object []Column): A table is created with the specified name where
[]Column defines the column names.
Functions in JTable:
3. editCellAt(int row, int col) : edits the intersecting cell of the column number col and row
number row programmatically, if the given indices are valid and the corresponding cell is
editable.
4. setValueAt(Object value, int row, int col) : Sets the cell value as ‘value’ for the position row,
col in the JTable.
Out put:
5. Progress Bar (JProgressBar)
Description:
Key Methods:
JProgressBar is a part of Java Swing package. JProgressBar visually displays the progress of some
specified task. JProgressBar shows the percentage of completion of specified task.The progress bar
fills up as the task reaches it completion. In addition to show the percentage of completion of task, it
can also display some text
3. JProgressBar(int min, int max) : creates an progress bar with specified minimum and
maximum value.
4. JProgressBar(int orientation, int min, int max) : creates an progress bar with specified
minimum and maximum value and a specified orientation.if SwingConstants.VERTICAL is
passed as argument a vertical progress bar is created, if SwingConstants.HORIZONTAL is
passed as argument a horizontal progress bar is created.
6. Tool Tip
Description:
Key Method:
We can add tooltip text to almost all the components of Java Swing by using the following method
setToolTipText(String s). This method sets the tooltip of the component to the specified string s.
When the cursor enters the boundary of that component a popup appears and text is displayed.
Methods used: