Class methods
S.N. | Method & Description |
---|---|
1 | void addNotify() Makes this Dialog displayable by connecting it to a native screen resource. |
2 | AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this Dialog. |
3 | Dialog.ModalityType getModalityType() Returns the modality type of this dialog. |
4 | String getTitle() Gets the title of the dialog. |
5 | void hide() Deprecated. As of JDK version 1.5, replaced by setVisible(boolean). |
6 | boolean isModal() Indicates whether the dialog is modal. |
7 | boolean isResizable() Indicates whether this dialog is resizable by the user. |
8 | boolean isUndecorated() Indicates whether this dialog is undecorated. |
9 | protected String paramString() Returns a string representing the state of this dialog. |
10 | void setModal(boolean modal) Specifies whether this dialog should be modal. |
11 | void setModalityType(Dialog.ModalityType type) Sets the modality type for this dialog. |
12 | void setResizable(boolean resizable) Sets whether this dialog is resizable by the user. |
13 | void setTitle(String title) Sets the title of the Dialog. |
14 | void setUndecorated(boolean undecorated) Disables or enables decorations for this dialog. |
15 | void setVisible(boolean b) Shows or hides this Dialog depending on the value of parameter b. |
16 | void show() Deprecated. As of JDK version 1.5, replaced by setVisible(boolean). |
17 | void toBack() If this Window is visible, sends this Window to the back and may cause it to lose focus or activation if it is the focused or active Window. |
Methods inherited
This class inherits methods from the following classes:- java.awt.Window
- java.awt.Component
- java.lang.Object
Dialog Example
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >AwtControlDemo.java
package com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showDialogDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showDialogDemo(){ headerLabel.setText("Control in action: Dialog"); Button showAboutDialogButton = new Button("Show About Dialog"); showAboutDialogButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { AboutDialog aboutDialog = new AboutDialog(mainFrame); aboutDialog.setVisible(true); } }); controlPanel.add(showAboutDialogButton); mainFrame.setVisible(true); } class AboutDialog extends Dialog { public AboutDialog(Frame parent){ super(parent, true); setBackground(Color.gray); setLayout(new BorderLayout()); Panel panel = new Panel(); panel.add(new Button("Close")); add("South", panel); setSize(200,200); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ dispose(); } }); } public boolean action(Event evt, Object arg){ if(arg.equals("Close")){ dispose(); return true; } return false; } public void paint(Graphics g){ g.setColor(Color.white); g.drawString("TutorialsPoint.Com", 25,70 ); g.drawString("Version 1.0", 60, 90); } } }Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.javaIf no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemoVerify the following output
AWT FileDialog Class
Introduction
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
Following are the fields for java.awt.Image class:- 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.
Class constructors
S.N. | Constructor & Description |
---|---|
1 | FileDialog(Dialog parent) Creates a file dialog for loading a file. |
2 | FileDialog(Dialog parent, String title) Creates a file dialog window with the specified title for loading a file. |
3 | FileDialog(Dialog parent, String title, int mode) Creates a file dialog window with the specified title for loading or saving a file. |
4 | FileDialog(Frame parent) Creates a file dialog for loading a file. |
5 | FileDialog(Frame parent, String title) Creates a file dialog window with the specified title for loading a file. |
6 | FileDialog(Frame parent, String title, int mode) Creates a file dialog window with the specified title for loading or saving a file. |
Class methods
S.N. | Method & Description |
---|---|
1 | void addNotify() Creates the file dialog's peer. |
2 | String getDirectory() Gets the directory of this file dialog. |
3 | String getFile() Gets the selected file of this file dialog. |
4 | FilenameFilter getFilenameFilter() Determines this file dialog's filename filter. |
5 | int getMode() Indicates whether this file dialog box is for loading from a file or for saving to a file. |
6 | protected String paramString() Returns a string representing the state of this FileDialog window. |
7 | void setDirectory(String dir) Sets the directory of this file dialog window to be the specified directory. |
8 | void setFile(String file) Sets the selected file for this file dialog window to be the specified file. |
9 | void setFilenameFilter(FilenameFilter filter) Sets the filename filter for this file dialog window to the specified filter. |
10 | void setMode(int mode) Sets the mode of the file dialog. |
Methods inherited
This class inherits methods from the following classes:- java.awt.Dialog
- java.awt.Window
- java.awt.Component
- java.lang.Object
FileDialog Example
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >AwtControlDemo.java
package com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showFileDialogDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showFileDialogDemo(){ headerLabel.setText("Control in action: FileDialog"); final FileDialog fileDialog = new FileDialog(mainFrame,"Select file"); Button showFileDialogButton = new Button("Open File"); showFileDialogButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fileDialog.setVisible(true); statusLabel.setText("File Selected :" + fileDialog.getDirectory() + fileDialog.getFile()); } }); controlPanel.add(showFileDialogButton); mainFrame.setVisible(true); } }Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.javaIf no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemoVerify the following output
Event Handling
What is an Event?
Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page are the activities that causes an event to happen.Types of Event
The events can be broadly classified into two categories:- Foreground Events - Those events which require the direct interaction of user.They are generated as consequences of a person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page etc.
- Background Events - Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.
What is Event Handling?
Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events.Let's have a brief introduction to this model.The Delegation Event Model has the following key participants namely:
- Source - The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it's handler. Java provide as with classes for source object.
- Listener - It is also known as event handler.Listener is responsible for generating response to an event. From java implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is received , the listener process the event an then returns.
Steps involved in event handling
- The User clicks the button and the event is generated.
- Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.
- Event object is forwarded to the method of registered listener class.
- the method is now get executed and returns.
Points to remember about listener
- In order to design a listener class we have to develop some listener interfaces.These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class.
- If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object.
Callback Methods
These are the methods that are provided by API provider and are defined by the application programmer and invoked by the application developer. Here the callback methods represents an event method. In response to an event java jre will fire callback method. All such callback methods are provided in listener interfaces.If a component wants some listener will listen to it's events the the source must register itself to the listener.
Event Handling Example
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >AwtControlDemo.java
package com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showEventDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showEventDemo(){ headerLabel.setText("Control in action: Button"); Button okButton = new Button("OK"); Button submitButton = new Button("Submit"); Button cancelButton = new Button("Cancel"); okButton.setActionCommand("OK"); submitButton.setActionCommand("Submit"); cancelButton.setActionCommand("Cancel"); okButton.addActionListener(new ButtonClickListener()); submitButton.addActionListener(new ButtonClickListener()); cancelButton.addActionListener(new ButtonClickListener()); controlPanel.add(okButton); controlPanel.add(submitButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } private class ButtonClickListener implements ActionListener{ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if( command.equals( "OK" )) { statusLabel.setText("Ok Button clicked."); } else if( command.equals( "Submit" ) ) { statusLabel.setText("Submit Button clicked."); } else { statusLabel.setText("Cancel Button clicked."); } } } }Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.javaIf no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemoVerify the following output
AWT Event Classes
The Event classes represent the event. Java provides us various Event classes but we will discuss those which are more frequently used.EventObject class
It is the root class from which all event state objects shall be derived. All Events are constructed with a reference to the object, the source, that is logically deemed to be the object upon which the Event in question initially occurred upon. This class is defined in java.util package.Class declaration
Following is the declaration for java.util.EventObject class:public class EventObject extends Object implements Serializable
Field
Following are the fields for java.util.EventObject class:- protected Object source -- The object on which the Event initially occurred.
Class constructors
S.N. | Constructor & Description |
---|---|
1 | EventObject(Object source) Constructs a prototypical Event. |
Class methods
S.N. | Method & Description |
---|---|
1 | Object getSource() The object on which the Event initially occurred. |
2 | String toString() Returns a String representation of this EventObject. |
Methods inherited
This class inherits methods from the following classes:- java.lang.Object
No comments:
Post a Comment