Download Read More - Myknowledge

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Fundamental of Java Programming
(630002)
Unit – 5
GUI with Swing
Introduction
After learning AWT, lets now see what's
Swing? Well, Swing is important to develop Java
programs with a graphical user interface (GUI).
Modern operating systems such as Microsoft’s
Windows, Apple’s MacOS, and the different Unixbased versions such as Linux or Solaris use a
graphical interface to communicate with the user.
The communication consists of information
displayed by programs and actions and commands
issued by the user.
Introduction
There are mainly two libraries for graphics
components in Java: the Abstract Windowing
Toolkit (AWT) and Swing. The first is the older one.
It contains all the components needed to design
graphical user interfaces. However, using AWT is
not easy and the library is not free of bugs. The
components of the Swing library are easier to work
with and are much better implemented.
Introduction
Swing was not there in the early days of java
but it is a response to deficiency present in the
Java’s original GUI system i.e. Abstract Window
Toolkit (AWT). The AWT defines the basic set of
controls. Windows, dialogboxes etc. One reason
for limited nature of awt. It translates it’s various
visual components into their corresponding
platforms. This means that the basic Look and Feel
of component is defined by the platform and not by
the Java. So that they are referred as Heavy Weight
Components.
Types of Components
There are mainly two types of components
(1) Heavy Weight Components
(2) Light Weight Components
The main difference between this two are
heavy weight components are Platform Dependent
while Light Weight Components are Platform
Independent.
Heavy weight components have various
problem which are as follows :
Drawbacks of Heavy Weight Comp.
(1) Because of variation in the O.S., the look, feel
and work of component may be different for
different platform.
(2) The look and feel of the components will be
change from OS to OS
(3) We know that all heavy weight components are
in rectangular shape.
Important
After introduction of java in 1991, the java
developers found limitation of AWT and they have
introduce Swing in 1997.
One important point that we have to
remember is even though swing eliminate a no. of
limitations of AWT but it doesn’t replace it.
Because swing is build in foundation of AWT, that’s
why AWT is crucial part of java even today.
Features of Swing
(1) Light Weight Components
Swing Components are light weight this
means that they are written directly in java and
doesn’t depend on any platform. So they can be
transparent and even they allow us to give nonrectangular shapes.
The light weight components are more
flexible and efficient. Here most important thing is
the look and feel of component will not determined
by the OS but determined by the java itself.
Features of Swing
(2) Pluggable Look and Feel (PLAF)
Swing components supports PLAF because
they are light weight components. Here look and
feel of component will decide by java and not by
the OS.
Even it is possible to give new look and feel to
the component with the help of swing
programming.
MVC Architecture
MVC
(Model-View-Controller)
is
the
architecture used by Swing components
In general any visual component has 3 aspects
(1) The way components looks on the screen
(2) The way the component react to the user
(3) The state of information associated with
component.
In programming field whatever architecture
you may use to implement component, it must
have all these 3 parts.
MVC Architecture
Since beginning of programming there is one
component architecture that has proven it self
effective is Model View Controller (MVC)
In MVC terminology the model corresponds to
the state of the information associated with
component. E.g. in case of checkbox model
indicates value of checkbox i.e. either true or false.
The view determines how the component will
display on screen, and controller determine how
component react to the user.
MVC Architecture
Even though MVC architecture and principles
behind it are perfect but high level of separation
between the view and controller doesn’t supported
by the swing. So swing uses modified version of
MVC which is known as Model Delegate
Architecture or Separable Model Architecture.
The swing’s pluggable look and feel is possible
by view (Look) and Controller (Feel)
Difference between Swing and AWT
AWT
Swing
Components are Heavy Weight
Components are Light Weight
Platform Dependent
Platform Independent
No support for MVC
MVC Supported
AWT is base for Swing
Swing is based on AWT
AWT is primary version of Java
GUI
Swing is Advance version of Java
GUI
AWT is applet
Swing is Applicaiton
Components and Containers
Swing consist of two main items.
Components and containers. The term component
is an independent visual control like push button or
text box. On other hand a container holds a group
of components.
All the swing components derived from
Jcomponent and Jcontainer classes. Both of these
classes are part of package javax.swing.
List of Components
JApplet
JButton
JCheckBox
JCheckBoxMenuItem
JColorChooser
JComboBox
JComponent
JDailog
JFileChooser
JFrame
JLabel
JList
JMenu
JMenuBar
JMenuItem
JOptionPane
JPanel
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JSeprator
JSlider
JTable
JTextArea
JTextField
JToggleButton
JToolBar
JToolTip
JTree
JWindow
Out of these components JFrame, JApplet,
JWindow and JDialog are containers.
JFrame
The frame in java works like the main window
where your components (controls) are added to
develop an application. In the Java Swing, top-level
windows are represented by the JFrame class. Java
supports the look and feel and decoration for the
frame.
ex\swing\FrameDemo.java
ex\swing\FrameClosing.java
ex\swing\SettingIconFrame.java
ex\swing\SwingFrameNonResizable.java
ex\swing\RemoveTitleFrame.java
JLabel
The Label in Swing works same as the AWT
Label.
ex\swing\Label.java
JTextField
The TextField in Swing works same as the AWT
TextField.
ex\swing\LoginDemo.java
ex\swing\NextPage.java
JTextArea
The JTextArea works same as TextArea of the
AWT. But here the main difference is in AWT we
can set the scrollbars when the text will not fit into
the TextArea then TextArea will automatically
grown.
ex\swing\TextAreaExample.java
JButton
JButton component will work same as AWT
button. The main difference is we can add Icons on
the buttons as well as we can write multiple lines in
the caption of button
ex\swing\ChangeButtonLabel.java
ex\swing\MultilineLabelButton.java
ex\swing\IconButton.java
New Feature like Absolute Coordinate
Swing supports a new and unique feature
through which we can specify the absolute
coordinates to any component that we want to add
into the frame.
ex\swing\AbsCoordinate.java
Use of HTML inside Swing components
In Swing we are allowed to use HTML tags
while creating various Swing Components. With
the help of using HTML tags while creating Swing
components we are allowed to set various fonts /
styles to all swing components
ex\swing\HtmlDemo.java
JCheckBox
JCheckBox component will work same as AWT
CheckBox. The main difference is we can add Icons
on the Checkbox also
ex\swing\CreateCheckBox.java
ex\swing\CustomizedCheckBox.java
JRadioButton
JRadioButton component will work same as
AWT RadioButton. The Main difference here is
Swing has given us a special class for radio button
while there in AWT we have the common class for
CheckBox and RadioButton
ex\swing\CreateRadioButton.java
ex\swing\SelectRadioButton.java
JList
JList component will work same as AWT List.
The Main difference here is this list can be directly
initialized through the array and the second
difference is scrollbar will not be automatically
placed in JList.
ex\swing\CreateJList.java
JComboBox
JComboBox component will work same as
AWT ComboBox. The Main difference here is this
ComboBox can be directly initialized through the
array.
ex\swing\ComboBox.java
ex\swing\AddRemoveItemFromCombo.java
Dialog Boxes in Swing
A Dialog window is an independent
subwindow meant to carry temporary notice apart
from the main Swing Application Window. Most
Dialogs present an error message or warning to a
user.
For convenience, several Swing component
classes can directly instantiate and display dialogs.
To create simple, standard dialogs, you use the
JOptionPane class.
ex\swing\ShowDialogBox.java
ex\swing\ShowMessageDialog.java
ex\swing\ShowInputDialog.java
JSlider
A JSlider component is intended to let the
user easily enter a numeric value bounded by a
minimum and maximum value. If space is limited, a
spinner is a possible alternative to a slider.
ex\swing\CreateSlider.java
JProgressBar
Sometimes a task running within a program
might take a while to complete. A user-friendly
program provides some indication to the user that
the task is occurring, how long the task might take,
and how much work has already been done. One
way of indicating work, and perhaps the amount of
progress, is to use an animated image.
ex\swing\SwingProgressBar.java
JSpinner
Spinners are similar to combo boxes and lists
in that they let the user choose from a range of
values. Like editable combo boxes, spinners allow
the user to type in a value. Unlike combo boxes,
spinners do not have a drop-down list that can
cover up other components. Because spinners do
not display possible values — only the current value
is visible — they are often used instead of combo
boxes or lists when the set of possible values is
extremely large. However, spinners should only be
used when the possible values and their sequence
are obvious.ex\swing\JSpinnerComp.java
JTable
With the JTable class you can display tables of
data, optionally allowing the user to edit the data.
JTable does not contain or cache data; it is simply a
view of your data.
ex\swing\JTableComponent.java
ex\swing\SetColumnHeader.java
Shape Drawing like AWT
It is also possible to draw various shapes like
rectangle, oval, lines etc. just like AWT Shapes.
Here swing has given same methods for drawing of
these shapes.
ex\swing\DrawingColor.java
JScrollbar
JScrollBar will work same as the AWT
Scrollbar. There is no change. This component will
work as it is.
ex\swing\Scrollbars.java
JMenu
JMenu are same as AWT Menu. Here it will
use various classes like JMenuBar, Jmenu,
JMenuItem,
JCheckBoxMenuItem,
JRadioButtonMenuItem etc.
Here a new feature that has been added to
the JMenu is we can add images to the menu item
also.
ex\swing\MenuLookDemo.java
ex\swing\MenuDemo.java
ex\swing\SwingMenu.java
ex\swing\PopUpMenu.java
JTree
Jtree is one of new classes provided by Swing.
This class is useful to display hirarchical data. Here
we have to define tree node interface and in that
we have to add various nodes and then all the
nodes will be added to the JTree object.
ex\swing\TreeComponent.java
ex\swing\swingtree.java
JPasswordField
The JPasswordField class, a subclass of
JTextField, provides specialized text fields for
password entry. For security reasons, a password
field does not show the characters that the user
types. Instead, the field displays a character
different from the one typed, such as an '*'. As
another security precaution, a password field stores
its value as an array of characters, rather than as a
string. Like an ordinary text field, a password field
fires an action event when the user indicates that
text entry is complete, for example by pressing the
Enter button. ex\swing\PasswordDemo.java
JPanels
The JPanel class provides general-purpose
containers for lightweight components. By default,
panels do not add colors to anything except their
own background; however, you can easily add
borders to them and otherwise customize their
painting. Details can be found in Performing
Custom Painting.
ex\swing\DemoDrawing2.java
JtabbedPane
JTabbedPane will allows us to create a page
with tabs. Tabs will gives us facility to create
multiple pages on a single frame.
ex\swing\CreateTabbedPane.java
JColorChooser
Use the JColorChooser class to enable users to
choose from a palette of colors. A color chooser is a
component that you can place anywhere within
your program GUI. The JColorChooser API also
makes it easy to bring up a dialog (modal or not)
that contains a color chooser.
ex\swing\ColorChooserDemo.java
JFileChooser
File choosers provide a GUI for navigating the
file system, and then either choosing a file or
directory from a list, or entering the name of a file
or directory. To display a file chooser, you usually
use the JFileChooser API to show a modal dialog
containing the file chooser. Another way to present
a file chooser is to add an instance of JFileChooser
to a container.
ex\swing\FileChooserDemo.java
Pluggable Look and Feel
The look and feel feature of Java Swing
provides more interactivity of the frame for user
application. Swing allows to change the Look and
Feel of the application on the fly.
ex\swing\GettingAndSettingLAF.java
Thank You