Swing
Swing
• AWT is heavyweight i.e. its components are using the resources of OS.
• AWT contains numerous classes and methods that allows us to create and
manage windows.
• The main function/purpose of the AWT is to support applet windows, but it can
also be used to create standalone windows.
• The java.awt package provides classes for AWT api such as
• TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
• The javax.swing package provides classes for java swing API such as
• JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser
etc.
Swing adding rich graphics functionality and interactivity to Java applications.
Swing supports numerous look and feels, including the ability to create your
own look and feel.
• Features:-
• 1. swings is not a replacement for the AWT needed to support fully architecture
independent interfaces.
• 2. It contains more powerful components.
• 3. It provides with the components, which are not dependent on native source to
sender.
• 4. swings uses the Model-View-Controller (MVC) architecture as the fundamental
design behind each of its components.
• 5. well suited for complex GUI applications.
java.awt.* javax.swing.* 14
• JFrame • JCheckBox
• JPanel • JList
• JApplet • JComboBox
• JDialog • JProgressBar
• JLabel • Tool Tips
• JTextField • JSeperator
• JTextArea • JTabbedpane
• JButton • JTree
• JRadioButton • Jtable
• JToggleButton • JScrollPane
Applet
Applet
An applet is a small program that is intended not to be run on its own, but rather to be
embedded inside another application.
➤ The Applet class must be the superclass of any applet that is to be embedded in a Web page or
viewed by the Java Applet Viewer.
➤ The Applet class provides a standard interface between applets and their environment.
Applet is java program that can be embedded into HTML pages.
➤ Java applets run on the java enables web browsers such as Mozilla and internet explorer.
➤ Applet is designed to run remotely on the client browser, so there are some restrictions on it.
• Advantage of Applet
1. It works at client side so less response time.
2. Secured
3. It can be executed by browsers running under many platforms,
including Linux, Windows, Mac Os etc. Drawback of Applet
1. Plugin is required at client browser to execute applet.
Lifecycle of Java Applet
• Lifecycle of Java Applet
• Applet is initialized.
• Applet is started.
• Applet is painted.
• Applet is stopped.
• Applet is destroyed.
• public void start(); is invoked after the init() method or browser is maximized. It is
used to start the Applet.
• public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
• public void destroy(): is used to destroy the Applet. It is invoked only once.
• Component:- A component is an object having a graphical representation that can be
displayed on the screen and that can interact with the user.
• Examples of components are the buttons, checkboxes, and scrollbars of a typical graphical
user interface.
• The Component class is the abstract superclass of the nonmenu-related Abstract Window
Toolkit components.
• A lightweight component is a component that is not associated with a native opaque window.
• java.lang.Object
• java.awt.Component
• void setBackground(Color c)
• void setForeground(Color c)
• void setEnabled(boolean b)
• void setVisible(boolean b)
• void setFont(Font f)
• void setSize(Dimension d)
• void setLocation(int x, int y)
All but setSize and setLocation are overridden
by the JComponent class.
Adding Color
The java.awt.Color class has the following static
fields (data members):
• Color.black • Color.magenta
• Color.blue • Color.orange
• Color.cyan • Color.pink
• Color.darkGray • Color.red
• Color.gray • Color.white
• Color.green • Color.yellow
• Color.lightGray
Swing Widgets (more on this next lecture)
Top-Level Containers General-Purpose Containers
JDialog
JSplitPane
JTabbedPane
JButton
JFileChooser
JTree
JTable
JColorChooser 35
Notes on the Code
contentPane.add(label, BorderLayout.NORTH);
contentPane.add(panel, BorderLayout.CENTER);
New Output
contentPane.add(label, BorderLayout.NORTH);
contentPane.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
Content Panes
• Q: Why not just: frame.setBackground(Color.red); ?
• A: In order to be lightweight, Swing's top-level window objects must
be built on top of a lightweight AWT Container object introduced in
version 1.1
• This container is called a content pane
File Edit
Undo
Redo
Cut
rootPane:
JRootPane
• PALETTE_LAYER: This layer of JLayeredPanes its over the default layer and useful for
floating toolbars and palettes.
• MODAL_LAYER: This layer of JLayeredPane is used for model dialogs and shown above the
palette layer.
• POPUP_LAYER: This layer of JLayeredPane is used to show popup windows above the modal
layer.
• DRAG_LAYER: A component is shown in this layer (above the popup layer) while dragging it.
When the dragging is finished, the component is shown in its original layer.
• https://www.educba.com/jlayeredpane/
http://www.iitk.ac.in/esc101/05Aug/tutorial/uiswing/components/layeredpane.html
Swing Hello World
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
frame.setVisible(true);
}
} 94
Different Kinds of Swing Events
Low-level events
• MouseEvent: Component got mouse-down, mouse-move, etc.
• KeyEvent: Component got key-press, key-release, etc.
• ComponentEvent: Component resized, moved, etc.
• ContainerEvent: Container's contents changed because a component was added or removed
• FocusEvent: Component got focus or lost focus
• WindowEvent: Window opened, closed, etc.
JLayeredPane JPanel
JDesktopPane JScrollPane
JInternalFrame
JTable
JTree
JComboBox
Swing provides a combo box(a combination of a text field and a dropdown list)
through the Jcombobox class,which extends Jcomponent.
However,it can also display a drop down list that allows user to select a different
entry.
User can make selection by clicking at an item from the list and typing into the box.
Constructors
•
1) JComboBox()
2) public JComboBox(ComboBoxModel asModel) takes its items from an existing ComboBoxModel.
3) public JComboBox(object[] items) Create a JComboBox that contains the elements of the specified
array
Method:
1)void addItem(Object obj)
• Methods
•
1)public void setEditable(boolean aFlag) --- It determine JComboBox field is editable or not
•
2)public Boolean isEditable() -- it returns true if JComboBox is editable. Bydefault Combobox is not
editable
•
3)public void setMaximumRowCount(int count) -- it sets the maximum no of rows the JComboBox
displays , if the no of object in the model is greater than count the comboBox uses ScrollBar
4)public void setSelectedItem(Object anObject):
frame.setVisible(true);
}
}
New Display
Sizing Hints
• Layout managers often need to resize their components to make things fit
• For example, the widths and heights of components in a BoxLayout are adjusted
according to both preferred and maximum heights and widths
• If you don't like the size of the components a layout manager comes up with, you may
have to give sizing hints using the following methods from the JComponent class:
• void setMinimumSize(Dimension d)
• void setPreferredSize(Dimension d)
• void setMaximumSize(Dimension d)
13.5 TextFields
• JTextField
• Single-line area in which user can enter text
• JPasswordField
• Extends JTextField
• Hides characters that user enters
Thank you