Is it possible to display Swing components in a JSP? - java

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.

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.

Java Framework for creating Ajax Websites (as opposed to web applications)

I'm looking for a java framework to develop a website that will contain various AJAXy style components on it, but will use tradition page refreshes when switching between pages. I understand that there are a lot of java based frameworks for building web applications (GWT, ZK etc.) but from what I can see these are good for building swing style web applications rather than traditional websites with Ajax components.
So my question is what java frameworks would you suggest using for this case?
Almost any (good) JS component library will allow you to insert the library components (UI widgets) into your multi page application.
Typically, your page loads and on load, you instantiate the (AJAX enabled) UI components and insert them into the page's DOM (usually bind them to some placeholder div tags on the page)
With the above concept out of our way, you want to look for a library or combination of libraries working together that
-Have the UI widgets you are looking for.
-Integrates (fairly easily) with Java.
Following possible options come to my mind
RichFaces with JSF as mentioned by the above poster.
YUI and DWR combination
ExtJS + directjngine
I am sure there are a lot more options but ones listed above are the ones that I have used successfully on production projects.

What are common Servlet-JSP GUI widgets?

I'm just getting involved with Servlets, Tomcat, JSP etc and was wondering what people typically use for GUI's in their JSP pages (for client-side interactivity). I've used jQuery, YUI, extJS, etc. for JS scripting in simple webpages, but what about for web applications? Do these client-side technologies integrate well with JSP pages? Are there tag libraries out there to help with this sort of thing?
Any suggestions?
As a specific example, would it be a good idea to populate a YUI DataTable using JSP embedded in the JavaScript?
For rich GUI, consider looking at JSF. There are lot of JSF component libraries around which offers GUI awesomeness, for example PrimeFaces. It's basically using jQuery / jQuery UI under the covers. Checkout for example the Mock OS X demo.
If you don't want to bow for a component based MVC framework like JSF, then you can also just go ahead with "plain" jQuery / jQuery UI. It shouldn't be that hard on JSP. All JSP basically does is generating and sending some bunch of HTML/CSS/JS code from webserver to webbrowser. All JavaScript/jQuery basically does is intercepting on the HTML DOM tree as the webbrowser has retrieved (in other words, it has totally no knowledge about the server side JSP code which generated/sent it).
You only need to realize that afterwards bringing a component based MVC framework in an existing JSP/jQuery webapplication isn't that easy. Rather look for a request based MVC framework like Spring MVC then.
Related questions
What is the need of JSF when rich UI can be achieved by plain HTML/CSS/jQuery?
You can use any of those libraries. I personally prefer jQuery. I've also used prototype and Ext JS in the past. Some frameworks integrate proprietary or generic Ajax approaches, but I find the popular ones (such as those you listed) to be a better alternative.

Google Web Toolkit -- only for Gmail type applications?

I am trying to build a web application, similar in feel to something like GMail, with one persistent AJAXy page.
I am told that GWT is the way to do this most effectively. However, I also want a framework that I can use for other things besides single AJAXy pages. Is GWT more scalable than this, or should I spend the time and build this thing in ASP.net MVC or Django?
I should mention, I'm trying to LEARN a framework that will be suitable for me to do whatever in.
You can use GWT also for parts of the page (a single widget or two), but yes, you are right,
even then it is geared towards the case where you do not refresh the page it lives on.
GWT works fine for both cases. I've written a couple applications at work with it, and I haven't had any trouble with multi-page navigation. As Thilo also mentioned, you can use as much or as little of GWT for manipulating your page(s) as you need or want.
You can do both styles of applications in ASP.NET MVC or GWT.
GWT makes it easier for GMail-type applications eg: web applications which mimic desktop application functionality.
On the other side, ASP.NET MVC is easier for dynamic sites. EG: This site is built on ASP.NET MVC, and if you look at it, its behaves more like a dynamic and interactive web site rather than a desktop-like application.
So its just easier to do web sites in one, and web applications in the other. So I'd think about what type of app you'd rather spend your time on and choose the framework accordingly - Realising, of course, that you can do both types of applications in either.
The nice thing of GWT is that in the end all you have is HTML and JavaScript. You can combine it with any other technology.
You can use GWT for the one page that needs ajaxy stuff and the rest with something more traditional.
Or you could even write everything with another framework and then put a GWT widget that needs more AJAX behaviours in a dedicated location of your page.
If you want to stay in the Java world you can use JSP,JSF, Struts, Wicket, or any other framework.
If you want you could even just combine GWT with a .NET or PHP. Just don't use the GWT RPC in that case (use JSON or some simple XML encoding instead).

Is Google Web Toolkit similar to AWT and Swing

I've looked breifly into GWT and like the idea that I can develop in Java and have the application compile down to HTML and JavaScript. Is the concept behind GWT and AWT and Swing the same or different?
It is programmed very similarly(patterned after Swing) and the code is 100% java (compiles with a standard Java compiler without errors), but the way it works is very different. Instead of compiling into a Java app, it compiles into Javascript that is sent to your browser.
This ability to program good active Javascript without actually coding Javascript and HTML is pretty nice.
Also, since it programs much like swing, you can do stuff like adding listeners that effect other controls pretty easily.
GWT is very much similar to Swing in its usage of Widgets, Panels and the EventListeners it provides. A different way to look at GWT is to think of Javascript and HTML as Assembly language and GWT as a sort of High level language which generates Javascript and HTML. With GWT its easy to develop desktop-like apps for the web using the same tools you would use for building a desktop app
Define concept.
AWT/Swing are used for desktop Java apps or applets. They both require JVM to run.
GWT is used to translate Java code to Javascript. This only runs on Javascript engines, i.e. browser.
The API design, as stated above, is similar to Swing. You get the same Panels, Buttons and other Component classes as in Swing.
AWT and Swing are for programming stand-alone applications (and to a lesser extent, applets). GWT is supposed to make programming web applications similar to stand-alone apps. I wouldn't program a non-web app using GWT.
GWT is conceptually similar to Swing, but is more a replacement for JSPs than anything else.
GWT is a javascript toolkit that allows you to write the javascript in Java. The only real similarities to Swing and AWT are that they are toolkits for creating user interfaces and they use Java. The end product is different and the real purpose for using them is different. GWT gives you the ability to generate a ajax user interface for a web browser while the other 2 give you a console (or applet) java app. In my mind the real reason for using GWT is to get a quick ajax interface up for prototyping purposes. But I dont think its really production ready- ie I dont thing Google uses it in their own webapps. A better choice for more robust ajax webapps is http://developer.yahoo.com/yui/ or http://script.aculo.us/.
disclamer: While I do work at IT Mill, this is just for information to the original poster. This isn't marketing spam.
If you like the idea of being able to write Java and get a webpage out of that, you might be interested in IT Mill Toolkit. It's a toolkit for making RIA software on top of a J2EE stack, and it uses GWT heavily.
One of the basic ideas are that the code generated by GWT is just as prone to client-side forging attacks as any other JS/Ajax-traffic. IT Mill Toolkit makes sure that the data is validated server-side too.
The toolkit is also designed in such a way that if you are familiar to Swing, you should have no problems picking it up.

Categories