0% found this document useful (0 votes)
42 views21 pages

Lecture 14 - AWT-II

This document summarizes key points about AWT controls from a lecture on object-oriented programming in Java. It discusses Choice controls which allow the selection of an item from a dropdown list, including how to add and retrieve items. It also covers TextField controls for single-line text entry, including getting/setting text, selecting text, and disabling echoing of characters for password fields. Code examples are provided to demonstrate using Choice and TextField controls.

Uploaded by

Muhammad Tayyab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views21 pages

Lecture 14 - AWT-II

This document summarizes key points about AWT controls from a lecture on object-oriented programming in Java. It discusses Choice controls which allow the selection of an item from a dropdown list, including how to add and retrieve items. It also covers TextField controls for single-line text entry, including getting/setting text, selecting text, and disabling echoing of characters for password fields. Code examples are provided to demonstrate using Choice and TextField controls.

Uploaded by

Muhammad Tayyab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CSE205: Object Oriented

Programming
Lecture # 14: Abstract Windowing Toolkit (AWT) - II

Muhammad Imran
(Based on Java, The Complete Reference)
http://www.secscourses.tk

1
Outline
• More AWT Controls
• Choice Control
• Textfield

2
2
Choice Controls
• The Choice class is used to create a pop-up list of items
from which the user may choose
• Each item in the list is a String that appears as a left-
justified label in the order it is added to the Choice object
• Choice only defines the default constructor, which creates
an empty list

3
3
Choice Controls
• To add a selection to the list, call addItem() or add()
 void addItem(String name)
 void add(String name)
• To determine which item is currently selected, we may call
either getSelectedItem() or getSelectedIndex()
 String getSelectedItem()
 int getSelectedIndex()
• The first item is at index 0
• By default, the first item added to the list is selected
4
4
Choice Controls
• To obtain the number of items in the list, call getItemCount()
 int getItemCount()
• We can set the currently selected item using the select()
method with a zero-based integer index or a string that will
match a name in the list
 void select(int index)
 void select(String name)
• Given an index, we can obtain the name associated with the
item at that index by calling getItem(), which has the general
form:
 String getItem(int index)
5
5
Choice Controls
• Each time a choice is selected, an item event is generated
• Each listener implements the ItemListener interface
• That interface defines the itemStateChanged() method
• An ItemEvent object is supplied as the argument to this
method

6
6
Choice Controls
// Demonstrate Choice List
import java.awt.*;
import java.awt.event.*;
class MyChoiceList extends Frame implements ItemListener{
Panel panel;
Label label;
String msg="";
Choice os, browser;
MyChoiceList() {
addWindowListener(new MyWindowAdapter());
label = new Label();
os = new Choice();
browser = new Choice();
panel = new Panel(); 7
7
Choice Controls
// add items to os list
os.add("Windows 98");
os.add("Windows NT/2000");
os.add("Solaris");
os.add("MacOS");
// add items to browser list
browser.add("Netscape 1.1");
browser.add("Netscape 2.x");
browser.add("Netscape 3.x");
browser.add("Netscape 4.x");
browser.add("Internet Explorer 3.0");
browser.add("Internet Explorer 4.0");
browser.add("Internet Explorer 5.0");
browser.add("Lynx 2.4"); 8
8
Choice Controls
browser.select("Netscape 4.x");
os.addItemListener(this);
browser.addItemListener(this);
panel.add(os);
panel.add(browser);
panel.add(label);
add(panel);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}

9
9
Choice Controls
public void paint(Graphics g)
{
msg = "Current OS: ";
msg += os.getSelectedItem();
msg += "Current Browser: ";
msg += browser.getSelectedItem();
label.setText(msg);
}
}

10
10
Choice Controls
public class MyChoiceListDemo {
public static void main(String args[]) {
MyChoiceList win=new MyChoiceList();
win.setSize(380,120);
win.setTitle("Choice List");
win.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
11
11
Using a TextField
• The TextField class implements a single-line text-entry area
• Text fields allow the user to enter strings and to edit the text
using the arrow keys, cut and paste keys, and mouse
selections
• TextField is a subclass of TextComponent
• TextField defines the following constructors:
 TextField()
 TextField(int numChars)
 TextField(String str)
 TextField(String str, int numChars)
12
12
Using a TextField
• TextField (and its superclass TextComponent) provides several
methods that allow us to utilize a text field
• To obtain the string currently contained in the text field, call
 String getText()
• To set the text, call
 void setText(String str)

13
13
Using a TextField
• The user can select a portion of the text in a text field
• We can also select a portion of text under program control by
using
 void select(int startIndex, int endIndex)
• The select() method selects the characters beginning at
startIndex and ending at endIndex-1
• Program can obtain the currently selected text by calling
 String getSelectedText()
14
14
Using a TextField
• There may be times when we will want the user to enter text that is
not displayed, such as a password
• We can disable the echoing of the characters as they are typed by
calling
 void setEchoChar(char ch)
• This method specifies a single character that the TextField will
display when characters are entered (thus, the actual characters
typed will not be shown)

15
15
Using a TextField
• Program generally will not respond to individual key
events that occur within a text field

• However, we may want to respond when the user presses


Enter key

• When this occurs, an action event is generated

16
16
Using a TextField
// Demonstrate TextField
import java.awt.*;
import java.awt.event.*;
class MyTextField extends Frame implements ActionListener{
Panel panel;
Label label, nlabel, plabel;
String msg="";
TextField name, pass;
MyTextField()
{
addWindowListener(new MyWindowAdapter());
label = new Label();
nlabel = new Label("Name: ",Label.RIGHT);
plabel = new Label("Password: ",Label.RIGHT);

17
17
Using a TextField
name = new TextField(12);
pass = new TextField(8);
pass.setEchoChar('*');
panel = new Panel();
name.addActionListener(this);
pass.addActionListener(this);
panel.add(nlabel);
panel.add(name);
panel.add(plabel);
panel.add(pass);
panel.add(label);
add(panel);
}

18
18
Using a TextField
public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
msg = "Name: " + name.getText();
msg += "Selected Text in name: " + name.getSelectedText();
msg += "Password: " + pass.getText();
label.setText(msg);
}
}

19
19
Using a TextField
public class MyTextFieldDemo {
public static void main(String args[]) {
MyTextField win=new MyTextField();
win.setSize(380,120);
win.setTitle("TextField");
win.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

20
20
Recommended Reading
• Chapter # 26: Using AWT Controls, Layout Managers, and
Menus from Herbert Schildt, Java: The Complete
Reference, J2SETM 9th Edition

21
21

You might also like