8

I have a java application that was mainly built for the Mac. I need to be able to do either one of three things:

  1. Have my application always follow the current space. Meaning, if I'm on Desktop 2 and start my app, then I switch to Desktop 3, my app will then be visible on Desktop 3 automatically. This is equivalent to right-clicking on the app icon in the dock, select Options, then choose All Desktops. If I can somehow make this the default behavior for my app, without requiring user action, then that would be great!

  2. Capture the screen of a specific space. I currently use the Robot class to take a screen capture. If there is a way I can specify which space to capture, not just the current space, then that would be great!

  3. Be able to at least detect if leaving the current space.

I'm starting to lean towards neither one of these are possible. But if you can give any help, or possible workarounds, that would be awesome!

I have also played around with the GraphicsEnvironment, GraphicsDevice, GraphicsConfiguration, but no luck. It doesn't return any info about Mac's Virtual Desktops (Spaces).

What is also making things difficult is I'm trying to avoid using platform specific code at all costs! I know I can use JNI to accomplish this, or there may be something in the java-apple extension. But currently, I use a single code branch and build for both Mac & Windows. I'm trying to avoid having two separate builds and binaries for both platforms.

Gray
  • 115,027
  • 24
  • 293
  • 354
dream_team
  • 511
  • 5
  • 9
  • You don't need to make separate builds - you just need some platform detection and conditional logic (plus your native code). You are, after all, looking to do something that *is* very platform specific. EDIT: in fact, Drizzt321 already said this in response to Gray's answer. – Greg Kopff May 09 '12 at 00:17

2 Answers2

3

The answer is "no" unless you are writing native code and using JNI. One of the key design features of Java is that it is OS agnostic. What you are asking for is extremely Mac OS X specific. I see nothing in the Apple Java Extensions that helps either.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • 2
    What Gray said, only thing you can do is have a branching code path where you check if you're running on OS X, if so run this JNI code, otherwise do Windows JNI code (or nothing). – Drizzt321 May 08 '12 at 23:54
  • Thanks for the response, I'm pretty sure this is the right answer, just wanted to confirm. I was hoping java would treat spaces like a multi-graphic-device environment, and you can access each graphic device separately. – dream_team May 09 '12 at 16:06
  • I figured that you were just verifying the facts. Yeah, Java user code has little knowledge of the "graphic-device" it is rendering on. The JRE is designed to shield the user from such knowledge. Best of luck. – Gray May 09 '12 at 16:12
2

No, as far as I know, Java doesn't have any special support for Spaces, so you'll need to use platform-specific, native code. However, JNI--with all its ugliness--isn't your only option. You can use JNA instead, which is much more programmer-friendly, and a lot easier to debug.

In your app, you can detect the platform. If it's a platform for which you've written platform-specific code, you can execute that code. If not, you can fall back to a default, pure Java implementation.

rob
  • 6,147
  • 2
  • 37
  • 56