Swing GUI usage on daily basis - java

I am new to Java Swing. I just started to learn about Swing.
I see these tutorial videos to develop GUI for ones needs using Net-beans/Eclipse. My question might be simple/funny to most of you guys.
Lets say GUI is developed to do basic calculation like addition/multiplication and when the project is run on Eclipse/Net-beans its doing perfectly fine.
How do we bundle the code and give it to users so that they can the GUI to do any calculation?
Is it something that will be bundled as setup or exe file?
Basically, how do other use this UI?

What you're looking for is called Building which creates a jar file (.jar) and contains intermediate code (bytecode) which allows users to run the code like an executable (.exe) file.
Remember: You'll need a place for the program to start from (you probably already have this if you can run the program from your IDE) (ie: public static void main (String[] args) { ... })
Refer to this link for building in netbeans, and this one for building in eclipse.

Related

How to generate UI tests from a dsl ?

I'm not sure this question belongs to SO since it is maybe to broad, but I don't know where to ask it (I did not find a better stackexchange site).
Context
I'm using UiAutomator to write some Ui test on android. I created some functions to simplify the write of the tests like the one in the doc
public void startMainActivityFromHomeScreen() {
/*Start the app from the home screen*/
}
As a developper, this works fine. But non technical peoples (contracting owner) can't easily use this functions to write tests.
Needs
I'm searching for a way for non technical users to write some scripts using the function I already defined. Here is a dummy example (both script format and actions)
Suite: Launch the app twice from the home screen
Case: Launch the app for the first time
Do startMainActivityFromHomeScreen
Expect ...
Case: Launch the app for the second time
Do startMainActivityFromHomeScreen
Expect ...
The important point here is to interact with java functions. I know other tools like calabash but it does not provide java interfaces.
Current approach
Here is an idea (nothing implemented)
Put all the functions in a lib
Write a groovy dsl (because groovy interact well with java) which allows non technical users to easily write scripts
Create a java program which will evaluate the groovy script and generate the android code source associated (with the lib (from (1)) as a gradle dependency).
Run gradle androidTestCompile
Since the functions are in a lib, developers can easily include and use it into their projects. So the same lib can be used for all users.
I hate this idea since I have to generate code source from my code, but this is the only one I have.
Questions
Is this approach as horrible as I think it is ?
Do you know another way to do it ?
I'd check out https://cucumber.io/docs/reference/jvm#java, it's a library that accomplishes pretty much exactly what you're looking for by letting you associate regex's with Java test methods.
Your Java code would look something like:
#When("^I open the app from the (main|home) screen$")
public void openApp(String launchScreen) {
...
}
And the testing file would look like:
Feature: Launching app
Scenario: Launching from first screen
When I open the app from the main screen
Then I see a blue icon...
Scenario: Launching from second screen
When I open the app from the second screen
Then I see a green icon...

Testing java web start application using jemmy

I need to create some gui tests using Jemmy but I have no idea how to launch it with javaws application.
In tutorials/examples/etc is something like that:
new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser")
.startApplication();
This code opens an example window, but how can I, using ClassReference object open *.jnlp file? Or is it another way to "connect" jemmy with java web start application?
Thanks for advance.
You can achieve this by preparing special build with Jemmy included and call Jemmy from the app run in jnlp mode itself.
The solution is:
Place build files into any folder on your PC.
Add .jar files from this build to your testing project.
Open .jnlp file in text editor and search for main application class
application-desc main-class="[Main application class]"/>
Call it from you test to launch app
new ClassReference("[Main application class]").startApplication();
Now you can access elements of this Java app from test environment
You can use Jemmy with JUnit in NetBeans IDE.
For the GUI testing we are using Jemmy, a library that comes with the NetBeans IDE and is very useful for testing Swing applications.
JUnit tests that utilize Jemmy so for example:
#Test
public void JunitTest() {
JFrameOperator mainFrame = new JFrameOperator();
JTextFieldOperator textField = new JTextFieldOperator(mainFrame, "textIn");
int x = 10;
assertEquals(x, textField.getLocationOnScreen().x);
}
Adding the #Test annotation and making use of JUnit's assertEquals() and fail() if needed.
Another Approach:
You can also do it using jnlp. Already suggested #Sergey Grinev
A good example and running code is given in this link:
Snapshot:
Click the jnlp file link in webdriver, save jnlp file to disk;
Run the webstart app from jnlp;
Capture opened app and use it for test.
This process can be done by using following libraries:
netx - for running webstart application from jnlp.
uispec4j - for intercepting created webstart window and
manipulating window elements.
You can probably do the same trick with other AWT/Swing testing tool, but uispec4j allows to intercept webstart app executed from jnlp, you don't need to run the app by calling main() and you don't need to have your webstart app source code in your testing code repo.
Credit goes to tporeba
For learning more about Jemmy, you can go through this link
Jemmy Tutorial
Jemmy Samples
NetBeans Platform Test Infrastructure Tutorial

Java applet using JAR?

I currently have a in-development Java Game.
It runs from a .jar, with all the image files inside. The .jar creates and accesses files in the working directory.
I was wondering if there was a simple way to put this on a webpage as a Java Applet. I currently have Applet code in the Game, but all it does is calls the normal main method to create JFrames and run the game.
I need a simple way to run this on clients from a webpage, prefferably an applet? Is there one?
Please note, I didn't actually make this as an Applet at first. It's currently a .jar, with a .bat to run it. My "Applet" class is this simple...
package explorer.applet;
import java.applet.Applet;
import explorer.boot.Startup;
#SuppressWarnings("serial")
public class ExplorerApplet extends Applet{
public void init()
{
Startup.wp = true;
Startup.main(null);
}
}
I was wondering if there was a simple way to put this on a webpage..
Sure. Launch a JFrame direct from a link using Java Web Start.
..as a Java Applet.
Why? Applets are harder to deploy and maintain, and provide a less satisfactory experience to the end user.
Note that the fundamental problem is the same either way. 'How to access an application resource?'
Such resource access would be by URL. There are these 2 primary alternatives:
Add the resource to the Jar and use Class.getResource("/path/to/the.resource")
Put the resource 'loose' on the home server, and form the URL relative to the code base or document base of the applet, or the code base of the JNLP (the file used to configure a JWS launch).
The .jar creates and accesses files in the working directory.
About 4MB, and they store the game information. (It's a 2D world game.)
They also have to be client side, and in the folder that the "jar" runs from.
That is too large for any of the sand-boxed techniques I had in mind, but since it is 'static'1 resources - they can be added to a Jar which is on the run-time class-path and thereby will be 'downloaded and made available to the app.'.
Access the resources as described above.
By 'static' I simply mean 'do not need to be edited or changed by the user or app.', as opposed to something like 'high scores' which must logically be written by the app. on game exit. It is still possible to update the maps, all you need to do is provide the updated Jar and Java will do the rest.

Run a non-applet .jar game on a browser

I am a beginner in java game programming. I have developed a simple java game and obtained a .jar file of it. It is not an applet. I would like to run it on a browser. Is that possible? How can I achieve that?
Assuming your jar's main class simply opens a JFrame to show its contents, you can build a wrapper applet class which simply invokes it, like this:
public class WrapperApplet extends Applet {
public void start() {
new Thread("application main Thread") {
public void run() { runApplication(); }
}.start();
}
private void runApplication() {
my.Application.main(new String[0]);
}
}
If you want it nicer, have the applet show a button and start the main method only after the button is clicked.
If you want to embed a java application in a web page, you need it to be in applet form. It's not that difficult to convert them, see this link for a bit of help.
It's possible with Java Web Start. From the Wikipedia article:
Web Start can also launch unmodified applets that are packaged inside .jar files, by writing the appropriate JNLP file. This file can also pass the applet parameters. Such applets also run in a separate frame. Applet launcher may not support some specific cases like loading class as resource." The same article mentions some of the problems with applets "Web Start has an advantage over applets in that it overcomes many compatibility problems with browsers' Java plugins and different JVM versions.
This SO question explains some of the tradeoffs on Applets v. JWS... In my opinion, if you expect a lot of people on different types of systems to use your application, or if it uses a fair amount of memory (likely with a game), JWS is better.

Is there a way to find out the class of form using eclipse?

I am working on a large undocumented application written in swing/awt. I have to make changes to it and i need to find out what class a form belongs to when i open it. Is there a way to do this via eclipse?
For example: I open the application and to do something with the app such that some frame opens. How do i find out the class of that form? Can this be done through eclipse?
I know i can comb trough the logic but this is a very laborious and largely ineffective process, chunks of the logic are either in jar files or obfuscated.
Thanks.
For figuring out how a given Swing frame is put together, I have found Swing Explorer to be VERY helpful.
https://swingexplorer.dev.java.net/
After installing the Eclipse plugin, note that you need to "Run as ->" to invoke Swing Explorer properly.
I don't know if this is what you need, but maybe you should try searching(MainMenu-->Search) your entire Project for the specific Window title (String) that comes up with this particular window.

Categories