View Java GUI inside .NET Form / Visual Studio editor? - java

My goal is to write a Visual Studio plugin (a VSPackage) for my Java application. I was wondering if it was possible to view some JPanels inside a System.Windows.Forms instance, or rather as an Microsoft.VisualStudio.Editor.
I was thinking an applet but I'm pretty much stuck there...
Is streaming a Swing component as JPEG and displaying it in a Form an applicable idea?
EDIT:
I would really appreciate answers that are more then a "yes"/"no"/"why would you do this?". I made my mind about working this way, so I ask for:
A detailed solution for achieving my goal, OR,
Good insights/ideas of what my approach should be, OR,
A thorough explanation for why it is impossible to achieve.

( ) Way of the warrior
Load the JVM from your extension (use jvm.dll).
Implement your own Applet Container. Something like this AppletViewer.
Put the Applet Container inside your native form. This is hard, there are ways to get the Applet Container HWND (In Java Swing how do you get a Win32 window handle (hwnd) reference to a window?), but I am not sure a simple SetParent can do the trick.
Load your Applet.
( ) Way of the master
Use NPAPI/NPRuntime, like the browsers do.
Load the npjp2.dll Java Plug-In.
Load your applet.
(X) The solution adopted was:
Run the VS Plugin part written in Java (like an Applet) with JVM in another process (the standard for Java).
Get the HWND using FindWindow (Win32 API).
Use SetParent (Win32 API) and MoveWindow (Win32 API), to dock the window and resize it.
I could name it the "the easiest way", but running Java in another process will give you more stability, so this is the "way that works". My only fear about using a window from another process, was the thread that process Windows messages in Java could interfere in Visual Studio. But from what I see, there is nothing to fear.
:-)

Related

Get Image from an Application

Sorry, this question is pretty broad, but I haven't a clue how to go about it.
In Java, how can I get whatever a Windows application is displaying in the form of an Image?
I'd like the contents of the Image to include just a frame from one application window, not a whole monitor.
I think getting the application window from the title of the window may be a start, but does Java even have access to the GUIs of other processes?
The best you can do in pure Java is to use the Robot class to capture a specified rectangle. The following Q&A show how ... for the case where the rectangle is the entire screen.
Screenshot capture in Window 8 OS
(This should work for all non-headless Java SE platforms.)
That's the easy bit. The hard bit is figuring out what rectangle to screenshot. The Java libraries provide no APIs for finding information about the screen locations of "other" applications. To find that kind of information, your Java application would need to interact with the native OS via a native library or an external utility command. This will inevitably be platform specific ... meaning that it will break for Mac and Linux, and possibly for other versions of Windows as well.
I wouldn't try to do this in Java at all. Instead, I would look for an OS-specific screenshot application that can be run as an external application; e.g. using Process.exec. Alternatively, I'd figure out how to write such an app, in (say) C#.
I think getting the application window from the title of the window may be a start, but does Java even have access to the GUIs of other processes?
Nope and nope.

What is the best way to turn this Java Swing application into a sandboxed embed-able (applet or Web Start)?

I am working on a primitive online game client that I wrote as a Java Swing application. My problem is that potential users refuse to download and run any unfamiliar executable. I want my game to be reach as many people as possible and users fear that the executable might contain a virus. That is why I would like to embed the game client into my web page instead. How would you turn this Java executable code (liked below) into an applet?
Thus far I have a main which makes the initial connection to the game server...
https://dl.dropboxusercontent.com/u/214507961/Main.java
A Java Swing GUI that I made with the Netbeans GUI builder...
https://dl.dropboxusercontent.com/u/214507961/GUI.form
Object input/output streams connected to my game/web server...
https://dl.dropboxusercontent.com/u/214507961/Clients_Input.java
https://dl.dropboxusercontent.com/u/214507961/Clients_Output.java
And some buffered images for graphics...
https://dl.dropboxusercontent.com/u/214507961/Image_Repo.java
Note that in future development, I would like to replace much of the primitive looking 2D Swing GUI with content rendered with the Lightweight Java Game Library or some other higher-level video game graphics specific library to make the game look better.
Given that information, what is the best way to make this Java application into an embed-able (applet)? Describe your method and reasoning in such a way that even a college freshman could follow.
Note: Following the advice below, I have tried using WebStart, but I still get an intimidating warning:
Update:
I am in the process of refactoring my code so that the top level container is a JPanel.
I have tried making "GUI" extend JPanel and changing the "gamewindow" from a JFrame into a JPanel.
Modified Main:
https://dl.dropboxusercontent.com/u/214507961/Panel_Top_Level_Container/Main.java
Modified GUI:
https://dl.dropboxusercontent.com/u/214507961/Panel_Top_Level_Container/GUI.java
Despite the changes, Netbeans will not allow this program to run with Java Web Start.
When I enable WebStart and Build/Clean, Netbeans creates two files:
I then clicked Build/Clean and it generated two files:
master-application.jnlp
https://dl.dropboxusercontent.com/u/214507961/Panel_Top_Level_Container/master-application.jnlp
preview-application.html
https://dl.dropboxusercontent.com/u/214507961/Panel_Top_Level_Container/preview-application.html
but no JAR file.
Still working on it. AI moved the remainder of this question to:
Why can't I get Netbeans with Java Web Start enabled to work on my executable?
At this point I'm so tired of putting up with the security hassles and see little to no way to get around certification warnings without paying. I have decided that it would probably be better to just re-write the entire client in HTML5 and javascript.
The basic concept is to build your app (GUI basicly) without using applet specific technologies (like commmunication with web page via JS etc.) and with JPanel as a top level container insteed of of JFrame (Window would be accceptable too, as JApplet extends Window). If you do so, that you will be able to deploy the same code as standalone application and via JNLP as applet.
The digital signature could not be verified by a trusted source.
This is caused by the app. being 'self signed' by a digital certificate we generate ourselves using the SDK tools. Security was increased recently so that 'self signed' apps. get that scary warning. This has been discussed extensively across SO in recent times.
Try looking at the posts under applet+security for details.
Redo the client using HTML5 and Javascript so that you don't run into any prompts or security warnings.

How do I create an interface with buttons and text?

I've tried different ways of creating different interfaces and applets. One of the main things I'm trying to do is to get these gui's to display text and show multiple buttons with different text leading to different outcomes. What I'm asking is, what code is necessary to create an applet or an interface (because I fail at telling the difference between the two) and how should I approach building and structuring that.
I recommend you use netbeans because it has an editor and is the easy way to create Applet.
https://netbeans.org/kb/docs/web/applets.html
You have three main choices I can think of, others will for sure think of something else:
Create a web server and actually serve a web site with your interface. You can code the interface yourself and communicate with your java server using ajax, you can let GWT among others do that for you. COnsider also frameworks like Struts
Create an applet which will become an embedded object in a web site. This is quickly falling out of grace, and I'd strongly discourage you from doing it, if only because of the pains of java plugins in the browser
Create an application with an interface by using AWT or Swing (which come with Java) or, and this is my personal opinion, more elegantly with SWT
It's hard to tell what will best help you without knowing more of your requirements, but if you are going web, I'd suggest you check out GWT, and if you are going desktop app, look into SWT. Also, please understand these are all well tested frameworks and my preferences are just that, preferences.
To create an interface in eclipse using java you can use swing.
In eclipse:
Create a new Java Project (File -> New -> Java Project).
Right Click on src folder and click New -> Other -> WindowBuilder -> Swing Designer -> [Application Window]/[JApplet].
Add the components that you want (buttons, textfields...).
Seems to me like your trying to graduate from console programs to GUI programs. A GUI works differently from a console program. A console program you have a bunch of loops and if statements, but a GUI program work completely differently. The main difference is that GUI programs are event driven.
With that being said, you want to choose a GUI framework, like Swing. Then decide if you want your application to be web based or desktop. An applet is more for web. If you want a desktop program, then you want a JFrame which will be the top-level container of your application.
You can learn all the components that are available to you in the standard Swing API here. IMO it seems like you need to start from the beginnging, so I would start from the very being of How to Create GUIs with Swing. You will want to pay close attention to section on Writing Event Listeners
If you do want to create an Applet instead of a desktop program, you can see the Applets, where you'll learn how to develop Applets and how to deploy them. You will still need to learn some basics though from the Swing link I mentioned.
Also, before you start using drag and drop gui builder tools, I would strongly urge you to first learn to hand code. It will work best for you in the long run.
I hope this gets you started in the right direction.
The defacto world standard GUI for all platforms is becoming Html5 and css3.
So the easiest way for you to write an interface is in HTML. You don't need an applet unless you have real specific needs like having a constant connection to the server for a chat or whatever. Anyway most of the applet reasons to exist are now resolved in standard HTML. If you want to learn a new langue, try Dart (dartlang.org)
You could use dart to have the more adaptable GUI to standards in the world
I explain why in this blog post
http://1veu.blogspot.com/2013/12/why-i-think-dart-will-detrone-java.html
Naturally even if you need to write a stand alone application with native GUI, HTML5 and css3 are still widely employed along with webkit or native code transformers like PhoneGap.

Netbeans Project Types

I'm building a Java application using NetBeans 7.0. The app is intended for use on Windows, so I guess the tool I'm really making most use of is Java Swing (for the cool, nifty screen elements).
My question relates to NetBeans' process of creating the various project types. The application I'm building is based on the "Java Desktop Application" template (?). My problem is that there seems to be a nice load of bloat built into that, and I can't figure out how to remove most of it without blowing up the app.
As a test, I created a "Java Application"project, but this has the opposite problem -- there's absolutely nothing built into the code, and I can figure out how to add anything. Specifically, a Java Desktop Application project created in NetBeans give me the ability to directly edit the screen layout like in Visual Studio. However, a Java Application does not appear to have this capability, I have no idea how (or even if) this can be addressed.
If I could start with a no-frills Java application, and add some capability to edit its layout and control the function of the screen elements (i.e. make it a windows application, I think), that would be just about perfect.
Does anybody have a suggestion for a minimal, but functional NetBeans application start point??
Thanks,
R.
If you start with a plain Java Application then as you realize you start with a bare bones type application. To add a GUI you can add New>JFRame Form. It adds a class that extends JFrame and Netbeans will recognize that it should open it in the visual editor for you.

Genome browser built in java: Swing and awt or Swing and Processing?

I'm writing a genome browser designed primarily to view the history of chromosomal rearrangements. Right now the project is a series of proof-of-concept demos written using Processing. At this point if I don't make any radical changes the final application will be a web applet with a gui built of swing components that open PApplets to actually show the rearrangements happening.
My question is: Should I give up on processing and switch over to pure Swing/AWT? This is my first big java project. I'm building in eclipse, but I can use netbeans as well. If I could embed PApplet objects inside a JFrame, for example, that would make my day.
processing.core.PApplet extends java.applet.Applet, so it should be possible to embed
a PApplet in a java.awt.Frame, as discussed in the article Applet ⇒ application: Hybrid Switch Hitters. See also, Mixing heavy and light components.
Addendum: From the API, "Processing runs in a Frame and not a JFrame. However, there's nothing to prevent you from embedding a PApplet into a JFrame," except for the limitations mentioned above.
Keep in mind that I know almost nothing about either Processing or your project, so I can only give you general advice.
The question that you should ask yourself before every major design change: what problem am I trying to solve by making this change? If the current architecture works, then you should keep it. If it's not working, then you should start by defining the specific things that are wrong with it (which I notice you didn't do).

Categories

Resources