Sharad Institute of Technology Polytecnic, Yadrav: Unit 2 Swings
Sharad Institute of Technology Polytecnic, Yadrav: Unit 2 Swings
POLYTECNIC ,YADRAV
UNIT 2
SWINGS
2. Lightweight Components :
Most Swing components are lightweight. That means the
components are not dependent on native peers to render
themselves. Instead, they use simplified graphics primitives to
paint themselves on the screen and can even allow portions to be
transparent.
3. Pluggable Look and Feel :
It has a powerful component that can be extended to
provide the support for the user interface that helps in good look
and feel to the application. Swing support several look and feels.
Swing allows the user to switch look and feel at runtime without
closing the current application. To set the appearance of the applets
and applications to one of the three standard looks : windows,
Motif(Unix), or Metal(the standard Swing look).
4. MVC:
Swing mainly follows the concept of MVC that is Model
View Controller. With the help of this, we can do the changes in
one component without impacting or touching other components.
It is known as loosely coupled architecture as well.
Additional Features of Swings :
Several other features distinguish Swing from the older AWT components:
AWT SWING
AWT Does not support pluggable Swing Supports pluggable look and
look and feel feel
AWT components need the java.awt Swing components in Java need the
package. javax.swing package.
1. Frame()
2. Frame(String title)
Methods of Frame class :
Methods Description
void setResizable(boolean resizable) sets frame to resizable.
void setSize(int width,int height) sets the width and height of a frame.
1. ImageIcon(String name)
2. ImageIcon(URL url )
3. int getIconHeight()
4. int getIconwidht()
5. void paintIcon(Component comp, Graphics g, int x, int y)
JLabels :
The instance of Jlabel class creates labels, used to provide
instructions and information.
User can not change this text.
Constructors of Jlabel :
1. Jlabel(ImageIcon obj)
2. JLabel(String text)
3. JLabel(String text, ImageIcon obj, int alignment )
Methods :
4. Void setIcon(ImageIcon obj)
5. ImageIcon getIcon()
6. void setText(String text)
7. String getText()
JTextField :
1. JTextField()
2. JTextField(String text)
3. JTextField(int max_chars)
4. JTextField(String text, int max_chars)
JTextArea :
1. JTextArea()
2. JTextArea(String text)
3. JTextArea(int rows, int columns)
4. JTextArea(String text, int rows. Int columns)
5. JTextArea(Document doc)
6. JTextArea(Document doc, String text, int rows, int columns)
JCombobox :
Constructors of JComboBox :
1. JCombobox()
2. JCombobox(Vector obj)
2.3. Buttons :
1. Jbutton
2. JCheckBox
3. JRadioButton
JButtons :
Constructors of JButton :
1. JButton(String label)
2. JButton(ImageIcon obj)
3. JButton(String label, ImageIcon obj)
Methods of JButton :
Constructors of JCheckBox :
1. JCheckebox(ImageIcon obj)
2. JCheckebox(ImageIcon, boolean state)
3. JCheckebox(String text)
4. JCheckebox(String text, boolean state)
5. JCheckebox(String text, ImageIcon obj)
6. JCheckebox(String text, ImageIcon obj, boolean state)
Methods of JCheckBox :
Constructors of JRadioButton :
1. JRadioButton(ImageIcon obj)
2. JRadioButton(ImageIcon, boolean state)
3. JRadioButton(String text)
4. JRadioButton(String text, boolean state)
5. JRadioButton(String text, ImageIcon obj)
6. JRadioButton(String text, ImageIcon obj, boolean state)
• Radio buttons must be configured into a group.
Hence, only one of the radio button can be checked
from a group at any time.
• If user selects a radio button then automatically
previously selected radio button will get deselected.
• So, for this create object of ButtonGroup class with
default constructor.
• Use add() method of ButtonGroup class to add radio
button objects into a group.
Syntax is :
void add(JRadioButton obj)
2.4. Advanced Swing Components :
1. JTabbedPane
2. JScrollPane
3. JTree
4. JTable
5. JProgressBar
6. JToolTip
1. JTabbedPane :
• When user select a tab, its contents becomes visible and user can
interact its with the contents.
• Using the addTab method the tabs can be added to the tabbed pane.
• Syntax is,
void addTab(String str, Component comp)
Constructors of JTabbedPane :
1. JTabbedPane( )
2. JTabbedPane(int tabPlacement)
2. JTabbedPane(int tabPlacement) :
• It also provides facility to decide the tab layout policy. This allows the
programmer to control how the tabs will be displayed. The tab layout
policy is either set to JTabbedPane.SCROLL_TAB_LAYOUT or
JTabbedPane.WRAP_TAB_LAYOUT.
By default, the policy is set toWRAP_TAB_LAYOUT.
Constructors of JScrollPane :
1. JScrollPane(Component obj)
2. JScrollPane(int vertical, int horizontal)
3. JScrollPane(Component obj, int vertical, int horizontal)
Vertical and horizontal are int constants that defines
type of horizontal and vertical scrollbar defined in
ScrollPaneConstants interface. These constants
are:
1. HORIZONTAL_SCROLLBAR_ALWAYS
2. HORIZONTAL_SCROLLBAR_AS_NEEDED
3. VERTICAL_SCROLLBAR_ALWAYS
4. VERTICAL_SCROLLBAR_AS_NEEDED
Methods of JScrollPane :
1. setColumnHeaderView(Component)
2. setRowHeaderView(Component)
3. setCorner(String key, Component)
4. getCorner(Component)
1. setColumnHeaderView(Component) –
It sets the column header for the scroll pane of the specified
component in the parameters.
2. setRowHeaderView(Component) –
It sets the row header for the scroll pane of the specified component in
the parameters.
3. setCorner(String key, Component) –
It sets the corner for the scroll pane for the specified
component in the parameter. String key parameter is along with
the following:
JScrollPane.UPPER_LEFT_CORNER,
JScrollPane.UPPER_RIGHT_CORNER,
JScrollPane.LOWER_LEFT_CORNER,
JScrollPane.LOWER_RIGHT_CORNER,
JScrollPane.LOWER_LEADING_CORNER,
JScrollPane.LOWER_TRAILING_CORNER,
JScrollPane.UPPER_LEADING_CORNER,
JScrollPane.UPPER_TRAILING_CORNER
4. getCorner(Component) –
It gets the corner for the scroll pane for the specified
component in the parameter.
JTree :
Constructor Description
JTree() Creates a JTree with a sample model.
3. Public void
addTreeSelectionListener(TreeSelectionListener)–
Adds a listener in the tree selection in the Jtree panel.
JTable :
Constructors of JTable :
1. addColumn(TableColumn []column) :
adds a column at the end of the JTable.
2. clearSelection() :
Selects all the selected rows and columns.
2. void setString(String s) :
it is used to sets the value of the progress string.
4. int getMaximum() :
it returns the progress bar’s maximum value.
5. int getMinimum() :
it returns the progress bar’s minimum value.
6. String getString() :
it get the progress bar’s string representation of
current value.
7. void setMaximum(int n) :
sets the progress bar’s maximum value to the value n.
8. void setMinimum(int n) :
sets the progress bar’s minimum value to the value n.
JToolTip :
• ToolTip is very important component that shows
a temporary pop-up message when the mouse
pointer stays on a component.
• This pop-up message can be a short description
or labelled information of any component.
• We can add tooltip text to almost all the
components of java swing.
• You can create a tool tip for any JComponent
with setToolTipText() method. This method is
used to set up a tool tip for the component.
void setToolTipText(String text)
When the cursor enters the boundary of that
component a popup appears and text is displayed.
View Controller