0% found this document useful (0 votes)
6 views11 pages

AWT Controls-1

Advance Java Concepts

Uploaded by

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

AWT Controls-1

Advance Java Concepts

Uploaded by

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

TextField

• It is a control used to enter a single-line of text.


• It is called as an edit control.
• TextField allows user to enter string, edit the text.
• TextField is a subclass of TextComponent.
Constructors
• TextField=new TextField()
-creates a default textField.
• TextField=new TextField(int numchars)
creates the textfield with “numchars” as no.of character of
textfield.
• TextField=new TextField(String str)
creates the textfield with specified string.
• TextField=new TextField(String str,int numchars)
creates the textfield with specified string and specified no.of
characters.
Methods
• String getText()-returns the string currently contained in the textfield.
• void setString(String str)-sets the String to str.
• String getSelectedText()-returns the text selected.
• Boolean isEditable()-returns true if the text is changeable or false if it
is not editable.
• void setEchoChar(char ch)-set echoing to the character.
• boolean echoCharIsSet()-returns true id echo char is set.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="TextFieldDemo" width=300 height=300>
</applet>*/
public class TextFieldDemo extends Applet implements

ActionListener
{
TextField name,password;
public void init()
{
Label name1=new Label("Name",Label.RIGHT);
Label password1=new Label("Password",Label.RIGHT);
name=new TextField(12);
password=new TextField(8);
password.setEchoChar('*');
add(name1);
add(name);
add(password1);
add(password);
name.addActionListener(this);
password.addActionListener(this); }
public void actionPerformed(ActionEvent ae) {
repaint(); }
public void paint(Graphics g)
{
g.drawString("Name:"+name.getText(),6,60);
g.drawString("Password:"+password.getText(),6,100);
} }
TEXTAREA
It a multiline text editor.
Constructors
TextArea ta=new TextArea()-creates an empty textarea
TextArea ta=new TextArea(int numlines,int numchars)-
creates a TextArea field with specified no.of lines and no.ofchar
for each line.
TextArea ta=new TextArea(String str,int numlines,int numchars,int bars)-
creates a TextArea field with specified no.of lines and no.ofchar
for each line and scrollbar scroll the text.
bars=SCROLL_BOTH
SCROLL_VERTICAL,SCROLL_HORIZONTAL,SCROLL_NONE.
import java.awt.*;
import java.applet.*;
import java.awt.TextArea.*;

/*<applet code="TextAreaDemo" width=300 height=300>


</applet>*/
public class TextAreaDemo extends Applet
{
String val="Hello to textarea with scroll bars";
public void init()
{
TextArea t=new TextArea(val,10,30);
add(t);
}
}
LIST
List class provides a compact,multichoice scrolling selection.
List obj can be constructed to show any no.of choices in the window.
Constructors
• List l=new List()=allows only one line tobe selected at a time.
• List l=new List(int rows)-rows specified no.of entries in the list tobe
visible.
• List l=new List(int rows,boolean multiselect)-when multiselect is true,
then user may select two or more items at a time.
Methods
• void add(String str)-add specified string as the item to list
• void add(String str,int index)-add specified string as the item to list
at specified index.Indexing in list starts at zero.(-1) adds the
item to end of the list.
String getSelectedItem()-returns the item which is currently selected.
Int getSelectedIndex()-returns the index of the item selected.
String []getSelectedItems()-returns the multiple item which is currently
selected.
Int []getSelectedIndex()-returns the index of the multiple item selected.
getItemCount()-returns the no.of items in the list.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="ListDemo" width=300 height=300>
</applet>*/
public class ListDemo extends Applet implements
ActionListener
{
List l;
public void init() {
l=new List();
l.add("Red");
l.add("Blue");
l.add("Green");
add(l);
l.addActionListener(this); }
public void actionPerformed(ActionEvent ae)
{
String msg=l.getSelectedItem();
if(msg.equals("Red"))
setBackground(Color.red);
if(msg.equals("Blue"))
setBackground(Color.blue);
if(msg.equals("Green"))
setBackground(Color.green); } }

You might also like