简体   繁体   中英

Find window by title and bring in the foreground in Java

I'm currently working on a project for my studies. I'm trying to solve a simple puzzle game by using breadth first search. After I have found the optimal solution I want to simulate a series of keypresses (using awt.Robot) to solve the game.

Is there a way to bring a window with known title to the foreground. I'm using Linux and it would be okay to simply use some CLI tools since this is not the main focus of my project.

I found a tool called wmctrl which does excatly this. I simply start it using ProcessBuilder.

new ProcessBuilder("wmctrl",  "-a", title).start()

To answer part of your question, see xprop or xwininfo to find the window.

For the second part, while Xlib provides a function to do this ( XRaiseWindow() ), I don't know how to do from the command line or inside Java. See this thread though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM