Chap 01 Abstract Windowing Toolkit
Chap 01 Abstract Windowing Toolkit
CHAPTER 01
8
Some Types Of Components
Button Checkbox
Label
Choice Scrollbar
Button
Checkbox
CheckboxGroup
9
Windows and Frames
The Window class defines a top-level Window with no Borders or Menu bar.
Usually used for application splash screens
Frame defines a top-level Window with Borders and a Menu Bar
Frames are more commonly used than Windows
Once defined, a Frame is a Container which can contain Components
Panels
When writing a GUI application, the GUI portion can become
quite complex.
To manage the complexity, GUIs are broken down into groups of
components. Each group generally provides a unit of
functionality.
A Panel is a rectangular Container whose sole purpose is to hold
and manage components within a GUI.
Container & Components Together for GUI
Container (Applet)
Containers (Panels)
Component
(Canvas)
Components
(Buttons)
Components (TextFields)
Components (Labels)
1.2 Creating Windowed Program And Applet
Download JDK 8
Note:- Latest Version of JDK is JDK 17 ,but it is not possible to test your applet program with
appletviewer tool because this tool is not present with later version of jdk, above 8 so we download
and use JDK 8
Install And Run 1st Program With Applet
Creating Windowed Program with Applet
import java.awt.*;
import java.applet.*;
public class sampleApplet extends Applet
{
public void init()
{ Frame ob=new Frame();
ob.setSize(200,200);
ob.setTitle("Frame Program");
ob.setVisible(true);
}
public void paint(Graphics g)
{ g.drawString("Inside Applet",20,10);
}
} /* <applet code="sampleApplet" width=200 height=300></applet> */
1.3 AWT Controls And Managers:
use Of
Labels,
Buttons,
checkbox,
checkboxGroup,
scrollBars,
TextField,
TextArea.
Java AWT Label
3. void setLabel (String It sets the label of button with the specified
label) string.
4. String getLabel() It fetches the label of the button.
Example
Sr Constructor Description
1. Checkbox() It constructs a checkbox with no string as the label.
2. Checkbox(String label) It constructs a checkbox with the given label.
3. Checkbox(String label, It constructs a checkbox with the given label and
boolean state) sets the given state.
4. Checkbox(String label, It constructs a checkbox with the given label, set
boolean state, the given state in the specified checkbox group.
CheckboxGroup group)
5. Checkbox(String label, It constructs a checkbox with the given label, in the
CheckboxGroup group, given checkbox group and set to the specified state.
boolean state)
Checkbox Class Methods
1. // creating a frame
2. Frame f = new Frame("TextField Example");
3. // creating objects of textfield
4. TextField t1, t2;
5. // instantiating the textfield objects
6. // setting the location of those objects in the frame
7. t1 = new TextField("Welcome to Javatpoint.");
8. t1.setBounds(50, 100, 200, 30);
9. t2 = new TextField("AWT Tutorial");
10. t2.setBounds(50, 150, 200, 30);
11. // adding the components to frame
12. f.add(t1);
13. f.add(t2);
Java AWT Textarea
It arranges the components in a container like the words on a page. It fills the
top line from left to right and top to bottom. The components are arranged
in the order as they are added i.e. first components appears at top left, if the
container is not wide enough to display all the components, it is wrapped
around the line. Vertical and horizontal gap between components can be
controlled. The components can be left, center or right aligned.
•public void next(Container parent): is used to flip to the next card of the
given container.
•public void previous(Container parent): is used to flip to the previous card
of the given container.
•public void first(Container parent): is used to flip to the first card of the
given container.
•public void last(Container parent): is used to flip to the last card of the given
container.
•public void show(Container parent, String name): is used to flip to the
specified card with the given name.
JAVA GRIDBAGLAYOUT
As you might have guessed from the above example, it is possible to reuse
the same GridBagConstraints instance for multiple components, even if the
components have different constraints.
gridx, gridy
The leftmost column has address gridx=0 and the top row has address gridy=0.
Use GridBagConstraints.RELATIVE (the default value) to specify that the
component be placed just to the right of (for gridx) or just below (for gridy)
the component that was added to the container just before this component was
added.
gridwidth, gridheight
insets
Specifies the external padding of the component -- the minimum amount of
space between the component and the edges of its display area. The value is
specified as an Insets object. By default, each component has no external
padding.
IPADX,IPADY AND INSETS
anchor
Used when the component is smaller than its display area to determine where
(within the area) to place the component.
Version note: The PAGE_* and *LINE_* constants were introduced in 1.4. Previous releases require values named after points of the
compass. For example, NORTHEAST indicates the top-right part of the display area. We recommend that you use the new constants, instead,
since they enable easier localization.
weightx, weighty
Constructor Description
Creates a menu bar to which one or many menus
public MenuBar()
are added.
Creates a menu with a title.
public Menu(String title)
public MenuItem(String title) Creates a menu item with a title.
JAVA AWT DIALOG
The Dialog control represents a top level window with a border and a
title used to take some form of input from the user. It inherits the
Window class.
Frame vs Dialog
Frame and Dialog both inherits Window class. Frame has maximize
and minimize buttons but Dialog doesn't have.
FILEDIALOG BOX
FileDialog control represents a dialog window from which the user can select a file.
Class declaration
Following is the declaration for java.awt.FileDialog class:
public class FileDialog extends Dialog
Field
•static int LOAD -- This constant value indicates that the purpose of the file dialog
window is to locate a file from which to read.
•static int SAVE -- This constant value indicates that the purpose of the file dialog
window is to locate a file to which to write.
S.N. Constructor & Description
1 FileDialog(Frame parent)
Creates a file dialog for loading a file.
2 FileDialog(Frame parent, String title)
Creates a file dialog window with the specified title for loading a
file.
3 FileDialog(Frame parent, String title, int mode)
Creates a file dialog window with the specified title for loading or
saving a file.
S.N. Method & Description
1 String getDirectory()
Gets the directory of this file dialog.
2 String getFile()
Gets the selected file of this file dialog.
3 FilenameFilter getFilenameFilter()
Determines this file dialog's filename filter.
4 int getMode()
Indicates whether this file dialog box is for loading from a file or for
saving to a file.
5 void setDirectory(String dir)
Sets the directory of this file dialog window to be the specified directory.
6 void setFile(String file)
Sets the selected file for this file dialog window to be the specified file.
7 void setFilenameFilter(FilenameFilter filter)
Sets the filename filter for this file dialog window to the specified filter.
8 void setMode(int mode)
Sets the mode of the file dialog.