JavaFX2: Webview: Page shows empty screen - webview

I tried displaying the google web page on my javafx view using webview. All it does is display an empty page. For testing I did add a text element at the bottom and it did show up. Any pointers would be helpful. My code and the sample screen are attached.
I am running this application on a Windows 7 machine with 8 GB RAM and this is deployed in an environment that needs proxy authentication.
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class MyBrowser extends Application
{
private Pane root;
#Override
public void start(final Stage stage) throws URISyntaxException
{
root = new VBox();
List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://www.google.com"));
final Proxy proxy = proxies.get(0); // ignoring multiple proxies to simplify code snippet
if (proxy.type() != Proxy.Type.DIRECT)
{
// you can change that to dialog using separate Stage
final TextField login = new TextField("login");
final PasswordField pwd = new PasswordField();
Button btn = new Button("Login");
btn.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent t)
{
System.setProperty("http.proxyUser", login.getText());
System.setProperty("http.proxyPassword", pwd.getText());
displayWebView();
}
});
root.getChildren().addAll(login, pwd, btn);
}
else
{
displayWebView();
}
stage.setScene(new Scene(root, 400, 600));
stage.show();
}
private void displayWebView()
{
root.getChildren().clear();
WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
root.getChildren().addAll(webView, new Text("HELLO"));
webEngine.load("http://www.google.com");
}
public static void main(String[] args)
{
launch();
}
}

I copied and pasted your code and ran it on Windows 7 with Java7u40 both Java8b108.
In both cases the code functioned correctly and displayed the http://www.google.com page.
The proxy selector code in your source was not triggered for me (probably because I have a Proxy.Type.DIRECT connection, so there was nothing for it to do).

Related

Integrate KoliBri web-components in Vaadin

I am trying to integrate KoliBri web-components (https://github.com/public-ui/kolibri) in a Vaadin project. I followed the documentation for web components integration (https://vaadin.com/docs/latest/create-ui/web-components) but I was not successful.
I want to integrate a KoliBri-button (kol-button) and therefor created getter and setter methods for the required properties of the button. When loading the website, the kol-button-component is loaded successfully from the .js file.
enter image description here
But the kol-button element in the DOM is empty and won´t show up:
enter image description here
Here is my KolButton.java:
package com.example.application.views.helloworld;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Synchronize;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
#Tag("kol-button")
#NpmPackage(value = "#public-ui/components", version = "1.1.10")
#JsModule("#public-ui/components/dist/components/kol-button")
public class KolButton extends Component {
public boolean getLabel() {
return getElement().getProperty("_label", false);
}
public void setLabel(String label) {
getElement().setProperty("_label", label);
}
public void setVariant(String variant) {
getElement().setProperty("_variant", variant);
}
public boolean getVariant() {
return getElement().getProperty("_variant", false);
}
}
And the view.java:
package com.example.application.views.helloworld;
import com.example.application.views.MainLayout;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
#PageTitle("Hello World")
#Route(value = "hello", layout = MainLayout.class)
#RouteAlias(value = "", layout = MainLayout.class)
public class HelloWorldView extends HorizontalLayout {
public HelloWorldView() {
var kolButton = new KolButton();
kolButton.setLabel("TestText");
kolButton.setVariant("danger");
setVerticalComponentAlignment(Alignment.END, kolButton);
add(kolButton);
}
}
Do you have any idea to solve this? Thanks in advance

I want to scroll down to Specific element using appium in Android, java client v-5.0.4 and appium v-1.7.1

I am trying to scroll down to an element, looked and searched everywhere and no code is helping to scroll down, i have tried with the code below which seems to be not working, anyone gives me the solution to scroll down perfectly.
As swipe and scrollTo functions are depreciated in the latest java client version, a perfect code will help me to solve my TASK
package mobileapp.com.example;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
public class Day5 {
AndroidDriver<WebElement> driver;
#AndroidFindBy (uiAutomator = "new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().textContains(\"PHP\"))")
public WebElement scrollStepOne;
#BeforeTest
public void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "6.0.1");
capabilities.setCapability("deviceName","Nexus 5");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.vector.guru99");
capabilities.setCapability("appActivity","com.vector.guru99.BaseActivity");
driver= new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void StartTest() throws InterruptedException {
//Verify Homepage
if(driver.findElement(By.id("android:id/action_bar_title")).isDisplayed())
System.out.println("Home page is displayed");
else
System.out.println("Home page is not displayed");
//step2 - click on Course List tab
driver.findElement(By.name("Course List")).click();
System.out.println("Courses list are : ");
Thread.sleep(3000);
//Step 3 - darg until PHP course found and click on it
scrollStepOne.click();
Thread.sleep(3000);
// driver.findElement(By.xpath("//android.widget.TextView[#text()='PHP']")).click();
// Thread.sleep(3333);
//Step 4 - Click on lesson 1 and verify
driver.findElement(By.xpath("//android.widget.TextView[#text='What is PHP? Write your first PHP Program']")).click();
Thread.sleep(3333);
if(driver.findElement(By.id("com.vector.guru99:id/lesson_title")).isDisplayed())
System.out.println("First Lesson is displayed");
else
System.out.println("First lesson not opened");
}
#AfterTest
public void tearDown() {
driver.quit();
}
}
for scrolling to a element have a text as App_Settings, you can use the below
androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +
".scrollIntoView(new UiSelector().description(\"App_Settings\"));");

JavaFX WebView doesn't execute JavaScript

I am on Windows 8. I have a JavaFX app that creates a simple window with a webview control and loads the local file "test.html". "test.html" contains javascript (a simple "alert("hello");<(script>").
However, the javascript is ignored.
What rendering engine does webview in JavaFX use?
I have Firefox and IE installed. The latter executes JS contained in local files only if the users accepts to do so. So my assumption is that webview uses IE due to some configuration of my OS. Is this possible?
Thanks for your help.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
public class Hello extends Application {
#Override
public void start(final Stage stage) {
stage.setWidth(400);
stage.setHeight(500);
Scene scene = new Scene(new Group());
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);
String filepath = this.getClass().getResource("test.html").toExternalForm();
webEngine.load(filepath);
scene.setRoot(scrollPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The JavaFX WebView does not visually show a popup when you use the alert() function in javascript. Instead it raises an event in java that you can handle like this:
myWebView.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
System.out.println("JS alert() message: " + wEvent.getData() );
});

Vaadin refresher extension doesn't work

I am trying to make a simple usage of refresher plug-in as below but it doesn't seem to work. I am tried to run this example in Chrome and in firefox. I am exporting the project as WAR and deploying it in tomcat webapps folder.
package com.example.vaadinapp;
import javax.servlet.annotation.WebServlet;
import com.github.wolfie.refresher.Refresher;
import com.github.wolfie.refresher.Refresher.RefreshListener;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
#SuppressWarnings("serial")
#Theme("vaadinapp")
public class VaadinappUI extends UI {
Refresher refresher;
Label timeLabel;
Page page;
#WebServlet(value = "/*", asyncSupported = true)
#VaadinServletConfiguration(productionMode = false, ui = VaadinappUI.class)
public static class Servlet extends VaadinServlet {
}
#Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
refresher = new Refresher();
page = this.getPage();
timeLabel = new Label(String.valueOf(page.getWebBrowser().getCurrentDate()));
refresher.setRefreshInterval(1000);
refresher.addListener(new RefreshListener() {
#Override
public void refresh(Refresher source) {
timeLabel.setCaption(String.valueOf(page.getWebBrowser()
.getCurrentDate()));
}
});
addExtension(refresher);
layout.addComponent(timeLabel);
}
}
What am I doing wrong here? I also tried the same example with SimpleDateFormat instead of using WebBrowser getCurrentDate() but still the same issue
since vaadin 7.2 you don't need the refesher addon. just enable push support
https://vaadin.com/book/vaadin7/-/page/advanced.push.html

How to bind a stage properties without listener?

Is behavior like this a bug in javafx2 binding?
It seems logical that standalone application itself is always self-informed about own state (xPosition, yPosition, height, width, and so on).
I'm reading book "Pro JavaFX 2 A Definitive Guide to Rich Clients with Java Technology" and some of code examples simply doesn't work. Note: The code I used here is mine. (Is this book relevant?)
package simplejavafxbindingquestion;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.Pane;
import javafx.scene.layout.PaneBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
public class SimpleJavaFXBindingQuestion extends Application {
#Override
public void start(final Stage stage) {
Text textBoundStageHeight;
Text textRequestedStageHeight;
final Label labelRequestedStageHeight;
VBox vBox;
HBox hBox;
Button btnRequest;
Pane sceneRoot = PaneBuilder.create()
.children(
vBox = VBoxBuilder.create()
.spacing(10)
.children(
textBoundStageHeight = TextBuilder.create()
.textOrigin(VPos.TOP)
.font(Font.font("Ariel", 20))
.build(),
hBox = HBoxBuilder.create()
.children(
textRequestedStageHeight = TextBuilder.create()
.textOrigin(VPos.TOP)
.font(Font.font("Ariel", 20))
.text("requested stage height: ")
.build(),
labelRequestedStageHeight = LabelBuilder.create()
.font(Font.font("Ariel", 20))
.text("Unknown")
.build()
)
.build(),
btnRequest = ButtonBuilder.create()
.text("Request height!")
.font(Font.font("Ariel", 15))
.onAction(new EventHandler<javafx.event.ActionEvent>() {
#Override
public void handle(javafx.event.ActionEvent event) {
labelRequestedStageHeight.setText("" + stage.getHeight());
}
})
.build()
)
.build()
)
.build();
textBoundStageHeight.textProperty()
.bind(new SimpleStringProperty("bound stage height: ")
.concat(stage.getHeight()));
Scene scene = SceneBuilder.create()
.root(sceneRoot)
.build();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
By saying "this behavior" I assume you are mentioning "stage.getHeight()" in
textBoundStageHeight.textProperty()
.bind(new SimpleStringProperty("bound stage height: ")
.concat(stage.getHeight()));
That behavior is not a bug, as documented in concat javadoc:
... Also if the other Object is an implementation of ObservableValue,
changes in the other Object are reflected automatically in the
resulting StringExpression.
So since stage.getHeight() is not an ObservableValue, the changes of height value will not observed. On other hand stage.heightProperty() implements ObservableValue:
textBoundStageHeight.textProperty()
.bind(new SimpleStringProperty("bound stage height: ")
.concat(stage.heightProperty()));

Resources