How to link javascript functions with applet? - java

I am trying to make a form page in a java applet. Now after making textbox and submit button, how can I validate this text-box? Anyone if can tell me if I can link JS code to applet and if yes, then how?
package com.tcs.applet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JButton;
public class MyApplet extends Applet {
TextField inputLine = new TextField(15);
private JButton button = new JButton("Submit");
public MyApplet() {
add(inputLine);
add(button);
}
}

Package netscape.javascript provides Java code(your applet code) the ability to access the JavaScript engine in the web browser.
for more info you can check following
Netscape Doc

On the action performed of the JButton do your validation.
If there is wrong input in textfield then throw a acception.

Related

nothing appear in my applet?

Why there is no thing appear in my applet ?
I started to learn java using eclipse before two days, but when I come to image part there is no thing appear in my applet
My code is as follows:
import java.awt.*;
import javax.swing.*;
public class CityScape extends JApplet {
setSize(800,500);
super.paint(g);
Image img=getImage(getCodeBase(),"aaa.png");
g.drawImage(img,0,0,this);
}}

getContentPane() not recognized when java.awt is imported

ANSWERED / CLOSED
to make things simple, I've just pulled code directly from the java applet tutorial.
The code is:
private void createGUI() {
JLabel label = new JLabel(
"You are successfully running a Swing applet!");
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
getContentPane().add(label, BorderLayout.CENTER);
}
The only thing is, getContentPane() isn't recognized even though I have the following imports
import java.applet.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JFrame;
could someone please tell me what I have to do to get Eclipse Kepler to recognize getContentPane()?
Thanks

How do I save firefox web page as image

Please help me in saving web page as image using java.
I am using selenium web driver for an application, I need to take screenshot for an alert box.
So I thought it will be better if we have "save as image" button so that I can take the alert screenshot.
I am using firefox web driver
Newer versions of firefox having new feature of executing commands by pressing SHIFT+F2
It helped me in taking alert screenshots, I used robot object for this but not using web driver
You can simply install Firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/fireshot/ and take any screenshot of web page.
This robot function will help you to take screenshot of displaying screen. You can edit it.
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public void captureScreen(String fileName)throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = newRectangle(screenSize);
Robot robot = newRobot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image,"png",newFile(fileName));
}
The following may help you.
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File f = new File("Location to save your image ");

Get page's html source using a Java applet

I know in scripting languages like Python that this is possible but I know that Java applets can't access other servers other than their own.
I don't know/think I can get this applet signed. Is there a way to use PHP to do what I want to accomplish?
I also know that this code will go to google.com
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
public class tesURL extends Applet implements ActionListener{
public void init(){
String link_Text = "google";
Button b = new Button(link_Text);
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent ae){
//get the button label
Button source = (Button)ae.getSource();
String link = "http://www."+source.getLabel()+".com";
try
{
AppletContext a = getAppletContext();
URL u = new URL(link);
// a.showDocument(u,"_blank");
// _blank to open page in new window
a.showDocument(u,"_self");
}
catch (MalformedURLException e){
System.out.println(e.getMessage());
}
}
}
That is assuming that source.getLabel() is "google"
But how would I get the source html of that page?
The source html is dynamic and is updated every few seconds or miliseconds. But, the html is also updated, so I can still read the dynamic content directly from the html. I already did this in vb.net, but now I need to port it to Java, but I can't figure out how to access a page's html source; that's why I'm asking.
AppletContext.showDocument opens a page in the browser, much like a hyperlink in HTML or a similar call in JavaScript would do. Under the Same Origin Policy you will not have access to this page if it is from a different site, even if the page is in an iframe.
Some sites may have a crossdomain.xml policy file that allows access if you were to read the contents of the java.net.URL directly. However, www.google.com appears to be using a restricted form that I don't believe is currently supported by the Java PlugIn.
Someone will probably suggest signing your applet, which turns off the "sandbox" security feature of Java. You would then need to persuade your users to trust your ability to publish safe signed code.

Can a Java Applet minimize its browser window?

My applet pops up in a browser window and then creates a separate JFrame. I'd like to minimize the original hosting browser window. Can I do it from the applet?
You can't minimize a browser window from the Applet or with Javascript. You can try playing around with the Javascript window blur and focus to see if you can make them work for you. Communicating with JS should be possible as follows
import netscape.javascript.*;
import java.applet.*;
import java.awt.*;
class MyApplet extends Applet {
public void init() {
JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject loc = (JSObject) doc.getMember("location");
String s = (String) loc.getMember("href"); // document.location.href
win.call("f", null); // Call f() in HTML page
}
}
Code untested and taken from Oracle docs
No, you can't do it. Your applet does not gain control over the calling window and therefore can't minimize (or otherwise manipulate) that window.

Categories