How to deploy GUI on an existing complicated program? - java

First of all, sorry for the little broad question. Just want to get some general idea of direction.
Right now I have a big project almost completed. It has over 5 classes and under complicated designs and initially non-GUI, and I want to add a GUI interface to it. the GUI was supposed to fetch information when it runs and displays the running information. I reads about Swings and SwingWorker but it seems not plausible to put entire original program under Background thread. Right now I'm think putting GUI as a separated class but I don't know what's the best way to make connections. Any help is appreciated.

You can't simply "put" a GUI on any existing program, but you can possibly give a program a GUI user interface if the program was well constructed in a very modular fashion such that its UI is well separated from its logic code, for example if it was constructed in an M-V-C (model-view-control) fasion.
As always, the devil is in the details, none of which I can discuss at the moment given the limited information that we possess about your current project and needs. If you need further help, you will need to give us more information and code.

Related

Launching an external .exe and reading in real-time what has been written in Console [duplicate]

This question already has answers here:
Run external program from Java, read output, allow interruption
(5 answers)
Closed 9 years ago.
I have been trying to work on this issue I had by searching thoroughly so as to find out what to do. However, none of the results I've found (at least until now) had suited my requests.
The fact is, I've got an executable JAR I've done. This jar starts an .EXE.
Now, the thing is, the EXE will keep on running the whole time, and I want to get whatever has been written in the console so as to write it on a JTextBox as soon as that is read.
Would you mind giving me an example of that? I would like to do it on my own, but my head doesn't seem to find out how.
Thank you very much.
EDIT: what I'm trying to do is a GUI for a gaming server
EDIT 2: for those saying its duplicate... wish it was... tried what the others explained but didn't work, so that's the reason I asked here..
EDIT 3: as I have been looking forward to find what the problem was, I will tell you that what I've done does not have any errors. However, I guess, it may be caused to the fact that the server (written in C++/C) may not output in a 'normal' way. May that be the reason? I hope so. Otherwise, I might be doing something really wrong.
Please notice I use InputStream in order to be able to read.. but well.
Basically, you need to start by running the process in some kind of background thread so there is no risk that it will block the Event Dispatching Thread.
Then you need to read the processes InputStream. As the input is read, you need to push these updates to the UI in such away so as not to violate the single thread rules of Swing. That is, you should ensure that all updates are made within the context of the Event Dispatching Thread.
Check out Concurrency in Swing for more details.
In this, I would recommend using something like SwingWorker. It allows you to monitor the process from a background thread, but has easy to use functionality to sync the updates back to the EDT.
Take a look at Printing a Java InputStream from a Process for an example

What's the right way to go from one form to another?

I am doing a school java project using the NetBeans IDE. It includes some basic database manipulations. We were taught at school to use the following for linking one form to another:
new <form_name>().setVisible(true)
But this seem to slow down the whole application and there is a small lag for going from one form to another. I heard that using JDialog boxes is a solution to this problem.
So what's the right way to do it?
Better to not swap in and out of different JFrames. How many professional applications such as word processors do you use that do this that throw different windows at the user? Better to use one main JFrame and swap views (usually JPanels) in it via a CardLayout and occasionally show a dependent Window as a dialog when needed, especially when you need to get information in a modal way.
some basic database manipulations. .. But this seem to slow down the whole application
Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling Thread.sleep(n) implement a Swing Timer for repeating tasks or a SwingWorker for long running tasks. See Concurrency in Swing for more details.
(But also see #Hovercraft's advice re. CardLayout..)

How do I turn my Java program into an "app" that can send push notifications?

Disclaimer: I am not a professional developer; I'm just a hobbyist, and a relatively inexperienced one at that, so I apologize for what figure to be some very basic questions. (and yes, I've search the forums)
I've recently been working on a "deal finder" program which is written using a combination of Java and R. The basic steps that I've completed so far are:
Load data on various deals into Java using a particular eCommerce API
Write the data that I need to a series of text files
Load the data from the text files into R
Manipulate the data in R and assign a "score" to each deal
Sort by score to produce a ranked list of deals
Here's where I need help: I'm currently running the program manually by running my Java program in Eclipse and subsequently running the R script. This is obviously inconvenient (and also a bit addictive), so what I'd like to do instead is:
Run the program continuously or at some predefined interval (say every minute)
Send a notification to my iPhone or (if that's too difficult) my desktop whenever
there's a new deal whose score is above a certain threshold.
The problem: I have no idea where to begin with the two tasks above. My coding experience is limited to a bit of Java and math/stat languages like R and MATLAB. I have zero experience with web/mobile development, servers, etc., but I am willing to learn. What I'm hoping to get from this forum is not a completely specified solution, but instead just some general direction. If someone can give me a sense of how this should be done, how much work it would be, what language(s) I would need, etc., that would be immensely helpful.
Two more things I should probably mention: 1) This program is only for my personal use, so the resulting application, whether it be on my phone or desktop, can have very minimal functionality beyond the ability to send/receive notifications. 2) If it makes things easier, I think I can eliminate the dependency on R and write everything in Java.
Any help will be greatly, greatly appreciated.
Two more things I should probably mention: 1) This program is only for my personal use, so the resulting application, whether it be on my phone or desktop, can have very minimal functionality beyond the ability to send/receive notifications.
You may want to use a third-party notification app like Boxcar and its Provider API for this, then.
Depending on your operating system, there are programs that allow you to schedule tasks to run. Cron in Linux or Windows Task Scheduler for instance. You can easily find guides for these online.
Have you considered using email? Rather simple to do from java and wouldn't be platform specific.

How to put a text-interface based on system.out.print on a GUI? - JAVA -

I did a project for university (it is a personal implementation of Zork the Game). As asked I did it with a text interface, using system.out.print. Is there a way to "put the text interface in a GUI" ? I mean for example a simple window with 2 fields, one that displays text output and one for the text input by keyboard.
I downloaded windowsBuilder for eclipse but.. I dont know what to do! :(
Thanks!
Sure there is, just change the output stream for System class. Create a PrintStream that will write out your data to your swing components and then replace it in the System class to use it.
System.setOut(printStream);
If you're doing this for a text game, I'd recommend using a Glk library. Glk is a cross-platform windowing I/O system designed for text games. You may have to write a JNI interface since the libraries tend to be written in C: an existing project called GlkJNI is meant to work the other way around, so a C program can use a Java UI, but it might be helpful anyway.
For an example of how to create a GUI that does what you are asking, take a look at this article: http://www.comweb.nl/java/Console/Console.html
This does not use best practices for building a GUI, but for quick and dirty, it'll get you started. You really should read up on how to properly write a Swing application, though, if this is going to be something you are serious about.

Eclipse - showing full call stack (like when it hits breakpoint in debugger) without putting in breakpoints?

I'm working with a legacy Java app that is new to me so one way to figure out how it works and find things easier, I have thought would be to be able to get the full stack trace after I perform actions, so as to be able to see which classes are being used based on a particular UI action. I had thought this was possible in the debugger but it seems that it only works if I insert a breakpoint and in this case part of the purpose of this is so that I don't have to know what's being called to be able to insert the breakpoint first (as this would help tell me that).
I apologize if this is a basic question, I have searched on this but I'm not finding the correct answer.
This doesn't directly answer your question, but maybe it will solve your problem better. Take a look at BTrace. It lets you instrument a running Java app and insert some basic code of your own. You could, for instance, have it write out entire method call chains to help you find your way through the app. It's somewhat similar to AspectJ, but with an entirely different purpose and requiring no change in the project source:
"BTrace is a safe, dynamic tracing tool for Java. BTrace works by dynamically (bytecode) instrumenting classes of a running Java program. BTrace inserts tracing actions into the classes of a running Java program and hotswaps the traced program classes."
A few suggestions:
Some profilers will allow you to walk from any particular method up (and sometimes down) to see what's calling and being called. I've found this surprising informative about flow, even in apps I thought I knew well.
For understanding the mainline flow, I don't think there's a better substitute for working interactively with a debugger. It will lead you into learning other important things. Not what you wanted to hear, I know. This presumes that you can rapidly restart the app when you miss a key off-ramp.
Reverse-designing large legacy apps is the one place where I use UML fairly regularly. There's too much to keep in my head to form a good big picture. If you have a UML tool that will do reverse-engineering, load it up with the app, then probably prune down hard on the classes you don't care about, because they are trivial or obvious. Arrange the diagrams in a way that helps you understand. I've used Together, Magic Draw, and Visual Paradigm in this way. Together worked the best - but it was a decade ago.
When you are in the debugger perspective, you will see a view showing the launched processes. In that view you can tell it to pause all threads of a process. Once stopped, you will be able to browse through threads to see what they are all doing. To try to catch what a particular action is doing, you would have to start the action and then quickly pause all threads.
You could always run the application with the VM arg of -verbose:class. You could then watch the console output and see what classes the VM is loading when you perform a particular action. This could possibly give you a starting place for where to place breakpoints. This won't always work depending on the scenario, but may be helpful.
Another trick you can use is to figure what classes you know that have to be involved in the code path you are trying to trap. For instance, you mentioned that it's a Java EE web app and therefore the action is likely some kind of a servlet interaction (at some level). I don't have the API in front of me, but you can place a breakpoint on the method in the response object where the output stream is retrieved. Once that breaks, you will know the code that's trying to service the request.
You can always see where a method is called by clicking "Open Call Hierarchy" from eclipse (left click on the selected method or CTRL+ALT+H ). Also, you always can inspect where a method/class is defined by clicking "Open Declaration" (left click on the selected method/class or F3).

Categories