In this AWT tutorial, we will create dialog to collect address information. Then we will launch this AWT dialog from an AWT Frame Window as a Modal dialog. When user closes the dialog by pressing OK button, AWT frame window shows the Formatted address in a TextArea component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 04: Add Action Listener public class FrameWin extends Frame implements WindowListener, ActionListener { //sample 01: Declaration TextArea taAddress; public FrameWin(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(420, 160); setLocation(100,100); addWindowListener(this); //Sample 02: Add a Button, Label and Text Area Label lblAddress = new Label("Address"); Label lblName = new Label("Name"); Label lblAge = new Label("Age"); TextField txtName = new TextField(30); TextField txtAge = new TextField(3); Button btnAddress = new Button("..."); taAddress = new TextArea(3,40); taAddress.setEnabled(false); //Sample 03: Add Controls to Frame Window setLayout(new FlowLayout()); add(lblName); add(txtName); add(lblAge); add(txtAge); add(lblAddress); add(taAddress); add(btnAddress); //Sample 05: Register the Button with Listener btnAddress.addActionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void actionPerformed(ActionEvent e) { //Sample 13: Launch the Dialog ||=> Run 2 AddressDlg addrDlg = new AddressDlg(this, "Address Form", true); addrDlg.setVisible(true); //Sample 15: Set the Formatted Text taAddress.setText(addrDlg.getAddrOut()); } } ================================================================ import java.awt.Button; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 09: Implement Window Listener //Sample 14: Implement Action Listener public class AddressDlg extends Dialog implements ActionListener, WindowListener { //Sample 06: Class Members TextField txtFlat; TextField txtStreet; TextField txtCity; TextField txtState; TextField txtPin; private String AddrOut; //Sample 07: Constructor public AddressDlg(Frame parent, String title, boolean isModal) { super(parent, title, isModal); //Sample 12: Initialize the Dialog InitDialog(); } //Sample 08: Arrange the Control private void InitDialog() { //Sample 8.1 Set Size and Location setSize(380, 200); setLocation(300,200); setLayout(new FlowLayout()); //Sample 8.2: Create Labels Label lblFlatNo = new Label("House/Flat :"); Label lblStreet = new Label("Street :"); Label lblCity = new Label("City :"); Label lblState = new Label("State :"); Label lblZip = new Label("Zip :"); //Sample 8.3: Create Text Boxes txtFlat = new TextField(30); txtStreet = new TextField(30); txtCity = new TextField(30); txtState = new TextField(30); txtPin = new TextField(10); //Sample 8.4: Create Buttons Button BtnOK = new Button("OK"); Button BtnCancel = new Button("Cancel"); BtnOK.setActionCommand("OK"); BtnCancel.setActionCommand("Cancel"); //sample 8.5: Add the Controls add(lblFlatNo); add(txtFlat); add(lblStreet); add(txtStreet); add(lblCity); add(txtCity); add(lblState); add(txtState); add(lblZip); add(txtPin); add(BtnOK); add(BtnCancel); //Sample 10: Register with Window Listener addWindowListener(this); //Sample 14.1: Register Action Listener BtnOK.addActionListener(this); BtnCancel.addActionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { //Sample 11: Hide the Dialog this.setVisible(false); } @Override public void actionPerformed(ActionEvent e) { //Sample 14.2 Fill the AddrOut Private Member String action = e.getActionCommand(); if (action.compareTo("OK") == 0) { AddrOut = txtFlat.getText() + "\r\n" + txtStreet.getText() + "\r\n" + txtCity.getText() + " - " + txtState.getText() + "\r\n" + txtPin.getText(); } else { AddrOut = "None"; } setVisible(false); } //Sample 14.3 Fill the AddrOut Private Member public String getAddrOut() { return AddrOut; } } ================================================================ |
Categories: AWT-Tube