Chapter4 New
Chapter4 New
• Container is responsible for laying out the any components that it contains.
2. public void setSize(int width, int height): Sets the size of the component.
3. public void setLayout(LayoutManager lm): Sets the layout to set the components in
a particular manner.
• The window is the container that have no borders and menu bars. You must
use frame, dialog or another window for creating a window.
• Simply window can contain other components, such as buttons, labels, and
text fields.
Panel
• The Panel is the container that doesn't contain title bar, border or menu bar.
It is generic container for holding the components.
Frame:
• It is subclass of Window
• The Frame is the container that contains the title bar, border
and can have menu bars.
Dialog:
• A dialog box is a temporary window an application creates to
retrieve user input.
AWT Controls
The component which are allow to interact with applications.
The AWT controls as follows;
• Labels
• Button
• Checkbox
• Checkbox group
• Scrollbars
• Text field
• Text Area
Label
A Label object is a component for placing text in a container. The three types of Label
constructors are as follows;
• Label(String str, int x): Create a label with the specified string and x as the specified
alignment. By default alignment is Left. Valid values for x are Left, Right and Center.
e.g. Label l1=Label(“User Name”, Label.Center);
Label
• void setText(String str): Used to sets the text of the label.
• void setAlignment(int how) : Used to sets the alignment of text in the label.
• int getItemCount() • It removes all the items from the choice menu.
• It returns the number of items of the choice menu. • void select(int pos)
• int getSelectedIndex() • It changes / sets the selected item in the choice menu to the
• Returns the index of the currently selected item. item at given index position.
• Adds the specified item to the end of the list. • Returns the index of the first selected item in a multiple-
selection list.
• void add(String item, int index)
• int[] getSelectedIndexes()
• Used to insert the given item to the given index in the
• Returns an array of the selected item indexes in a multiple-
list.
List
• String getSelectedItem() • void remove(String item)
• Returns the selected item in a single-selection list. • Used to remove the first occurrence of the
• String[] getSelectedItems() given item from the list.
• Lightweight : Java Swing provides a pluggable look and feels that allows developers to
customize the appearance of the GUI according to the user’s preferences.
• Pluggable look and feel : Java Swing provides a set of lightweight components that are
easy to use and customizable.
• Rich controls : Swing provides a rich set of advanced controls like Tree TabbedPane,
slider, colorpicker, and table controls.
Difference Between AWT and Swing
Icons and Labels
• Icons:
• In swing icons are encapsulated by the ImageIcon class which paints an icon from an image.
• The constructors of Icon are:
• ImageIcon(String filename) : Creates an ImageIcon from the specified file.
• ImageIcon(URL location) : Creates an ImageIcon from the specified URL.
• JLabel:
• JLabel creates an label used to provide the information and instruction.
• It is used to display a single line of read only text (Passive Component).
• The constructors of JLabel are:
• JLabel() : creates a new label with the empty string.
• JLabel(Icon i) : creates a new label with a image on it which nothing but icon.
• JLabel(String s, Icon i, int alignment) : creates a new label with a string, an image and a specified alignment as LEFT,RIGHT and
CENTER.
JButton JButton Methods
JButton component has a label and generates an event when pressed.
void setIcon(ImageIcon obj) : It is used to set
JButton can also have an Image.
the specified Icon on the button.
It inherits or derived from AbstractButton class.
The JButton class is used to create a labeled button that has platform ImageIcon getIcon() : It is used to get the Icon
independent implementation. of the button.
• JButton() : It creates a button with no text and icon(image).
void setText(String label) : It is used to set
• Example JButton jb=new JButton();
• JButton(String label) : It creates a button with the specified specified text on button.
text or string. String getText() : It is used to return the text of
• Example JButton jb=new JButton(“Button1”);
the button.
• JButton(ImageIcon obj) : Creates a button with an
icon(Image). void setEnabled(Boolean b) : It is used to set
• JButton(String label, ImageIcon obj) : Creates a button with the button enable or disable by using boolean
an initial text and an Image icon. value true or false.
JTextField
• The class JTextField is a component that allows editing of a single line of text.
JTextField(int columns) : It creates a new empty TextField with specified number of columns.
JTextField(String text) : It creates a new empty text field initialized with the given string.
JTextField(String text, int columns) : It creates a new empty TextField with the given string
and a specified number of columns.
JTextArea
• JTextArea is a part of java Swing package .
• It represents a multi line area that displays text.
• It is used to edit the text .
• JTextArea inherits JTextComponent class and super class is JComponent.
JTextArea(int row, int column) : It creates a new text area with a given number of rows and columns.
JTextArea(String s, int row, int column) : It is used to constructs a new text area with a given number of
JTextArea(Document doc) : Constructs a new JTextArea with the given document model
JTextArea(Document doc, String s, int row, int column) : Constructs a new JTextArea with the specified
• JCheckbox Constructors:
• JCheckBox(ImageIcon obj, boolean state) : It is • String getText() : It returns current text of the checkbox.
used to creates a new checkbox with the icon and • void setIcon(Icon i) : It sets the icon(image) to the
the state value specifies whether it is selected or not. checkbox by the given icon.
• JCheckBox(String text, ImageIcon obj) : It • ImageIcon getIcon() : It returns the image of the
checkbox.
creates a new checkbox with the string and the
image icon. • void setSelected(boolean state) : It sets the checkbox to
be selected if boolean value is true or not selected if
• JCheckBox(String text, ImageIcon obj,Boolean
boolean value is false.
state) : It creates a new checkbox with the text as
• boolean isSelected(): It is used to check the item is
string and the icon and the state value specifies
selected or not.
whether it is selected or not.
JRadioButton
• The JRadioButton class is used to create a radio button.
• JComboBox shows a popup menu that shows a list and the user can select a option from
that specified list.
JComboBox Constructors:
JComboBox() : It creates a new empty JComboBox .
JComboBox(Vector items) : It creates a new JComboBox with items from the specified
vector(to select different items of different datatypes) or same types of data.
• To add the new items in JComboBox we can use the addItem() method.
Advanced Swing Components
• JTabbedPane
• JScrollPane
• JTree
• JTable
• JProgressBar
• Tool Tips
JTabbedPane
• JTabbedPane is a component which appears as a group of multiple tabs, each containing a title ,panel and group of
components.
• When user can click on a tab, only data related to that tab will be displayed and user can interact with it.
• In simple JTabbedPane means same frame can be shared among the different layouts.
JTabbedPane(int tabPlacement) : Creates an empty TabbedPane with a specified tab placement(Location).The value of
JScrollPane
• JScrollPane is a component in the Java • JScrollPane(Component comp) : This constructor creates a
JScrollPane with the specified view component as the scrollable
Swing library that provides a scrollable view of
content.
another component, usually a JPanel or a
• JScrollPane(int vertical, int horizontal) : This constructor creates
JTextArea. an empty JScrollPane with the specified vertical and horizontal
scrollbar.
• A ScrollPane is a Container. A ScrollPane handles
• JScrollPane(Component comp ,int vertical, int horizontal) :
its own events and performs its own scrolling.
This constructor creates an JScrollPane with the specified
• When screen size is limited, we use a scroll pane to component with vertical and horizontal scrollbar.
display a large component or a component whose • The horizontal and vertical scrollbar are defined in
ScroolPaneConstants interface and theses constants are:
size can change dynamically.
1. HORIZONTAL_SCROLLBAR_ALWAYS
• JScrollPane class which
2. HORIZONTAL_SCROLLBAR_AS_NEEDED
extends JComponent class. 3. VERTICAL_SCROLLBAR_ALWAYS
4. VERTICAL_SCROLLBAR_AS_NEEDED
•
JTable
• The JTable class is used to display data in tabular form.
• JTable must be added to the JScrollPane to see all records of the table Properly.
3. Add the scroll pane into the content pane or any frame.
• JTable(Object[][] data, Object []Column): A table is created with the specified name where
JTree
• The JTree is a type of GUI(Graphic User Interface) that displays information in a hierarchical way.
• JTree is an expandable and collapsible component containing sub_trees to display.
• It has a 'root node' at the top most which is a parent for all nodes in the tree.
• JTree inherits from JComponent class.
• To create JTree we must be include following package;
• Import javax.swing.tree.*;
• DefaultMutableTreeNode : This class is used to create a Node.
• Example: DefaultMutableTreeNode obj=new DefaultMutableTreeNode(“Name of node”);
• The default constructor of JTree is JTree(TreeNode obj), obj is nothing but root node
• Example: JTree jt=new JTree(obj);
• It may even display the text as specified. As the task reaches its completion, the progress bar fills up.
• A progress bar is a component that is used when we process lengthy tasks. It is animated so that the user knows
that our task is progressing.
• JProgressBar(int min, int max) : creates an progress bar with specified minimum and
maximum value.
• Example: JProgressBar jpb=new JProgressBar(0,100);
• 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.
• Example: JProgressBar jpb=new JProgressBar(SwingConstants.VERTICAL,0,100 );
JToolTip
• ToolTip is very important component that shows a temporary pop-up message when
the mouse pointer stays on component.
• We can add tooltip text to almost all the components in java swing.
• When the cursor enters the boundary of the component a pop up appears and text is
displayed that is nothing but tooltip.