Is there any option which i can use to Control Window State of another App. from a Java Application .
Like if i need to minimize or maximize another app. window on Ubuntu Enviroment (if it's matter's im using Gnome Window Manager).
Bests
Two bad options:
1) use java.awt.Robot to click the appropriate coordinates where the window is. (You have to hard code these)
2) use jni
Related
I am looking for a way to mimic operating-system (Windows in specific) actions through Java. Preferably, the program should run in the background, but it is not a big deal if it does not. I got the background part covered thanks to this question. I was looking for the following specific features :
Maximizing/Minimizing the currently active window. (Can be any window, not just the Java application window.)
Closing the currently active window.
Open installed programs, and system utilities like the calculator, paint, etc. (I figured out this one from this question.)
Shutdown/Restart (This one's done too, thanks to the question here.)
So, my actual question is:
Is it possible to minimize/maximize or close an application window from a java program? (in Windows)
Example Scenario:
Firstly the java program is started, and it runs either as a background process or as a window. Bottom-line is that it should be able to accept triggers like maybe a keyboard shortcut or microphone input to trigger the action. After that suppose a Chrome window is opened and is currently active. Now on pressing the pre-defined shortcut, the Chrome window will minimize/maximize or close.
If the answer to the question is yes, I could use some pointers to start with my application. Thanks!
What you need is like an OS shell programming interface.
In Java side you will define a few interfaces.
Another Java layer will detect which OS is used and will return an implementation of interface: Windows, Linux, Macosx.
Some functionality you can have with simple bash command: in windows cmd, in linux .. to many. Eg shut down, launch MSPaint, Calculator.
Other functionality you can have it with windows API: you will need to write some JNI functions and call it. eg minimize, maximize. It is possible.
Edit:
I see there is no accepted answer, although it is answered properly.
Here is a C# code which does what you need in Java.
Now you need to migrate this code to Java:
In your java class declare a function:
private native maximizeOrMinimizeWindowWithName(String windowName, boolean maximize);
Compile -it
use Javah.exe - it will generate the necesary .h files
Use a C editor, configure environment, use the generated .h file.
-include windows api headers
-load user32.dll
- do more stuf..
compile your C code to .dll
put the your.dll into your app PATH environment variable. ( windows has the . in path, linux not)
-text, bugfix,
for more info you should see a basic JNI tutorials.
-upvote accept :)
This can be initiated from Java, but not actually implemented in Java. In other words, it will take a lot of platform-specfiic JNI library code to get it working.
Java will give you almost no benefit for your use case; you should avoid it altogether for this project.
You should look into Autohotkey. It's an system dedicated to simulate user programmaticly.
Using AH scripts you can easily access all open windows, installed programs and even control mouse and keyboard.
I have a java Robot program ,where it can type a word on notepad,word etc,.
But the problem is i am not getting focus of window application, when i tried to enter the cursor in its textbox manually by using ALT-TAB,
And the application is not available in the list ,while doing ALT-TAB ,
also it is not visible in Task Manager-> Application tab
but, it is available with Processes tab of Task Manager.
Is there any solution to get focus of that windows application for keyPress using Java Robot.
Also ,in some other PC's ,i am getting Clear Focus of the application when i put the cursor manually after running the java robot program. I have installed JDK1.6 in all my PC's. It is not working only in my PC's.
Thanks in advance!!
I think that your best bet is to use JNA to allow you to make system calls.
Assuming a Windows program, you could use JNA to make calls to the user32.dll including dll functions such as FindWindowEx(...) and SetForegroundWindow(...) to set the window of your choice to the foreground.
Instead of getting the focus of the other app, you could put your robot to the background, returning whatever was in the foreground previously (ie your target app) to get the focus.
I am creating a program using Java Sockets in which I capture the client desktop and send messaging to client. Its working properly but now I want to block Client applications like Notepad, MS-Word, etc.
How can I do this?
Thanks.
It is hard to do using pure java API.
I do not know what do you mean when you say "block". The easiest way is to check from time to time running processes and kill one named "notepad" by executing taskkill from java.
If you wish to achieve effect of inactivity of application, i.e. user sees the notepad but cannot type you can do the following.
You have to check which application is on front. There is no clean pure java solution for this but you can probably write VBScript or JScript that does this task and run it from java. Once you detected that notepad is on top create transparent window (or even probably half-transparent window) that occupies full screen. Bring it on top. User will not be able to type into notepad because your window is on top but will see it.
Here is reference how to create transparent windows: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
Good luck.
I am working on a Java application which has to launch a different application. If I launch the second application using Runtime.getRuntime().exec(), it becomes the active process and its window comes before my application's window. What I really want to do is launch the process in "hidden" mode so that its taskbar entry does not appear and its window is initially invisible or behind my application window. Then my application would make it visible or move it to the front when it is good and ready. Is this possible or am I asking for too much?
This is for a demo. So I am not worried about security issues.
Edit: Daniel's answer has given me an idea. What if I use Powershell to invoke the application instead of CMD.EXE? Will that let me start the app without the window and then bring the window back? I will be using to launch java to launch PowerShell to launch app, but what the heck!
You don't say what this other application is, but I'm assuming that it's one that you have no control over (i.e. you can't give it a parameter option to start up in a minimized mode or similar.) Rather than hide the application you're launching, can you just use the toFront() method on your window after the other application has launched to bring your window in front of the other? And then minimize your window when you want to reveal the other one?
I'm the first to admit it's a bit of a bodged solution, but it might work depending on what you're after.
You cannot provide these parameters, BUT you can use the "start" command (try it in cmd), which supports these parameters. Eventually you have to call it with a cmd.exe shell, but this will work!
I have a java application that will run on Windows 7 (using Swing, App #1) that runs as full screen (but not in exclusive mode). I have another application (App #2) that displays a GUI to configure an external device over a serial port that I do not have the source to and cannot change at all.
I want to embed App #2 inside of App #1 so that it looks like it is part of the parent java application (hiding the file --> exit button and hiding the minimize, maximize, and close buttons).
If this kind of integration is not possible inside the Java application, I would be fine with opening the process using Java and just monitoring it to keep it open . It would need to keep the window set to "always on top" because App #1 is fullscreen and hide as much of the external MS Windows UI as possible to trick the user into thinking it isn't an external application. Is there some kind of method whether using JNI or something else to manage another processes window (screen location, title bar, minimize, maximize, close button visibility) and process state from inside my Java application?
I will be happy to provide more information if needed.
The following scheme is language independent, I've managed to embed IE window into a Ruby application this way:
First of all, change the style of the external application window (you can use JNA for calling WinAPI):
style = GetWindowLongPtr(APP_HWND, GWL_STYLE);
style |= WS_CHILD;
style &= ~WS_CAPTION;
style &= ~WS_POPUP;
SetWindowLongPtr(HWND, GWL_STYLE, style);
Define parent-child relationship between windows:
SetParent(APP_HWND, JAVA_HWND);
Listen to Move/Resize events of your Java window, and apply new positions on a child window.