call a java method from javascript in javafx - javafx

I have a javafx application. Initially it loads a login page using WebView. Login page takes user name and redirects to another page. In this html page I have a function inside javascript. I want to call a java method while executing the script. but I end up getting an error saying
ReferenceError: Can't find variable: OpenDoc[at 17]
This my html
html>
<body onload="login()">
<div id="jnlpStart_EN">
<H2>Welcome to Home Page</H2>
</div>
</body>
<script type="text/javascript">
function login() {
OpenDoc.passDocId('q56wre');
}
</script>
</html>
This is my java code
public class WebEngineTest1 extends Application {
#Override
public void start(Stage primaryStage) {
WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
System.out.println(message + "[at " + lineNumber + "]");
});
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.load("http://localhost:8001/app/login");
engine.locationProperty().addListener((obs, oldLocation, newLocation) -> {
if (newLocation != null && newLocation.endsWith("/home")) {
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("OpenDoc", new OpenDoc());
}
});
Scene scene = new Scene(webView, 300, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public class OpenDoc {
public void passDocId(String id) {
System.out.println(id);
}
}
}

I have found the answer.
Since after login it redirects to an URL. I had to add a listener with documentProperty(). Inside I add the code for calling java method from javascript. So while loading the page I don't get ReferenceError: Can't find variable: OpenDoc[at 17] message since I added the reference already. here is the code
engine.documentProperty().addListener((v, o, n) -> {
String newLocation = webEngine.getLocation();
if (newLocation != null && newLocation.endsWith("/home")) {
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("OpenDoc", new OpenDoc());
}
});

First you have to enable JavaScript on the WebEngine
webEngine.setJavaScriptEnabled(true);
and this line will do the trick
webengine.executeScript("initOpenDoc(\"ID\")");

Related

Master Detail Page is not appearing after Content page

I am working in xamarin.forms. I am creaing an android application. In my application I have to use menu. So I took Master detail page to show the menus. And its working fine.
But my problem is before showing the Master detail page, I have to open a content page which doesn't contain the menus. So I took a content page and set it. But when I am running the application after content page, Master detail page is not appearing. The code is running successfully but the Master page is not appearing.
Can any one tell me how I can show the Master detail page after showing simple content page?
The answer will depend on if you want to maintain the navigation stack or not. If you want to add the new page to the current Navigation Stack then you need to so something like this in the Content Page:
((NavigationPage)Parent).PushAsync(newPage);
If you want to make the new page the root of the Navigation Stack then you need to do something like this:
((App) Parent).MainPage = newPage;
If this doesn't work, post your code.
Giving you a trick ! suppose you have a login page ,after authentication you will go to RootPage which is a masterDetailPage .
Take a hint from from below code
namespace LoginNavigation
{
public class App : Application, IloginInterface
{
public static App current;
public static bool IsUserLoggedIn { get; set; }
public static double ScreenWidth;
public static double ScreenHeight;
public App () {
current = this;
MainPage = new LoginPageWithStack ();
}
public void Logout() {
MainPage = new LoginPageWithStack ();
}
public void ShowMainPage() {
MainPage = new RootPage ();
}
}
}
rootPage:
namespace LoginNavigation
{
public class RootPage:MasterDetailPage
{
MenuPage menuPage;
public RootPage () {
ToolbarItems.Add(new ToolbarItem("Filter", "ring.png", async () => {
var page = new ContentPage();
var result = await page.DisplayAlert("Title", "Message", "Accept", "Cancel");
Debug.WriteLine("success: {0}", result);
}));
menuPage = new MenuPage ();
menuPage.Menu.ItemSelected += (sender, e) => NavigateTo (e.SelectedItem as MenuItemForMaster);
//Master = new MasterMenu();
Master = menuPage;
Detail = new NavigationPage (new TimeSheet()){
BarBackgroundColor = Color.FromHex("008dce"),BackgroundColor = Color.FromHex("008dce")
};
}
void NavigateTo (MenuItemForMaster menu) {
if (menu == null)
return;
Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
//Detail = displayPage;
Detail = new NavigationPage (displayPage) { BarBackgroundColor = Color.FromHex("008dce"),BackgroundColor = Color.FromHex("008dce")};
menuPage.Menu.SelectedItem = null;
IsPresented = false;
}
}
}
So the trick is ,get the current instance of App class and manipulate Mainpage property of it .

Xamarin forms MasterDetail and PageRenderer

Situation:
Building an application using Xamarin Forms and MasterDetail component.
Question:
How Can I render a specific page on Android based on a PageRender? and keep the Drawer?
Edit
public class MasterBacASable : MasterDetailPage
{
public MasterBacASable ()
{
Icon = null;
Title = "The title";
Detail = (new FirstPage ());
Master = new AppMenuPage ();
}
}
[assembly:ExportRenderer (typeof(BacASable.FirstPage), typeof(BacASable.Droid.FirstPageContentRennderer))]
namespace BacASable.Droid
{
public class FirstPageContentRennderer : PageRenderer
{
public FirstPageContentRennderer ()
{
}
protected override void OnElementChanged (ElementChangedEventArgs<Page> e)
{
base.OnElementChanged (e);
var activity = this.Context as Activity;
var v = activity.LayoutInflater.Inflate (Resource.Layout.AndroidView,this,false);
AddView (v);
}
}
}
Follow this for Xamarin.Forms Master-Detail Documentation
The base Concept is the following
public class MainPageCS : MasterDetailPage
{
MasterPageCS masterPage;
public MainPageCS ()
{
masterPage = new MasterPageCS ();
Master = masterPage;
Detail = new NavigationPage (new ContactsPageCS ());
...
}
}
Your Master is your drawer and Detail the Page (ContentPage, TabbedPage,NavigationPage,CustomPageRenderer ).
So each time you want to display a different page set the Detail property
Detail = new MyContentPage();
I was just forgetting to override the OnLayout in the Renderer.
Thank you all.

Open JavaFX virtual keyboard programmatically

I have partially solved the following problem: JavaFX WebView / WebEngine show on-screen-keyboard automatically for each text input
I stucked at the 6th point because I would like to use the built in JavaFX virtual keyboard but I can not find any reference how can trigger the displaying of it.
Do you know any solution for this? If it is possible I do not want to use 3rd party library.
I am going to answer my question because I found a solution.
First of all I added an event listener for all input tags on webpage, after page loaded:
private void addEventListenersToDOM() {
webview.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
#Override
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) webview.getEngine().executeScript("window");
win.setMember("javaFXVirtualKeyboard", new JavaFXVirtualKeyboard());
String script =
"var inputsList = document.getElementsByTagName('input');"
+ "for (var index = 0; index < inputsList.length; ++index) { "
+ "inputsList[index].addEventListener('focus', function() { javaFXVirtualKeyboard.show() }, false); "
+ "inputsList[index].addEventListener('focusout', function() { javaFXVirtualKeyboard.hide() }, false); "
+ "}";
webview.getEngine().executeScript(script);
}
}
});
}
And the key point, how I triggering the keyboard displaying and hiding:
public class JavaFXVirtualKeyboard {
public void show() {
FXVK.init(webview);
FXVK.attach(webview);
}
public void hide() {
FXVK.detach();
}
}
One note: FXVK class is not an API so we get a warning message in all cases but it works without any bug.
Discouraged access: The type 'FXVK' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_91\lib\ext\jfxrt.jar')

Handle multiple JavaFX application launches within a loop

My code currently reads my Gmail inbox via IMAP (imaps) and javamail, and once it finds an email with zip/xap attachment, it displays a stage (window) asking whether to download the file, yes or no.
I want the stage to close once I make a selection, and then return to the place within the loop from which the call came. My problem arises because you cannot launch an application more than once, so I read here that I should write Platform.setImplicitExit(false); in the start method, and then use primartyStage.hide() (?) and then something like Platform.runLater(() -> primaryStage.show()); when I need to display the stage again later.
The problem occuring now is that the flow of command begins in Mail.java's doit() method which loops through my inbox, and launch(args) occurs within a for loop within the method. This means launch(args) then calls start to set the scene, and show the stage. Since there is a Controller.java and fxml associated, the Controller class has an event handler for the stage's buttons which "intercept" the flow once start has shown the stage. Therefore when I click Yes or No it hides the stage but then just hangs there. As if it can't return to the start method to continue the loop from where launch(args) occurred. How do I properly hide/show the stage whenever necessary, allowing the loop to continue whether yes or no was clicked.
Here is the code for Mail.java and Controller.java. Thanks a lot!
Mail.java
[Other variables set here]
public static int launchCount = 0;#FXML public Text subjectHolder;
public static ReceiveMailImap obj = new ReceiveMailImap();
public static void main(String[] args) throws IOException, MessagingException {
ReceiveMailImap.doit();
}
#Override
public void start(Stage primaryStage) throws Exception {
loader = new FXMLLoader(getClass().getResource("prompts.fxml"));
root = loader.load();
controller = loader.getController();
controller.setPrimaryStage(primaryStage);
scene = new Scene(root, 450, 250);
controller.setPrimaryScene(scene);
scene.getStylesheets().add("styleMain.css");
Platform.setImplicitExit(false);
primaryStage.setTitle("Download this file?");
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void doit() throws MessagingException, IOException {
Folder inbox = null;
Store store = null;
try {
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect("imap.gmail.com", "myAccount#gmail.com", "Password");
inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
Message[] messages = inbox.getMessages();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(UIDFolder.FetchProfileItem.FLAGS);
fp.add(UIDFolder.FetchProfileItem.CONTENT_INFO);
fp.add("X-mailer");
inbox.fetch(messages, fp);
int doc = 0;
int maxDocs = 400;
for (int i = messages.length - 1; i >= 0; i--) {
Message message = messages[i];
if (doc < maxDocs) {
doc++;
message.getSubject();
if (!hasAttachments(message)) {
continue;
}
String from = "Sender Unknown";
if (message.getReplyTo().length >= 1) {
from = message.getReplyTo()[0].toString();
} else if (message.getFrom().length >= 1) {
from = message.getFrom()[0].toString();
}
subject = message.getSubject();
if (from.contains("myAccount#gmail.com")) {
saveAttachment(message.getContent());
message.setFlag(Flags.Flag.SEEN, true);
}
}
}
} finally {
if (inbox != null) {
inbox.close(true);
}
if (store != null) {
store.close();
}
}
}
public static boolean hasAttachments(Message msg) throws MessagingException, IOException {
if (msg.isMimeType("multipart/mixed")) {
Multipart mp = (Multipart) msg.getContent();
if (mp.getCount() > 1) return true;
}
return false;
}
public static void saveAttachment(Object content)
throws IOException, MessagingException {
out = null; in = null;
try {
if (content instanceof Multipart) {
Multipart multi = ((Multipart) content);
parts = multi.getCount();
for (int j = 0; j < parts; ++j) {
part = (MimeBodyPart) multi.getBodyPart(j);
if (part.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
saveAttachment(part.getContent());
} else {
int allow = 0;
if (part.isMimeType("application/x-silverlight-app")) {
extension = "xap";
allow = 1;
} else {
extension = "zip";
allow = 1;
}
if (allow == 1) {
if (launchCount == 0) {
launch(args);
launchCount++;
} else {
Platform.runLater(() -> primaryStage.show());
}
} else {
continue;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if ( in != null) { in .close();
}
if (out != null) {
out.flush();
out.close();
}
}
}
public static File createFolder(String subject) {
JFileChooser fr = new JFileChooser();
FileSystemView myDocs = fr.getFileSystemView();
String myDocuments = myDocs.getDefaultDirectory().toString();
dir = new File(myDocuments + "\\" + subject);
savePathNoExtension = dir.toString();
dir.mkdir();
System.out.println("Just created: " + dir);
return dir;
}
}
Controller.java
public class Controller implements Initializable {
#FXML
private Text subjectHolder;
public Button yesButton, noButton;
public ReceiveMailImap subject;
#Override
public void initialize(URL url, ResourceBundle rb) {
subject= new ReceiveMailImap();
subjectHolder.setText(subject.returnSubject());
}
public Stage primaryStage;
public Scene scene;
#FXML
ComboBox<String> fieldCombo;
public void setPrimaryStage(Stage stage) {
this.primaryStage = stage;
}
public void setPrimaryScene(Scene scene) {
this.scene = scene;
}
public String buttonPressed(ActionEvent e) throws IOException, MessagingException {
Object source = e.getSource();
if(source==yesButton){
System.out.println("How to tell Mail.java that user clicked Yes?");
return "POSITIVE";}
else{subject.dlOrNot("no");
System.out.println("How to tell Mail.java that user clicked No?");
primaryStage.hide();
return "NEGATIVE";}
}
}
There are a lot of issues with the code you have posted, but let me just try to address the ones you ask about.
The reason the code hangs is that Application.launch(...)
does not return until the application has exited
In general, you've kind of misunderstood the entire lifecycle of a JavaFX application here. You should think of the start(...) method as the equivalent of the main(...) method in a "traditional" Java application. The only thing to be aware of is that start(...) is executed on the FX Application Thread, so if you need to execute any blocking code, you need to put it in a background thread.
The start(...) method is passed a Stage instance for convenience, as the most common thing to do is to create a scene graph and display it in a stage. You are under no obligation to use this stage though, you can ignore it and just create your own stages as and when you need.
I think you can basically structure your code as follows (though, to be honest, I have quite a lot of trouble understanding what you're doing):
public class Mail extends Application {
#Override
public void start(Stage ignored) throws Exception {
Platform.setImplicitExit(false);
Message[] messages = /* retrieve messages */ ;
for (Message message : messages) {
if ( /* need to display window */) {
showMessage(message);
}
}
}
private void showMessage(Message message) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("prompts.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
Scene scene = new Scene(root, 450, 250);
stage.setScene(scene);
stage.initStyle(StageStyle.UNDECORATED);
stage.setTitle(...);
// showAndWait will block execution until the window is hidden, so
// you can query which button was pressed afterwards:
stage.showAndWait();
if (controller.wasYesPressed()) {
// ...
}
}
// for IDEs that don't support directly launching a JavaFX Application:
public static void main(String[] args) {
launch(args);
}
}
Obviously your logic for decided whether to show a window is more complex, but this will give you the basic structure.
To check which button was pressed, use showAndWait as above and then in your controller do
public class Controller {
#FXML
private Button yesButton ;
private boolean yesButtonPressed = false ;
public boolean wasYesPressed() {
return yesButtonPressed ;
}
// use different handlers for different buttons:
#FXML
private void yesButtonPressed() {
yesButtonPressed = true ;
closeWindow();
}
#FXML
private void noButtonPressed() {
yesButtonPressed = false ; // not really needed, but makes things clearer
closeWindow();
}
private void closeWindow() {
// can use any #FXML-injected node here:
yesButton.getScene().getWindow().hide();
}
}

ASP.NET Back Button for multiple forms

I am trying to track navigation between a number of Web Forms in ASP.NET. I've tried the client side back navigation using the following:
<asp:Button ID="BackButton" runat="server" Text="Back"
OnClientClick="JavaScript:window.history.back(1);return false;" />
Unfortunately this does not work for my scenario due to postbacks going on. My scenario has a number of Web Forms:
Page1.1
Page1.2
Page2
Page3
Navigating forward through the pages works similarly to a wizard. There are 2 starting points - from Page1.1 and Page1.2.
Page1.1 -> Page2 -> Page3
Page1.2 -> Page2 -> Page3
So clicking back buttons will have the following navigation:
Page3 -> Page2
Page2 -> Page1.1
Page2 -> Page1.2
There are additional parameters passed between the pages which need to be maintained.
I am currently looking at maintaining something in the Session to maintain the current call stack which somewhat works however, I am getting a build up of referrer urls. At the minute I am just trying to conceptualise this.
I am running this in SharePoint as Application Pages, however each page is essentially an ASP.NET page for the sake of this example.
So I have introduced an abstract class for each Page:
public abstract class SecureLayoutsPageBase : System.Web.UI.Page
{
private PageController _pageController;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_pageController = (PageController)Session["PageController"];
if (_pageController == null)
{
_pageController = new PageController();
Session["PageController"] = _pageController;
}
if (!Page.IsPostBack && Page.Request.UrlReferrer != null)
{
this.PageController.History.Push(Page.Request.UrlReferrer.ToString());
}
}
protected PageController PageController
{
get
{
return _pageController;
}
}
}
Which has an instance of PageController:
[Serializable()]
public class PageController
{
private Stack<string> _history = new Stack<string>();
public void Previous(HttpResponse response)
{
string previous = _history.Pop();
response.Redirect(previous);
}
public Stack<string> History
{
get
{
return _history;
}
}
}
Then each page will call the PageController.Previous in the server side event handler for the back button click:
protected void BackButton_Click(object sender, EventArgs e)
{
this.PageController.Previous(this.Response);
}
This issue with this is that calling PageController.Previous still results in the Url being added to the stack. I am just wondering if there is a way to prevent the url getting added when back has been clicked. Or alternative solutions...
History(-1) wont work, because this will include postbacks. Just set the button href dependant on whatever page you're on. If you know itsloaded page 3, set the back button to page 2
OK... couple of tweaks to get my scenario working. Not keen on this solution so any others would be good.
Change to SecureLayoutsPageBase:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_pageController = (PageController)Session["PageController"];
if (_pageController == null)
{
_pageController = new PageController();
Session["PageController"] = _pageController;
}
if (!Page.IsPostBack && Page.Request.UrlReferrer != null && Page.Request.Url.ToString() != this.PageController.PreviousUrl)
{
this.PageController.AddHistory(Page.Request.UrlReferrer.ToString());
}
}
Change to PageController:
[Serializable()]
public class PageController
{
private Stack<string> _history = new Stack<string>();
private string _previous;
public void Previous(HttpResponse response)
{
_previous = _history.Pop();
response.Redirect(_previous);
}
public void AddHistory(string url)
{
if(url != _previous)
{
_history.Push(url);
}
}
public Stack<string> History
{
get
{
return _history;
}
}
public string PreviousUrl
{
get
{
return _previous;
}
}
}

Resources