Bryan051997
asked on
Opening up a URL page from a Java Dialog
Does anyone know how to open up another URL page from within a Java Dialog component. I have a login dialog, and when the user clicks on the submit button, it is supposed to open up a new HTML page. How can I do this. Some source code would be very beneficial. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is the code I have now:
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLExcep tion;
import java.applet.*;
import symantec.itools.multimedia .ImageView er;
public class TestDialog extends Dialog
{
static Author author;
static Author author2;
static Author author3;
String l_userid, l_pass;
String FilePath = "file:///C|/VisualCafePro/ Bnp/";
public TestDialog(Frame parent, boolean modal, Applet applet) {
super(parent, modal);
//{{INIT_CONTROLS
setLayout(null);
addNotify();
resize(insets().left + insets().right + 300,insets().top + insets().bottom + 271);
setBackground(new Color(255));
label2 = new java.awt.Label("Password:" ,Label.RIG HT);
label2.reshape(insets().le ft + 9,insets().top + 205,79,24);
label2.setFont(new Font("Helvetica", Font.BOLD, 13));
label2.setForeground(new Color(16777215));
add(label2);
label1 = new java.awt.Label("User ID:",Label.RIGHT);
label1.reshape(insets().le ft + 25,insets().top + 174,63,24);
label1.setFont(new Font("Helvetica", Font.BOLD, 13));
label1.setForeground(new Color(16777215));
add(label1);
userid = new java.awt.TextField();
userid.reshape(insets().le ft + 98,insets().top + 175,144,26);
userid.setFont(new Font("Dialog", Font.PLAIN, 13));
userid.setBackground(new Color(16777215));
add(userid);
pass = new java.awt.TextField();
pass.reshape(insets().left + 98,insets().top + 205,144,26);
pass.setFont(new Font("Dialog", Font.PLAIN, 13));
pass.setBackground(new Color(16777215));
add(pass);
quit = new java.awt.Button("Reset");
quit.reshape(insets().left + 175,insets().top + 242,77,23);
quit.setFont(new Font("Helvetica", Font.PLAIN, 12));
add(quit);
submit = new java.awt.Button("Submit");
submit.reshape(insets().le ft + 91,insets().top + 242,77,23);
submit.setFont(new Font("Helvetica", Font.PLAIN, 12));
add(submit);
imageViewer1 = new symantec.itools.multimedia .ImageView er();
imageViewer1.reshape(inset s().left + 9,insets().top + 8,283,141);
add(imageViewer1);
try {
imageViewer1.setURL(new java.net.URL("file:/C:/Vis ualCafePro /BNP/Objec t.jpg"));
} catch (java.net.MalformedURLExce ption error) {
}
setTitle("Object Works Login");
//}}
userid.requestFocus();
center(this ,300 ,350 , true);
}
public TestDialog(Frame parent, String title, boolean modal, Applet applet) {
this(parent, modal, applet);
setTitle(title);
}
public String openURL(String url)
{
URL theURL = null;
try { theURL = new URL(url); }
catch ( MalformedURLException e)
{
System.out.println("Bad URL: " + theURL);
}
applet.getAppletContext(). showDocume nt(theURL) ;
return url;
}
// This method displays the dialog component in the center of the browser.
public static void center(Window w, int width, int height, boolean bSetVisible)
{
int xloc = 0;
int yloc = 0;
Toolkit tk;
Dimension screenSize;
if ((tk = w.getToolkit())!=null)
{
screenSize = tk.getScreenSize();
if (width > screenSize.width)
width = screenSize.width;
xloc = (screenSize.width - width) / 2;
if (height > screenSize.height)
height = screenSize.height;
yloc = (screenSize.height - height) / 2;
}
w.reshape(xloc, yloc, width, height);
if (bSetVisible)
w.show(true);
}
// Handles the events of key presses for the textfields and buttons.
public boolean keyDown(Event event, int key)
{
// Sets tab order between fields and buttons.
if((event.target == userid)&&(key == 9)) //Tab key press
{
pass.requestFocus();
}
if((event.target== pass)&&(key == 9)) //Tab key press
{
submit.requestFocus();
}
if((event.target== submit)&&(key == 9)) //Tab key press
{
quit.requestFocus();
}
if((event.target== quit)&&(key == 9)) //Tab key press
{
userid.requestFocus();
}
if((event.target== submit)&&(key == '\n')) //Tab key press
{
Submit_Clicked(event);
}
if((event.target== quit)&&(key == '\n')) //Tab key press
{
Quit_Clicked(event);
}
return false;
}
void Submit_Clicked(Event event)
{
if (userid.getText().length() <= 0 || pass.getText().length() <=0 )
{
}
else
{
author = new Author("bgenti", "bgenti", "Bryan", "Gentile", "bgentile@bdsinc.com", "", "",
"", "", "", "", "", "", "", "");
author2 = new Author("kpetti", "kpetti", "Kevin", "Pettine", "kpettine@bdsinc.com", "", "",
"", "", "", "", "", "", "", "");
author3 = new Author("tclot", "tclot", "Tim", "Clotworthy", "tclot@bdsinc.com", "", "",
"", "", "", "", "", "", "", "");
l_userid = userid.getText();
l_pass = pass.getText();
if (((l_userid.equals(author. UserId) && l_pass.equals(author.Passw ord))) || ((l_userid.equals(author2. UserId) && l_pass.equals(author2.Pass word)))
|| ((l_userid.equals(author3. UserId) && l_pass.equals(author3.Pass word))))
{
//openURL(FilePath + "BNPFrame.html");
hide();
}
else
{
userid.setText("Logon Failed!");
pass.setText("Try again SUCKER!!!!");
userid.requestFocus();
}
}
}
// Clears the textfields when the resetButton is clicked
void Quit_Clicked(Event event)
{
userid.setText("");
pass.setText("");
userid.requestFocus();
}
public boolean handleEvent(Event event)
{
if(event.id == Event.WINDOW_DESTROY) {
hide();
return true;
}
if(event.target == quit && event.id == Event.ACTION_EVENT)
{
Quit_Clicked(event);
return true;
}
if(event.target == submit && event.id == Event.ACTION_EVENT)
{
Submit_Clicked(event);
return true;
}
return super.handleEvent(event);
}
//{{DECLARE_CONTROLS
java.awt.Label label2;
java.awt.Label label1;
java.awt.TextField userid;
java.awt.TextField pass;
java.awt.Button quit;
java.awt.Button submit;
symantec.itools.multimedia .ImageView er imageViewer1;
//}}
}
In my applet when I call the dialog I have:
TestDialog dlg = new TestDialog((Frame)c, true, this);
I am having trouble compiling the dialog. It says that the applet is undefined. What else do I need to do?
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLExcep
import java.applet.*;
import symantec.itools.multimedia
public class TestDialog extends Dialog
{
static Author author;
static Author author2;
static Author author3;
String l_userid, l_pass;
String FilePath = "file:///C|/VisualCafePro/
public TestDialog(Frame parent, boolean modal, Applet applet) {
super(parent, modal);
//{{INIT_CONTROLS
setLayout(null);
addNotify();
resize(insets().left + insets().right + 300,insets().top + insets().bottom + 271);
setBackground(new Color(255));
label2 = new java.awt.Label("Password:"
label2.reshape(insets().le
label2.setFont(new Font("Helvetica", Font.BOLD, 13));
label2.setForeground(new Color(16777215));
add(label2);
label1 = new java.awt.Label("User ID:",Label.RIGHT);
label1.reshape(insets().le
label1.setFont(new Font("Helvetica", Font.BOLD, 13));
label1.setForeground(new Color(16777215));
add(label1);
userid = new java.awt.TextField();
userid.reshape(insets().le
userid.setFont(new Font("Dialog", Font.PLAIN, 13));
userid.setBackground(new Color(16777215));
add(userid);
pass = new java.awt.TextField();
pass.reshape(insets().left
pass.setFont(new Font("Dialog", Font.PLAIN, 13));
pass.setBackground(new Color(16777215));
add(pass);
quit = new java.awt.Button("Reset");
quit.reshape(insets().left
quit.setFont(new Font("Helvetica", Font.PLAIN, 12));
add(quit);
submit = new java.awt.Button("Submit");
submit.reshape(insets().le
submit.setFont(new Font("Helvetica", Font.PLAIN, 12));
add(submit);
imageViewer1 = new symantec.itools.multimedia
imageViewer1.reshape(inset
add(imageViewer1);
try {
imageViewer1.setURL(new java.net.URL("file:/C:/Vis
} catch (java.net.MalformedURLExce
}
setTitle("Object Works Login");
//}}
userid.requestFocus();
center(this ,300 ,350 , true);
}
public TestDialog(Frame parent, String title, boolean modal, Applet applet) {
this(parent, modal, applet);
setTitle(title);
}
public String openURL(String url)
{
URL theURL = null;
try { theURL = new URL(url); }
catch ( MalformedURLException e)
{
System.out.println("Bad URL: " + theURL);
}
applet.getAppletContext().
return url;
}
// This method displays the dialog component in the center of the browser.
public static void center(Window w, int width, int height, boolean bSetVisible)
{
int xloc = 0;
int yloc = 0;
Toolkit tk;
Dimension screenSize;
if ((tk = w.getToolkit())!=null)
{
screenSize = tk.getScreenSize();
if (width > screenSize.width)
width = screenSize.width;
xloc = (screenSize.width - width) / 2;
if (height > screenSize.height)
height = screenSize.height;
yloc = (screenSize.height - height) / 2;
}
w.reshape(xloc, yloc, width, height);
if (bSetVisible)
w.show(true);
}
// Handles the events of key presses for the textfields and buttons.
public boolean keyDown(Event event, int key)
{
// Sets tab order between fields and buttons.
if((event.target == userid)&&(key == 9)) //Tab key press
{
pass.requestFocus();
}
if((event.target== pass)&&(key == 9)) //Tab key press
{
submit.requestFocus();
}
if((event.target== submit)&&(key == 9)) //Tab key press
{
quit.requestFocus();
}
if((event.target== quit)&&(key == 9)) //Tab key press
{
userid.requestFocus();
}
if((event.target== submit)&&(key == '\n')) //Tab key press
{
Submit_Clicked(event);
}
if((event.target== quit)&&(key == '\n')) //Tab key press
{
Quit_Clicked(event);
}
return false;
}
void Submit_Clicked(Event event)
{
if (userid.getText().length()
{
}
else
{
author = new Author("bgenti", "bgenti", "Bryan", "Gentile", "bgentile@bdsinc.com", "", "",
"", "", "", "", "", "", "", "");
author2 = new Author("kpetti", "kpetti", "Kevin", "Pettine", "kpettine@bdsinc.com", "", "",
"", "", "", "", "", "", "", "");
author3 = new Author("tclot", "tclot", "Tim", "Clotworthy", "tclot@bdsinc.com", "", "",
"", "", "", "", "", "", "", "");
l_userid = userid.getText();
l_pass = pass.getText();
if (((l_userid.equals(author.
|| ((l_userid.equals(author3.
{
//openURL(FilePath + "BNPFrame.html");
hide();
}
else
{
userid.setText("Logon Failed!");
pass.setText("Try again SUCKER!!!!");
userid.requestFocus();
}
}
}
// Clears the textfields when the resetButton is clicked
void Quit_Clicked(Event event)
{
userid.setText("");
pass.setText("");
userid.requestFocus();
}
public boolean handleEvent(Event event)
{
if(event.id == Event.WINDOW_DESTROY) {
hide();
return true;
}
if(event.target == quit && event.id == Event.ACTION_EVENT)
{
Quit_Clicked(event);
return true;
}
if(event.target == submit && event.id == Event.ACTION_EVENT)
{
Submit_Clicked(event);
return true;
}
return super.handleEvent(event);
}
//{{DECLARE_CONTROLS
java.awt.Label label2;
java.awt.Label label1;
java.awt.TextField userid;
java.awt.TextField pass;
java.awt.Button quit;
java.awt.Button submit;
symantec.itools.multimedia
//}}
}
In my applet when I call the dialog I have:
TestDialog dlg = new TestDialog((Frame)c, true, this);
I am having trouble compiling the dialog. It says that the applet is undefined. What else do I need to do?
ASKER
I figured it out.
ASKER