Question:
How do i write Java for a user entering a URL into a form and it opens that url in a new window upon submit?
2010-05-18 11:01:25 UTC
I'm trying to find out what the Java code would be for the following scenario:
1. user enters a URL into a form box.
2. user clicks a button (Submit) on the right of the form box
3. a window pops up using the URL that was entered in the form box as the URL that it goes to.

Thank you for any advice or direction that you can point me.
Three answers:
?
2010-05-20 22:36:52 UTC
//pk



//APPLET

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.net.URL;

import java.net.MalformedURLException;





public class MyUrl extends JApplet implements ActionListener {

JLabel l;

JTextField t;

JButton b;

JPanel p;



public void init() {

p = new JPanel();

l = new JLabel("URL: ");

t = new JTextField("http://www.",20);

b = new JButton("Open");

b.addActionListener(this);



p.add(l);

p.add(t);

p.add(b);



getContentPane().add(p);

}



public void actionPerformed(ActionEvent e) {

if (e.getSource() == b) {

try {

String sUrl = t.getText().trim();

if (sUrl.length() != 0) {

URL url = new URL(sUrl);

getAppletContext().showDocument(url);

} else {

JOptionPane.showMessageDialog(this, "URL cannot be left blank");

}

}

catch (MalformedURLException ex) {

JOptionPane.showMessageDialog(this, "Invalid web address");

}

}

}

}





====

HTML

=====



?
2016-04-14 12:39:20 UTC
Java has a URL class.
Leo D
2010-05-18 13:05:17 UTC
Are you writing an Applet or an Application? It's completely different depending on which one. For an applet, please visit: http://web.archive.org/web/20000301020422/java.sun.com/docs/books/tutorial/applet/appletsonly/browser.html For the latest version of this article, please visit: http://java.sun.com/docs/books/tutorial/deployment/applet/browser.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...