UIWebView-style Java component - java

I'm new to Java and Java Swing, and I'm creating an application that requires the presentation of web pages. I program for iOS a lot, and the simple solution here would be to load the page in a UIWebView. Is there an easy component for Java Swing? Or, is there an easy way to create something of the sort either in or outside of Swing?
I'm basically looking for something where you put in a URL, and a page loads in the application that looks like a normal page in a web browser.

If your Java program is a desktop application, the browse() method of java.awt.Desktop accepts a URI that may be obtained form a URL. The feature was added in Java SE 6. It's not embedded, but the user's chosen browser may be more appealing.
Addendum: See also How to open a URL in the default browser.

There's no single useful solution. There is the JEditorPane class, but it supports only HTML 4 and my experience with it is awful. For example, HTML comments showed up as plain-text.
If, however, you broaden the search to something outside Swing, there are plenty components that will do the job well. See this related question for a detailed discussion of these.

Related

How can I use HTML & CSS to create the user interface for my java app?

I have just read this article: http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm It shows how to build a java app using the javafx library and also how to use some classes such as WebEngine and WebView to display a web page in the app, basically turning it into a browser.
Here is some relevant info from the article:
The embedded browser component is based on WebKit, an open source web
browser engine. It supports Cascading Style Sheets (CSS), JavaScript,
Document Object Model (DOM), and HTML5.
The embedded browser enables you to perform the following tasks in
your JavaFX applications:
Render HTML content from local and remote URLs
Obtain Web history
Execute JavaScript commands
Perform upcalls from JavaScript to JavaFX
Manage web pop-up windows
Apply effects to the embedded browser
I would basically like to entirely dispense with Java or JavaFX GUI tools, except for those required to display HTML and CSS, as described in the article, and build the entire user interface for my app in HTML and CSS. I would like various HTML buttons to cause events to transpire in my java code.
Does this seem like a good idea? And since it does seem like a good idea to me, I'm also wondering why would anyone ever use any other method to build a GUI in java.
I'm creating an entire desktop application with a single WebView, it is available at github. Basically it's UI is a single HTML file which links a dozen of JS files. To call Java from JS I wrap my requests into json and call a Java facade bean. It is also possible to call JS from Java in same fashion. Though it is possible to call Java directly by invoking a method on a Java bean with parameters of any type, I did have a few application crashes after which I decided to make it completely safe and stay with json. This app uses AngularJS and Twitter Bootstrap to render pages.
I had created a ticket in Oracle's JIRA for better Java integration (JSR-223) inside a WebView and their answer was it could be scheduled for Java 9.
The development is pretty fast, when the process is set up - it's hard to debug the app in the beginning because there is no debugger. Some top-level JS exceptions are not being caught as well. At the moment I'm having no issues with WebView in JavaFX 8. JavaFX 7 is unusable for me because of the problem with fonts.
Answering your last question - I have no idea, but the situation is completely the opposite. For some reason Oracle puts resources for JavaFX native components development, but not for better WebView integration.
You can use a MVC framework such as Struts2 or Spring MVC together with AJAX and build a user interface (the view component) completely with HTML/CSS. Sometimes, using a template engine such as FreeMarker3 also helps replacing default java rendering with pure HTML/CSS solutions.
Vaadin
Vaadin is a sophisticated servlet-based framework that creates server-side based apps that run your pure Java code while automatically rendering the user interface on the client side in HTML, CSS, and JavaScript.
You don't need to know about or do in programming in those web client technologies, Vaadin does all the web-related work for you. No web templates, no pages, none of that. Your Java code simply creates label, field, buttons, and layouts. Vaadin transforms those on-the-fly to be rendered in the browser. Pure Java on the server, no Java at all on the client/browser side.
Xojo
Vaadin is not "yet another web app framework". It really has no direct competitors in terms of architecture except the non-Java Xojo Web Edition which uses its own proprietary OOP language.
It looks a very good idea.
An alternative can be to use Jetty or a similar open source server but you have much more work to do for adapt it to your application.
An other alternative is to build a Java EE application but it is not so much agile for a simple web view of your app, Java EE gives you the possibility to build dynamic web pages using the Java Server Pages and manage the user requests using the Servlets but you have to submit to his structure and this is not useful for your easy application.

Making Web Forms: Java vs HTML

I am new in programming and this question may really be stupid but I still want to ask because I don't know what practices are possible and what are not. You see, right now I am studying basic J2EE. Our assignment is to make a web-based application using JSP and servlets. One of my problems is doing the forms.
I know that HTML is the best practice in making the forms in JSP because almost all people are using it(based on the examples on the books/internet). But Java also have its own forms which is the Swing, AWT and it all worked by using the events. If i don't want to use HTML, can I use Java's Swing/AWT or is it for the desktop applications only? Aside from what I've mentioned, what else can I use?
What you are talking about is called Rich Internet Applications RIA. These are web applications that make your web pages look like desktop applications. You can achieve this with Grails and Flex, GWT and JavaFX. There is a number of very interesting and great environments you can use like Vaadin, Open Laszlo, based on GWT. GWT and the frameworks based on it generate JavaScript and produce separate output for every browser environment.
These are the tools for the future. I do not really understand why these technologies are not mainstream yet.
Here is a list of the various RIAs frameworks available. Expect to welcome newcomers in SourceForge in the near future.
But Java also have its own forms which
is the JSwing, Swing, AWT and it all
worked by using the events.
Yes java has many Components in Swing and AWT.
And both Swing and AWT Components support Event Handling.
But I never have heard of JSwing.
If I don't want to use HTML, can I use
Java's JSwing/Swing/AWT or is it for
the desktop applications only?
Swing and AWT are for the desktop application only.
But as Orbit said
In Appletz? – Orbit
You can definetly use Swing components on an applet and then embed the applet on your JSP page.
I know that HTML is the best practice
in making the forms in JSP
Using HTML will be a lot easier than using Applet to make a form
As Andrew said
Applets are a PITA to develop and
deploy, and most users prefer pure
HTML (or dynamic HTML, using
JavaScript) to applets.
You can try out Wicket - seems like transfer your desktop expirence to web
Perhaps you need to think about what is running where, and how those programs communicate. When you develop a web application, you are co-ordinating a conversation between (at least) two separate machines.
On one side, you are running a web server, that serves up static content (simple files that are just sent out with no changes) and dynamic content (essentially files that are constructed only when needed, and that will change according to their situation - for instance a welcome page that displays the user's name). Plain HTML is static content, JSP and servlets provide dynamic content.
On the other side, you are running a browser. The browser can display static content, run javascript programs built into those pages, and embed specific types of applications. Examples of embedded applications are Flash and Applets.
So you can be clear, JSP and Servlets are NOT running on the client machine, they just deliver different content for the browser to display. A J2EE program is not delivered to the client, it just sits on the server carrying out one side of the conversation with the browser carrying out the other side.
A web form is just a lump of HTML that tells the browser to display text fields, buttons and other controls. The browser knows all about these things, and you don't need to write a program to tell it what to do to display a form and send the content back to the server. A simple form doesn't need to be dynamic - you're asking the same questions every time - so you can write it in pure html.
When the user submits the form, the data is transmitted to the server and you will need to write a program to receive that data and do something with it. That is the role of the servlet, which can receive data and respond - with another web page, probably using JSP to display dynamic content (ie. the answer to the form submission).
If that all sounds complicated, it certainly can be. There are various frameworks (Wicket, Struts and so on) that try to make it easier to develop web applications by hiding some of the details of the conversation, and carrying out common tasks automatically. However, the underlying mechanism is still the same - content from the server is displayed on the browser, which can send back messages such as form contents, or requests for different pages. Understanding the basic mechanism allows you to make informed choices about how you can do more powerful things.
Embedded applications allow you to run more complex software on the client - so you can display animations or custom controls. However, the software is ONLY running on the client, and still needs to communicate data back to the server. The server still needs to be running it's own software to receive that data and do something with it. As such, embedded applications don't make your job any easier, but do allow you more control over how things work on the client. However, a modern web browser gives you a lot of control over how forms are displayed and handled, so you need a good reason to ditch that and use something like Flash or an applet.
For websites, always use HTML (and Javascript, for things like input corrections, alerts and such). The less you use Java and Flash, the better.
Keep in mind that websites must be fast and efficient, users don't want to wait minutes until the Java applet loads.
I would recommend you to do in HTML. Because HTML can do wonnders and you can split your web apps sematically using HTML. And i recommend you to use Wicket(its an framework), where all are been splited. That is, web designing can be done separately using HTML, presentation can be done via CSS and the event handling all can be done via JAVA. And wicket takes care of all these connections too. So have a look on Wicket. And hope it helps you.!

Handling CSS and JavaScript when building a Java browser

My task is to create a simple web browser in Java.
So far it can only read HTML pages.
I'm using standard JEditorPane component to display webpages.
Now I was wondering is there any way you could explain me how can I manage to display at least some simple pages that contain CSS/Javascript.
If you could point me to some useful links or appropriate examples I would be very happy.
Well, my advice would be to look at open source rendering engines such as Gecko - https://developer.mozilla.org/en/Gecko_FAQ
You can embed Gecko with Java using the JREX library - http://jrex.mozdev.org/
Starting from scratch with a problem like this is a very big task, and as your username is AmateurProgrammer, I wouldn't recommend it.
There alrady is some prior art for the Java browser segment.
concerning javascript, you will have to use a javascript interpreter in Java. A renowned one is Rhino (by Mozilla). Its integration may reveals to be an interesting challenge.
concerning CSS, it seems the question has already been asked ...

How can a java library be ported to the web?

How do you take a java library that is for the desktop or an applet and make it so you can use its functions for a web page? I know not all things are meant for the web, but can you call a "regular" java class in an apache/tomcat setup server and it "work"? Do you have to so something to it to get it work with a web page?
I was interested in jgrapht for the web but it looks like all desktop or applet and I don't know that it would be possible to return its graphics to the browser, but maybe its libraries and a generated .png rendered by the browser.
Although there is debate on applets being alive or dead, I am not interested in using one at the moment, as it appears it is dead, albeit with new possible life in the latest update 6u10. I don't know that I want to invest in JavaFX either.
Johnny, the thing is that you have to decide what you mean to do. You could, for example, use JGraphT in server side code; the graph could be visualized in a bunch of different ways, like JGaph or graphviz. Then you would output the visualization in some form that works on the web and put it into your web page. In JGraph, that probably means a print interface that generates SVG or PNG.
Or you could emit the graph structure as JSON with a simple walk of the graph and interpret it on the fly in the browser using Javascript.
It's hard to answer the question generally, because it strongly depends on how you mean to use it.
Yes, it is possible to use a java class or jar file in a web appliaction, but it also depends on what you want to do with it.
For example if the library is for mathematics and you want to return results back to the users on a web based application, this is definitely possible. There are many other examples I can give but they are all the same.
As for displaying charts, I have successfully used JFreeChart in a web application using Apache Wicket (which rocks btw).
So to answer your question, you don't really need to "port" the library, you just need to use it and display it's output to a web page instead of a java desktop GUI.
Of course their might be some libraries that are specifically designed for Java GUI stuff like Swing or AWT. But for the most part there is no difference in using a Java library in a J2EE environment or a J2SE environment.
There isn't a closed-form answer to what you are suggesting: porting a Java Library to a browser's environment and using its functionality.
If you are interested in graphing things, here are a few things you can try:
Open Flash Chart - a scriptable flash app that will produce graphs
Bluff - a Javascript library that produces graphs
processing.js - a port of the excelling 'processing' library to Javascript
As for accessing Java-based functionality in the browser, there are some approaches for that. One is to use a toolset like GWT that complies java into an HTML/Javascript/Java application. Another is to use an AJAX-based technology like Direct Web Remoting. It really depends on what you are trying to accomplish.

Is it possible to display Swing components in a JSP?

I was wondering if I could pop up JOptionPanes or other Swing components from within a browser using JSP.
If you embed an applet. But I don't think that's what you want. Swing is for desktop apps. JSP web pages. If you want components, try looking into JSF or some of the many AJAX Javascript frameworks like prototype.
You may also want to consider GWT, which enables you to develop a web interface in Java code (the Java code is converted to HTML & JavaScript by the GWT compiler). Although you don't program to the Swing API directly when writing GWT applications, the GWT API is very similar in style to Swing programming.
Assuming you're familiar with Swing, you may want to introduce yourself to Apache Wicket which is very similar to the way you build web pages as Swing is to building GUI:s. That is, in Wicket you add new instancef of Label, Button, DataTable etc. etc. to page and link those to bean property data which is then ultimately transformed to fully functioning web page.
As for the actual question, you really can't do it as-is. If choosing a new/different/another web framework to help you isn't possible, the only proper way I can think of is doing what sblundy suggested.

Categories