Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java applet: output in textArea

Options
  • 04-10-2006 8:49pm
    #1
    Registered Users Posts: 1,820 ✭✭✭


    I have the code below, trying to make a java applet with a textField, button and textArea, where you enter a domain name in textField, click the button and get the ip address output in textArea. I get the ip address output in the system output window, but not in the textArea.. Please have a look at this code below and see what I am doing wrong. Thanks alot!


    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.net.*;
    import com.borland.jbcl.layout.XYLayout;
    import com.borland.jbcl.layout.*;

    public class InetAddressTester extends Applet {
    boolean isStandalone = false;
    BorderLayout borderLayout1 = new BorderLayout();
    Panel panel1 = new Panel();
    XYLayout xYLayout1 = new XYLayout();
    TextField textField1 = new TextField();
    Button button1 = new Button();
    XYLayout xYLayout2 = new XYLayout();
    TextArea textArea1 = new TextArea();
    //Get a parameter value
    public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
    (getParameter(key) != null ? getParameter(key) : def);
    }

    //Construct the applet
    public InetAddressTester() {
    }

    //Initialize the applet
    public void init() {
    try {
    jbInit();
    InetAddress localAddr = InetAddress.getByName("www.hotmail.com");
    System.out.println("Local IP Address: \t\t" + localAddr.getHostAddress());
    System.out.println("Local hostname: \t\t" + localAddr.getHostName());
    System.out.println("Canonical local hostname: \t" + localAddr.getCanonicalHostName());

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    //Component initialization
    private void jbInit() throws Exception {
    this.setLayout(xYLayout1);
    textField1.setText("Input");
    textField1.addActionListener(new
    InetAddressTester_textField1_actionAdapter(this));
    button1.setLabel("Click here");
    button1.addActionListener(new InetAddressTester_button1_actionAdapter(this));
    panel1.setLayout(xYLayout2);
    textArea1.setText("Output");
    this.add(panel1, new XYConstraints(59, 18, 268, 254));
    panel1.add(button1, new XYConstraints(70, 30, -1, -1));
    panel1.add(textField1, new XYConstraints(157, 31, -1, -1));
    panel1.add(textArea1, new XYConstraints(87, 164, -1, -1));
    }

    //Get Applet information
    public String getAppletInfo() {
    return "Applet Information";
    }

    //Get parameter info
    public String[][] getParameterInfo() {
    return null;
    }

    //Main method
    public static void main(String[] args) {
    InetAddressTester applet = new InetAddressTester();
    applet.isStandalone = true;

    Frame frame;
    frame = new Frame();
    frame.setTitle("Applet Frame");

    frame.add(applet, BorderLayout.CENTER);

    applet.init();
    applet.start();
    frame.setSize(400, 320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2,
    (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
    }

    public void button1_actionPerformed(ActionEvent e) {

    }

    public void textField1_actionPerformed(ActionEvent e) {

    }
    }


    class InetAddressTester_textField1_actionAdapter implements ActionListener {
    private InetAddressTester adaptee;
    InetAddressTester_textField1_actionAdapter(InetAdd ressTester adaptee) {
    this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
    adaptee.textField1_actionPerformed(e);
    }
    }


    class InetAddressTester_button1_actionAdapter implements ActionListener {
    private InetAddressTester adaptee;
    InetAddressTester_button1_actionAdapter(InetAddres sTester adaptee) {
    this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
    adaptee.button1_actionPerformed(e);
    }
    }


Comments

  • Registered Users Posts: 4,188 ✭✭✭pH


    when you use
    System.out.println("xxxx");
    
    the output goes to the system output window

    To put it in the text area (append to existing stuff in the textarea)
    textArea1.setText(textArea1.getText() + "new stuff");
    


  • Registered Users Posts: 4,188 ✭✭✭pH


    To make the lookup happen when you click the button, just add the code you want to run into this method:
    public void button1_actionPerformed(ActionEvent e) {
            // Put code to run on button click in here!
        }
    


  • Registered Users Posts: 1,820 ✭✭✭flodis79


    pH wrote:
    To make the lookup happen when you click the button, just add the code you want to run into this method:
    public void button1_actionPerformed(ActionEvent e) {
            // Put code to run on button click in here!
        }
    

    Ok - so what's wrong with this code?
    public void button1_actionPerformed(ActionEvent e){
            [B]InetAddress localAddr = InetAddress.getByName();
            String text_from_textField1 = (InetAddress.getByName);
            textArea1.setText("Local IP Address: \t\t" + localAddr.getHostAddress());[/B]
      }
    


Advertisement