Text fields, buttons, Scrollbar, Choice, List and Check box in Java program

Aim
To demonstrate text fields, button, scrollbar, choice, list, check box using applet in java| CS1404 INTERNET PROGRAMMING LABORATORY
Algorithm:
Import the necessary package java.awt.*;,java.awt.evet.*;,java.applet.*;
Declare the class AWTControls and implements Action listener, item Listener, Adjust Listener
Include there component with choice box, buttons, scrollbar, text fields, check box, list
And implement action performed event and create the object for action performed
Add action listener for every object component.
Declare the paint function and display the object
Terminate the program
Source Code | Java program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AWTControls" width=500 height=550>
</applet>
*/
public class AWTControls extends Applet implements
ActionListener, ItemListener, AdjustmentListener {
String btnMsg = "";
String lstMsg = "";
Button btnHard, btnSoft;
Checkbox chkC, chkCpp, chkJava;
CheckboxGroup cbgCompany;
Checkbox optTcs, optInfosys, optSyntel;
Scrollbar horzCurrent, vertExpected;
TextField txtName, txtPasswd;
TextArea txtaComments = new TextArea("", 5, 30);
Choice chCity;
List lstAccompany;
public void init() {
Label lblName = new Label("Name : ");
Label lblPasswd = new Label("Password : ");
Label lblField = new Label("Field of Interest : ");
Label lblSkill = new Label("Software Skill(s) : ");
Label lblDreamComp = new Label("Dream Company : ");
Label lblCurrent = new Label("Current % : ");
Label lblExpected = new Label("Expected % : ");
Label lblCity = new Label("Preferred City : ");
Label lblAccompany = new Label("Accompanying Persons% : ");
txtName = new TextField(15);
txtPasswd = new TextField(15);
txtPasswd.setEchoChar('*');
btnHard = new Button("Hardware") ;
btnSoft = new Button("Software") ;
chkC = new Checkbox("C");
chkCpp = new Checkbox("C++");
chkJava = new Checkbox("Java");
cbgCompany = new CheckboxGroup();
optTcs = new Checkbox("Tata Consultancy Services",
cbgCompany, true);
optInfosys = new Checkbox("Infosys", cbgCompany,
false);
optSyntel = new Checkbox("Syntel India Ltd",
cbgCompany, false);
horzCurrent = new Scrollbar(Scrollbar.VERTICAL, 0,1, 1, 101);
vertExpected = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 1, 101);
chCity = new Choice();
chCity.add("Chennai");
chCity.add("Bangalore");
chCity.add("Hyderabad");
chCity.add("Trivandrum");
lstAccompany = new List(4, true);
lstAccompany.add("Father");
lstAccompany.add("Mother");
lstAccompany.add("Brother");
lstAccompany.add("Sister");
add(lblName);
add(txtName);
add(lblPasswd);
add(txtPasswd);
add(lblField);
add(btnHard);
add(btnSoft);
add(lblSkill);
add(chkC);
add(chkCpp);
add(chkJava);
add(lblDreamComp);
add(optTcs);
add(optInfosys);
add(optSyntel);
add(lblCurrent);
add(horzCurrent);
add(lblExpected);
add(vertExpected);
add(txtaComments);
add(lblCity);
add(chCity);
add(lblAccompany);
add(lstAccompany);
btnHard.addActionListener(this);
btnSoft.addActionListener(this);
chkC.addItemListener(this);
chkCpp.addItemListener(this);
chkJava.addItemListener(this);
optTcs.addItemListener(this);
optInfosys.addItemListener(this);
optSyntel.addItemListener(this);
horzCurrent.addAdjustmentListener(this);
vertExpected.addAdjustmentListener(this);
chCity.addItemListener(this);
lstAccompany.addItemListener(this);
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if(str.equals("Hardware")) {
btnMsg = "Hardware";
}
else if(str.equals("Software")) {
btnMsg = "Software";
}
repaint();
}
public void itemStateChanged(ItemEvent ie) {
repaint();
}
public void adjustmentValueChanged(AdjustmentEvent ae) {
repaint();
}
public void paint(Graphics g) {
g.drawString("Detailed Profile :-", 10, 300);
g.drawString("Field of Interest : " + btnMsg, 10,320);
g.drawString("Software Skill(s) : " , 10, 340);
g.drawString("C : " + chkC.getState(), 10, 360);
g.drawString("C++ : " + chkCpp.getState(), 10, 380);
g.drawString("Java : " + chkJava.getState(), 10,400);
g.drawString("Dream Company : " +
cbgCompany.getSelectedCheckbox().getLabel(), 10,420);
g.drawString("Current % : " +
horzCurrent.getValue(), 10, 440);
g.drawString("Expected % : " +vertExpected.getValue(), 10, 460);
g.drawString("Name: " + txtName.getText(), 10, 480);
g.drawString("Password: " + txtPasswd.getText(), 10,
500);
g.drawString("Preferred City : " +chCity.getSelectedItem(), 10, 520);
int idx[];
idx = lstAccompany.getSelectedIndexes();
lstMsg = "Accompanying Persons : ";
for(int i=0; i<idx.length; i++)
lstMsg += lstAccompany.getItem(idx[i]) + " ";
g.drawString(lstMsg, 10, 540);
}
}

Output:-
C:\IPLAB>javac AWTControls.java
C:\IPLAB>appletviewer AWTControls.java


Choice button, scrollbar, Text fields, check box list to implement in java program

Related post