I'm developing a Swing application and I need to flash the Windows taskbar. I can't use frame.requestFocus() because I don't want to steal focus from any other application.
I don't know if it applies to newer versions of Windows, but the .toFront() method used to flash the window if none of the current VM's windows were in the foreground.
This means that calling frame.toFront() on a minimized frame would always make it flash...
JNIWrapper with its winpack extension can do what you want.
The Demo on the site shows it in action.
You can either force-minimize your GUI and .toFront-en it:
Gui.frame.setState(Frame.ICONIFIED);
for (int i = 0; i < 3; i++) {
Thread.sleep(10);
Gui.frame.toFront();
Thread.sleep(10);
Gui.frame.setVisible(false);
Thread.sleep(10);
Gui.frame.toBack();
Thread.sleep(10);
Gui.frame.setVisible(true);
}
// be creative!!
which sadly will remove the focus from the active window. You could find out the active window and reactivate it afterwards. But still, the flashing will only last for about three seconds.
...or get down to the really root of the matter by using a DLL-call on FlashWindow. Calling dlls is not possible with clean Java code, you'll need the help of other programming languages, possible e.g. with JNA. Other than that, you could also write your own program in another language and call it from your Java application. I'll give an example in AutoHotkey below:
AutoHotkey Code:
if 0 != 1 ; note: in ahk, 1 corresponds args[1] and 0 corresponds args.length
{
msgbox, There needs to be ONE parameter passed over to this file, which is the name of the window that should be flashed.
ExitApp
}
programName = %1%
winget, hWnd, ID, %programName%
DllCall("FlashWindow",UInt,hWnd,Int,True)
compiled into a file called flash.exe, put into your Java working directory, you can call it from any function:
Runtime.getRuntime().exec("./flash.exe \"" + MyJFrame.getTitle() + "\"");
Alternatively, one could use the AutoHotkey.dll and access it within the Javacode (there are guides on how to do it on the internet), so there'd be no need for any external exe files.
If you still have problems achieving the flashing in the windows taskbar, please let me know!
The best way to do this:
if (!isFocused()) {
setVisible(false);
setVisible(true);
}
Using Swing per se, you very probably can't; that's a Widnows specific thing.
Related
I'm trying to code a program in Java that automatically types each character in a string using the Robot class. I've used it to make a similar automated program a while ago (which I'll refer to as the 'previous project') that used 'keyPress()'. This and most, if not all, of the other methods for the Robot class which I needed to use worked perfectly.
Now I've started and 'keyPress()' isn't typing anything, regardless of the KeyEvent I pass as an argument. I thought I had written the code incorrectly, so I ran my previous project just to make sure it worked, but it didn't.
Here's the snippet of code I used for the previous project and my current one (which you can also use to test this).
EDIT: Try the on a text editor or anything that functions like a text field. I've now shown the entire demo class.
// demo procedure
public class DemoClass() {
public static void main(String[] args) {
new DemoClass().run()
}
public void run() {
try {
Robot robot = new Robot();
for (int i = 1; i <= 30; i++) {
robot.keyPress(KeyEvent.VK_A);
robot.delay(100);
System.out.println("Typed key");
}
} catch (AWTException e) {
e.printStackTrace();
}
}
}
'Typed key' gets printed 30 times and no errors appear in the output either.
I've tried;
adding 'robot.keyRelease(KeyEvent.VK_A)' just after the key press,
allowing the program type in the software I want to automate,
allowing the program to type in the IDE I'm using and in a text editor,
allowing the program to click in the IDE and text editor (so I could see if only pressing keys was the issue).
None of these have produced results. I've checked the code from multiple sources (articles and videos), all of which have the same code stub. I've read that some software prevent Robot objects from typing/clicking in them, but both typing and clicking worked when I was developing my previous project.
So I woke up today and... the program works.
I still don't know why it wasn't working in the first place or how the problem fixed itself (magic?). Running the sudo command didn't seem to make a difference, and none of the software I'm dealing with prevent a Robot object from automated typing/clicking.
It's most likely just a me-problem since other people managed to get it working. Since it's happened once it'll probably happen again.
Anyways, thanks to everyone that helped! If future readers have any ideas of why it didn't work then feel free to share them.
String cmd = "start calc.exe";
Process process = Runtime.getRuntime().exec(codeString);
I can call calculator out, but I wish to specify a accurate position like (200,300).
how can I rewrite my cmd String?
I know that java.awt.window can set a window or frame to the specific position.
Is there any method I can use to fill frame or window with my process?
There is no clean pure java solution because JDK does not provide API that can control non-java windows. So, if you want to can use JNI/JNA.
But I can suggest you a patch that will typically work.
Windows OS allows moving windows using keyboard. Try the following manually:
Win+R
type calc and press enter
press alt+space
press M
press enter
now use arrows to move the window. Press ESC to exit this mode.
All these actions can be implemented using java.awt.Robot.
So, you can run calculator and then immediately move its window where you want.
Well, this is not clear solution, but very simple one.
Expected Problems:
Alt+space is mapped to other, custom application
Other window that started together with calc overlaps it.
User will see that window is created somewhere and then quickly moved.
So, everything depends on how important all this for you. This solution is good as an exercise or demo but bad for real commercial application.
I work with autoitx4java in my java project.
That works fine until the window title I work with exists multiple times.
Imagine I have three windows with the title "MyAppTitle" open:
AutoItX x = new AutoItX();
int posX = x.winGetPosX("MyAppTitle");
Obviously a single value is returned. And it's the x-pos of the window "MyAppTitle" that I focused last.
It's no problem to get the handles of the windows. But most of the methods autoitx4java provides require the window title as parameter.
How can I access the windows with their handle? Or is there a better library I could use?
UPDATE: From the official AutoIt website:
When you have a handle you may use it in place of the title parameter
in any of the function calls that use the title/text convention.
But it doesn't work within the autoitx4java library.
I am working on a program, which allows the user to lock the computer, so no one else can use it. Is there anyway, I can disable the mouse, and specific keys on the keyboard? Thanks.
No but you can create have your program locking the screen and then using a MouseMovementListener so each time the mouse moves, you return it programatically to some point in the screen ( it would look like it doesn't move anymore )
over Linux you can use xinput enable id
you can get the id throw xinput without parameters.
Process p;
p = Runtime.getRuntime().exec("xinput disable 12");
I don't know of a portable way and I'm pretty sure Java does not supply anything like that in fact. However on Windows this can be done with BlockInput. But if you were going to code JNI/JNA you might as well use LockWorkStation.
I think you could do it if you are implementing the MouseListener interface. In the MouseClicked method you could check a boolean before actually performing any action. When you want to disable or enable the action change the state of that boolean.
If you want to lock the screen then you can use java Robot class. And if you want to block the keyboard and mouse events when Windows is locked then there is no need. Since locking the screen results in disabling all the inputs.
I have a gwt suggest box that does an RPC call to get some data from the server and display it. In some cases there are up to 2000 results. Whilst this works fine in chrome when the javascript runs in firefox it freezes the window for 5 seconds and sometime brings up script not responding warnings.
What I wanted to do was something like show 20 results and have a more button that can just append the next 20 without having to call back to the server every time it is clicked. I am fairly new to this, I have tried extending suggestBox and overriding showSuggestions() but it is protected so I can't.
Any suggestions/ ideas would be great.
Cheers,
Rob
See this question for pointers on how to extend the GWT's SuggestBox - basically, you want to provide your own SuggestOracle (it's used for fetching the suggestions), maybe your textbox (see the links in the question I mentioned earlier) and most likely a custom SuggestBox.SuggestionDisplay. Those three are passed via the constructor to SuggestBox. See the existing default implementations (MultiWordSuggestOracle, SuggestBox.DefaultSuggestionDisplay) for some ideas :)
If you want to change source code of SuggestBox see this
you should create com.google.gwt.user.client.ui packeges in your src root and copy there SuggestBox.java. When you use SuggestBox it calls your SuggestBox which is in your src.
Check this also it can be useful too
First solution come to mind is that write your own widget which extends from SuggestBox and the second solution maybe change the default css parameters of suggestbox
.gwt-SuggestBox {
}
.gwt-SuggestBoxPopup {
}
.gwt-SuggestBoxPopup .item {
}
.gwt-SuggestBoxPopup .item-selected {
}
.gwt-SuggestBoxPopup .suggestPopupContent {
}