Swing
Swing:-
Swing is a Java Foundation
Classes [JFC] liberary and an extension of the Abstract Window Toolkit
[AWT],new components, expanded components features, excellent event handling
with drag and drop support.
Swing Components:-
Swing is a
package built on top of the AWT that provides a great number of pre built
classes [over 250 classes and 40 UI components].
Swing Components
|
Features
|
JApplet
|
An extended version
of java.applet.Applet that adds support for
root panes and other panes
|
JButton
|
A command button
|
JCheckBox
|
A checkbox that can
be user selected or deselected and displays its state visually on the GUI
|
JCheckMenuItem
|
A menu item that
can be select or deselect and display its state visually on the GUI
|
JComboBox
|
A combo box, which
is a combination of a text field and drop down list of selectable items
|
JDialog
|
The base class for
creating a dialog window
|
JFrame
|
An extended version
of java.awt.Frame
That adds support
for root panes and other panes
|
JLable
|
A display area that
can hold a short text message, an image or both normally placed adjacent to a
textbox as its prompt
|
JList
|
A component that
allows the user to select one or more objects from a list
|
JMenu
|
A pop up menu
containing JMenuItem that is displayed when the user selects it in the
JMenuBar component
|
JMenuBar
|
An implementation
of a menu bar
|
JMenuItem
|
An implementation
of a menu item
|
JOptionPane
|
Make it easy to pop
up a standard dialog box
|
JPanel
|
A generic
Lightweight container
|
JPasswordFeild
|
Allow editing of a
single line of text where the view does not show the original characters
|
JPopupMenu
|
A pop up menu
|
JPopupMenuSeparator
|
A pop-up
menu-specific separator
|
JProgressBar
|
A component that
displays an integer value within an interval
|
JRadioButton
|
A radio button that
can be selected or deselected, displaying its state visually
|
JRadioButtonMenuItem
|
A radio button menu
item
|
JRootPane
|
The fundamental
component in the container hierarchy
|
JScrollBar
|
An implementation
of a scrollbar
|
JScrollPane
|
A container that
manages a viewport, optional vertical and horizontal scrollbars and optional
row and column heading viewports
|
JSlider
|
A component that
lets the user select a value by sliding a knob within an interval
|
JTabbedPane
|
Lets the user
switch between a group of components by clicking tabs
|
JTable
|
Presents data in a
two-dimensional table format
|
JTextArea
|
A multi line area
that accepts and displays user input as plain text
|
JTextFeild
|
A single line area
that accepts and displays user input as plain text
|
JtextPane
|
A text component
that can be marked up with attributes that are representes graphically
|
JToolBar
|
A toolbar, used for
displaying commonly used controls as icon in a the GUI of a commercial
application
|
JTree
|
Display a set of
hierarchical data as an tree outline
|
Comparison between swing and AWT:-
The biggest difference between the AWT and swing components is that
swing components are implemented totally in java. Swing components have more
functionality than AWT components.
Working with swings
JFrame:
A swing frame is a container that
functions as the main window for applications that use swing components. A
swing frame has a title, a border and buttons for iconofying, maximizing and
closing frame.
The JFrame
class is subdivided into several different panes. The main pane is the content
pane which represents the full area of a frame into which components can be
added.
Methods of JFrame:
Methods
|
Description
|
Void
setContentPane(Container)
Container
getContentPane()
|
Set or get the
frame’s content pane. The content pane contains the frame’s visible GUI
components. The content pane is opaque.
|
JRootPane
creatRootPane()
Void
setRootPane(JRootPane)
JRootPane
getRootPane()
|
Create, set or get
the frame’s root pane. The root panel manages the interior of the frame including
the content pane, the glass pane and so on.
|
Void
setGlassPane(Component)
Component
getGlassPane()
|
Set or get the
frame’s glass pane.Use the frame’s layered pane to put components on top or
behind other components
|
Void
setLayeredPane(JLayeredPane)
JLayeredPane
getLayeredpane()
|
Set or get the
frame’s layered pane. Use the frame’s layered pane to put components on top
of or behind other components.
|
Example1:
Import javax.swing.*;
Import
java.awt.event.*;
Import java.awt.*;
Public class
HelloworldFrame extends JFrame
{
Public static void
main(String[]args)
{
JFrame frm =new
JFrame(“welcome”);
Container container
=frm.getContentPane();
Container.add(new
JLable(“Hello World”, JLable.CENTER));
Frm.setSize(200,200);
Frm.setVisible(true);
WindowListener listener
= new WindowAdaper()
{
Public void
windowClosing(WindowEvent winEvt)
{
System.exit(0);
}
};
Frm.addWindowListener(listener);
}
}
JPanel:-
A JPanel is a Swing container [a
component extension of JComponent] that is used for grouping components within
one area of an applet or a frame. A panel can also be used to group other
panels.
Example2:
Import javax.swing.*;
Import
java.awt.event.*;
Import java.awt.*;
Public class
MasterForm extends JFrame
{
Public MasterForm()
{
super(“Address
Book”);
JButton btnAdd =new
JButton(“Add”);
JButton btnEdit =new
JButton(“Edit”);
JButton btnDelete
=new JButton(“Delete”);
JButton btnView=new
JButton(“View”);
JPanel pnlMenu =new
JPanel();
pnlMenu.add(btnAdd);
pnlMenu.add(btnEdit);
pnlMenu.add(btnDelete);
pnlMenu.add(btnView);
btnAdd.setMnemonic(‘a’);
btnEdit.setMnemonic(‘e’);
btnDelete.setMnemonic(‘d’);
btnView.setMnemonic(‘v’);
btnAdd.setToolTipText(“To
add new contents”);
btnEdit.setToolTipText(“To
edit existing contents”);
btnDelete.setToolTipText(“To
delete existing contents”);
btnView.setToolTipText(“To
view existing contents”);
setContentPane(pnlMenu);
}
public static void
main(String[]args)
{
MasterForm frmMaster
=new MasterForm();
frmMaster.setSize(300,200);
frmMaster.setVisible(true);
WindowListener
listener = new WindowAdaper()
{
Public void
windowClosing(WindowEvent winEvt)
{
System.exit(0);
}
};
Frm.addWindowListener(listener);
}
}
Swings Basic Containers:
These components are the content pane, layered pane, glass pane and an
optional menu bar.
In addition to this,
the root pane is a virtual container that contains all four of them.
Root Pane:
Swing containers such as
JApplet, JFrame, jWindow delegates their duties to the root pane represented by
the class JRootPane. Root pane cannot have any children class. Thus, GUI
Components cannot add directly.
JButton Method:
Methods
|
Description
|
Boolean
isDefaultButton()
|
Sets the button as
the default button in the GUI.
|
void updateUI()
|
Used to update the
look and feel of the application’s GUI
|
JLabel Methods:
Methods
|
Description
|
void
setHorizontalAlignment(int)
void
setVerticalAlignment(int)
int
getHorizontalAlignmen()
int
getVerticalAlignmen()
|
The set or get
specifies where in the lable its contents should be placed. The
SwingContantsinterface define five possible values for horizontal alignment:
LEFT[default for text only labels], CENTER[default for image only labels],
RIGHT, LEADING and
TRAILING.For
Vertical alignment:
TOP, CENTER[default], BOTTOM
|
Void
setIconTextGap(int)
Int
getIconTextGap()
|
Sets or gets the
number of pixels between the labels text and its images
|
Icon getIcon()
|
Returns an icon
associated with the image
|
Void setIcon(Icon)
|
Sets an icon to an
lable
|
String getText()
|
Return the text
string that the lable display
|
Void
setText(String)
|
Defines a single
line of text the lable will display
|
Void
setDisplayedKeyAcceletor(char)
|
Specifies a
character that indicates the shortcut key
|
Char
getDisplayedKeyAccelerator()
|
Returns the char
that indicates the shortcut key
|
Example3:
Import javax.swing.*;
Import
java.awt.event.*;
Import java.awt.*;
Public class Login
extends JPanel
{
Public Login()
{
Icon imgIcon =new
ImageIcon(“Images/security.jpg”);
JLabel lbllnValid
=new JLable(“The Login Username or password is not
valid.”,SwingConstant.RIGHT);
add(lblReEnter);
}
Public static void
main(String args[])
{
JFrame frm =new
JFrame(“Invalid Login”);
Login lblLogin =new
Login();
Frm.getContentPane().add(“Center”,lblLogin);
Frm.setSize(400,200);
Frm.setVisible(true);
WindowListener
listener = new WindowAdaper()
{
Public void
windowClosing(WindowEvent winEvt)
{
System.exit(0);
}};
}
}
Text Field:
A text field is a component
that lets a user enter a small amount of text, normally used with GUI based
data entry screen in commercial application or Applets.
JTextField Method:
Methods
|
Description
|
Void
setEditable(Boolean)
Boolean
getEditable()
|
Sets or gets
whether the user can edit the text in the text field
|
Void setColumns(int)
Int getColumns()
|
Sets or gets the
number of columns displayed by the text field
|
Int
getColumnWidth()
|
Get the width of
the text field’s columns
|
Void
setHorizontalAlignment(int)
Int
getHorizontalAlignment()
|
Set or get how the
text is aligned horizontally within its area. One can use JTextField.LEFT,
JTextField.CENTER and JTextFeild. RIGHT for aligning text in the text field.
|
Void
setEchoChar(char)
Char getEchoChar ()
(in JPasswordField)
|
Set or get the echo
character – the character displayed instead of the actual characters typed by
the user
|
Example4:
Import javax.swing.*;
Import
java.awt.event.*;
Public class Login
extends JPanel
{
JLable lblUserName;
JLable
lblUserPassword;
JTextField
txtUserName;
JpasswordField
txtPassword;
Public Login()
{
lblUserName =new
JLable(“Username:”);
txtUserName =new
JTextField(10);
lblUserPassword =new
JLable(“UserPassword:”);
txtUserPassword =new
JPasswordField(10);
txtPassword.setEchoChar(‘*’);
txtUserName.setToolTipText(“Enter
Username”);
txtPassword.setToolTipText(“Enter
Password”);
add(lblUserName);
add(txtUserName);
add(lblUserPassword);
add(txtlUserPassword);
}
Public static void
main(String[]args)
{
Login frmLogin =new
Login();
JFrame frm =new
JFrame(“Login Form”);
Frm.setContentPane(frmLogin);
Frm.setSize(200,150);
Frm.setVisible(true);
WindowListener
listener = new WindowAdaper()
{
Public void
windowClosing(WindowEvent winEvt)
{
System.exit(0);
}
};
Frm.addWindowListener(listener);
}
}
Text Areas:
A text area is a text
control used in a GUI that lets a user enter multiple lines of text.
JTextArea Methods:
Methods
|
Description
|
Append(String)
|
Appends the given
string to the end of the document
|
Void getColumns(int)
Int getRows(int)
|
Returns the number
of columns and rows in the text area.
|
Insert(String,int)
|
Inserts a specified
text at the specified position in the text area
|
Void setRows(int)
|
Sets the number of
rows for the text area
|
Void setFont(Font)
|
Sets a specific
font for the text
|
Example5:
Import javax.swing.*;
Import
java.awt.event.*;
Public class
AddressDetails extends JPanel
{
JLable lblFirstName,
lblLastName, lblAddress;
JTextFeild txtFirestName,
txtLastName;
JTextArea
txtareaAddress;
Public
AddressDetails()
{
setLayout(null);
lblFirstName =new
JLable(“First Name:”);
lblLastName =new
JLable(“Last Name:”);
lblAddress =new
JLable(“Address:”);
txtFirstName =new
JTextFeild(15);
txtLastName =new
JTextFeild(15);
txtareaAddress =new
JTextArea(10,10);
lblFirstName.setBounds(10,20,120,25);
txtFirstName.setBounds(150,20,210,25);
lblLastName.setBounds(10,60,180,25);
txtLastName.setBounds(150,60,210,25);
lblAddress.setBounds(10,100,250,25);
txtareaAddress.setBounds(150,100,280,100);
add(lblFirstName);
add(txtFirstName);
add(lblLastName);
add(txtLastName);
add(lblAddress);
add(txtareaAddress);
}
Public static void
main(String[]args)
{
AddressDetails
frmAddress =new AddressDetails();
JFrame frm =new
JFrame(“Address Details”);
frm.setContentPane(frmAddress);
frm.setSize(500,300);
frm.setVisible(true);
WindowListener
listener = new WindowAdaper()
{
Public void
windowClosing(WindowEvent winEvt)
{
System.exit(0);
}
};
Frm.addWindowListener(listener);
}
}
JCheckBox Methods:
Methods
|
Description
|
String getLabel()
Void
setLabel(String)
|
Sets or gets a
label for a check box
|
Boolean getState()
Void
setState(Boolean)
|
Sets or gets the
status of the check box
|
Example6:
Import javax.swing.*;
Import
java.awt.event.*;
Public class Hobbies
extends JPanel
{
JCheckBox chkMusic,
chkDancing, chkReading, chkStamp, chkCoins, chkDriving;
Public Hobbies()
{
chkMusic =new JCheckBox(“Music”,true);
chkDancing =new
JCheckBox(“Dancing”,false);
chkReading =new
JCheckBox(“Reading”,false);
chkDriving =new JCheckBox(“Driving”, false);
chkStamp =new JCheckBox(“Collecting
Stamp”, false);
chkCoins =new JCheckBox(“Collecting Old
Coins”, false);
add(chkMusic);
add(chkDancing);
add(chkReading);
add(chkDriving);
add(chkStamp);
add(chkCoins);
}
Public static void
main(String[] args)
{
Hobbies frmHobbies
=new Hobbies();
JFrame frm =new
JFrame(“Hobbies”);
Frm.setContentPane(frmHobbies);
frm.setSize(300,150);
frm.setVisible(true);
WindowListener
listener = new WindowAdaper()
{
Public void
windowClosing(WindowEvent winEvt)
{
System.exit(0);
}
};
Frm.addWindowListener(listener);
}
}
I hope after reading this article now you pupil understood how Net Beans IDE or Eclipse IDE made our programming life simple.
So enjoy GUI programming with NET BEANS IDE.
CBSE private form
ReplyDelete12th CBSE Online Admission form
NIOS 12th Admission
10th CBSE Correspondence Admission
12th CBSE Patrachar admission form
12th fail Admission in Nios stream 2
CBSE Improvement Exam Form 2020 Class 12th,
patrachar vidyalaya ,nios classes timings for 10th,12th
NIOS Previous years papers for class 10th
12th Admission CBSE Patrachar
ReplyDelete12th Admission CBSE Patrachar
12th Admission CBSE Patrachar
12th Admission CBSE Patrachar
12th admission CBSE Patrachar