You must Sign In to post a response.
  • Category: Computers

    Telephone directory program using frame and applet.


    Want to write a Java program? Looking out for a program to display telephone directory? Find solution from experts on this page.

    Write a Java program that shows a frame. The frame must show the message "Press a character to view the details". When we press the first alphabet of the name, the name and Telephone number must be display. (for example: if we type the letter V or v the following message must be displayed " Name: Victor Phone No. : 0462-580713").
  • #142472
    Create a web page with JavaScript functions. The example AppletNamePage.html has JavaScript functions to retrieve name, and phone numbers. A variable userName Victor which has no value at the outset.

    Open Notepad, type the following Lines of Code below. Just copy and paste.

    Note: I am not guiding you on how to set MS-DOS correct directory. Ensure you set it from your end.


    Data Summary Applet User Page - Java to JavaScript LiveConnect

    <script language="javascript">
    var userName = "";

    // returns number
    function getAge() {
    return /null;
    }
    // returns an object
    function address() {
    this.street = "/null Example Lane";
    this.city = " /null";
    this.state = "CA";
    }
    // returns an array
    function getPhoneNums() {
    return ["0462-5807-13", "0462-5807-13"];
    }
    function writeSummary(summary) {
    summaryElem =
    document.getElementById("summary");
    summaryElem.innerHTML = summary;
    }
    </script>



    <script src =
    " https://www.java.com /js/deployJava.js"></script>
    <script>

    deployJava.runApplet(attributes, parameters, '1.6');
    </script>

    // this HTML element contains
    // the summary



    Applet Java Class
    Name an applet class calledDataSummaryApplet.

    1. TheDataSummaryAppletclass performs the following operations.

    2. Invokes theJSObject'ssetMembermethod to set theuserNamevariable to "Victor".

    3. Retrieves the name, and phone numbers.

    JSObject window = JSObject.getWindow(this);
    ...
    Put the preceding statement in a try ...catch.. block to handlenetscape.javascript.JSException.

    4. Now that the applet has a reference toJSObject, it can invoke the relevant JavaScript functions by using theevalandcallmethods ofJSObject.

    package javatojs;
    import java.applet.Applet;
    import netscape.javascript.*; // add plugin.jar to classpath during compilation
    public class DataSummaryApplet extends Applet {
    public void start() {
    try {
    JSObject window = JSObject.getWindow(this);
    String userName = "Victor";
    // set JavaScript variable
    window.setMember("userName", userName);
    // invoke JavaScript function
    Number age = (Number) window.eval("getAge()");
    // get a JavaScript object and retrieve its contents
    JSObject address = (JSObject) window.eval("new address();");
    String addressStr = (String) address.getMember("street") + ", " +
    (String) address.getMember("city") + ", " +
    (String) address.getMember("state");
    // get an array from JavaScript and retrieve its contents
    JSObject phoneNums = (JSObject) window.eval("getPhoneNums()");
    String phoneNumStr = (String) phoneNums.getSlot(0) + ", " +
    (String) phoneNums.getSlot(1);
    // dynamically change HTML in page; write data summary
    String summary = userName + " : " + age + " : " +
    addressStr + " : " + phoneNumStr;
    window.call("writeSummary", new Object[] {summary}) ;
    } catch (JSException jse) {
    jse.printStackTrace();
    }
    }
    }

    To compile javascriptpackage, type your /jre/lib/plugin.jarin your classpath.
    The result of codes applet's Java calls to JavaScript.

    Victor : 0 : 0 : 0462-5807-13

    The /null or /void will return blank digit or output. Change as per your preferences.


    Be the Hero of the zero hour!

  • #142510
    TThis response is marked as DELETED by the admin.

    To convert from a Java Applet to an application:

    Delete import java.applet.*;.
    Alter the class header so that it extends Frame rather than Applet (line 4 in BalloonFrame.java, below).
    Change the name of the init method to the name of the class, making the former init method the constructor for the class. Delete the word void in the declaration since constructors have no return type (line 9).
    Add an invocation of a method to set the title and the layout of the frame by adding the lines setTitle() and setLayout(new FlowLayout()); inside the constructor (lines 10 and 12).
    Declare (line 6) and add a button to close the window (lines 30-32 and 52-53) or implement WindowListener (not shown, but see reference books cited above).
    Create a new method called main, with the header public static void main (String[] args). This method should create a Frame object and should use the methods setSize(,) and setVisible(true) to display the frame (lines 37 through 41). See example below where the class is called BalloonFrame:

    1 import java.awt.*;
    2 import java.awt.event.*;
    3
    4 public class BalloonFrame extends Frame
    5 implements ActionListener {
    6 private Button grow, shrink, left, right, close;
    7 private Balloon theBalloon;
    8
    9 public BalloonFrame() {
    10 setTitle("Balloon");
    11
    12 setLayout(new FlowLayout());
    13
    14 grow = new Button("Grow");
    15 add(grow);
    16 grow.addActionListener(this);
    17
    18 shrink = new Button("Shrink");
    19 add(shrink);
    20 shrink.addActionListener(this);
    21
    22 left = new Button("Left");
    23 add(left);
    24 left.addActionListener(this);
    25
    26 right = new Button("Right");
    27 add(right);
    28 right.addActionListener(this);
    29
    30 close = new Button("Exit");
    31 add(close);
    32 close.addActionListener(this);
    33
    34 theBalloon = new Balloon();
    35 }
    36
    37 public static void main(String args[]) {
    38 BalloonFrame f = new BalloonFrame();
    39 f.setSize(500, 500);
    40 f.setVisible(true);
    41 }
    42
    43 public void actionPerformed(ActionEvent event) {
    44 if (event.getSource() == grow)
    45 theBalloon.grow();
    46 if (event.getSource() == shrink)
    47 theBalloon.shrink();
    48 if (event.getSource() == left)
    49 theBalloon.moveLeft();
    50 if (event.getSource() == right)
    51 theBalloon.moveRight();
    52 if (event.getSource() == close)
    53 System.exit(0);
    54 repaint();
    55 }
    56
    57 public void paint(Graphics g) {
    58 theBalloon.display(g);
    59 }
    60 }
    To convert from a Java application into an applet:

    Create an HTML file with an applet tag to invoke the applet.
    For example, here is the HTML file for the Balloon example:







    Add the line import java.applet.*; at the top (line 1 in BalloonApplet.java, below).
    Alter the class header so that it extends Applet rather than Frame (line 5).
    Delete the main method.
    Change the name of the constructor method from the name of the class to init (line 10).
    Note that there is no need to create a button to close the window as in the Frames example, above. Any such button and its associated action in the actionPerformed method can be deleted.
    For example, here is the entire BalloonApplet.java file:


    1 import java.applet.*;
    2 import java.awt.*;
    3 import java.awt.event.*;
    4
    5 public class BalloonApplet extends Applet implements ActionListener {
    6
    7 private Button grow, shrink, left, right;
    8 private Balloon theBalloon;
    9
    10 public void init() {
    11 grow = new Button("Grow");
    12 add(grow);
    13 grow.addActionListener(this);
    14
    15 shrink = new Button("Shrink");
    16 add(shrink);
    17 shrink.addActionListener(this);
    18
    19 left = new Button("Left");
    20 add(left);
    21 left.addActionListener(this);
    22
    23 right = new Button("Right");
    24 add(right);
    25 right.addActionListener(this);
    26
    27 theBalloon = new Balloon();
    28 }
    29
    30 public void actionPerformed(ActionEvent event) {
    31 if (event.getSource() == grow)
    32 theBalloon.grow();
    33 if (event.getSource() == shrink)
    34 theBalloon.shrink();
    35 if (event.getSource() == left)
    36 theBalloon.moveLeft();
    37 if (event.getSource() == right)
    38 theBalloon.moveRight();
    39 repaint();
    40 }
    41
    42 public void paint(Graphics g) {
    43 theBalloon.display(g);
    44 }
    45 }
    Balloon.java file used by both of the above programs:


    1 import java.awt.*;
    2
    3 public class Balloon {
    4
    5 private int diameter;
    6 private int x, y;
    7
    8 public Balloon() {
    9 diameter = 10;
    10 x = 20;
    11 y = 50;
    12 }
    13
    14 public void display(Graphics g) {
    15 g.drawOval(x, y, diameter, diameter);
    16 }
    17
    18 public void moveLeft() {
    19 x -= 10;
    20 }
    21
    22 public void moveRight() {
    23 x += 10;
    24 }
    25
    26 public void grow() {
    27 diameter += 5;
    28 }
    29
    30 public void shrink() {
    31 diameter -= 5;
    32 }
    33 }

  • #142731
    Below is the required program
    <script language="javascript">
    var userName = "";

    <script src =
    " link the required file"></script>
    <script>

    deployJava.runApplet(attributes, parameters, '1.6');
    </script>

    function Age() {
    return /null;
    }
    function address() { this.state = "CA"this.street = "/null ";this.city = " /null";

    }
    function getPhoneN0() {
    return ["0462-5807-13", "0462-5807-13"];
    }
    function Summary(summary) {
    summaryElem =
    document.getElementById("summary");
    summaryElem.innerHTML = summary;
    }
    </script>


    10:40 07-05-201710:40 07-05-201710:40 07-05-201710:40 07-05-201710:40 07-05-201710:40 07-05-201710:40 07-05-201710:40 07-05-2017Applet Java Class
    Name an applet class calledDataSummaryApplet.


    JSObject window = JSObject.getWindow(this);
    ...

    package javatojs;
    import java.applet.Applet;
    import netscape.javascript.*; // add plugin.jar to classpath during compilation
    public class DataSummaryApplet extends Applet {
    public void start() {
    try {
    JSObject window = JSObject.getWindow(this);
    String userName = "logan";

    window.setMember("userName", userName);

    Number age = (Number) window.eval("Age()");

    JSObject address = (JSObject) window.eval("address();");
    String addressStr = (String) address.getMember("street") + ", " +
    (String) address.getMember("city") + ", " +
    (String) address.getMember("state");

    JSObject phoneNums = (JSObject) window.eval("getPhoneNums()");
    String phoneNumStr = (String) phoneNums.getSlot(0) + ", " +
    (String) phoneNums.getSlot(1);

    String summary = userName + " : " + age + " : " +
    addressStr + " : " + phoneNumStr;
    window.call("Summary", new Object[] {summary}) ;
    } catch (JSException jse) {
    jse.printStackTrace();
    }
    }
    }

    path= /jre/lib/plugin.jarin


  • Sign In to post your comments