views:

161

answers:

2

From within Java, I am opening an Excel file with the default file handler (MS Excel, in this case :-) ) using the method described in this stackoverflow question:

Desktop dt = Desktop.getDesktop();
dt.open(new File(filename));

However, the Excel program doesn't get the focus. Is there any easy way to do so?

Edit: There is a related stackoverflow question for C#, but I didn't find any similar Java method.

Edit 2: I've did some simple tests, and discovered that Excel starts and gets the focus whenever no instance of Excel is running. When Excel is already open en NOT minimized, the application doesn't get the focus. If instead the Excel Windows was minimized, the above code will trigger a maximization of the window and Excel getting the focus (or vice versa :-) ).

A: 

From a scala-program, which runs in the JVM too, I can open an application, and that get's the focus by default. (Tested with xUbuntu, which is a kind of Linux).

import java.awt.Desktop                
val dt = Desktop.getDesktop ();        
dt.open (new java.io.File ("euler166.svg"));

I can't say, whether this is specific for Linux, or maybe something else - however starting Inkscape in my example, excel in yours, may take a few seconds, while the user impatiently clicks in the javaprogram again, thereby claiming the cursor back. Did you check for that?

You could then change to the last application, at least on Linux and Windows with ALT-Tab aka Meta-Tab (again shown in scala code, which you can easily transform to javacode, I'm sure):

import java.awt.Robot
import java.awt.event._ 

val rob = new Robot ()
rob.keyPress (KeyEvent.VK_META)
rob.keyPress (KeyEvent.VK_TAB)
rob.keyRelease (KeyEvent.VK_TAB)
rob.keyRelease (KeyEvent.VK_META)

but unfortunately the unknown source off more trouble, also known as user, might do nothing, so switching would be the false thing to do. Maybe with a thread, which checks for a certain amount of time, whether the java-program has the focus, but it keeps a form of roulette, in an interactional environment, because the user may have a fast or slow machine, or change to a third application meanwhile, and so on. Maybe a hint before triggering the new app is the best you can do?

user unknown
Thanks for the answer. I am the sole user (currently), so I can confirm that it is not a problem of too many clicks. Indeed I've noticed that sometimes Excel gets the focus, but most of the times it doesn't. BTW Excel is usually open already, which might be the source oft he problem...
Rabarberski
+1  A: 

If you only care about Windows (implied in the question), you can change the way you invoke Excel: use "cmd start...".

I have been using this piece of code to launch Windows applications for some time now. Works every time. It relies on the file association in Windows to find the application. The launched application becomes the focused window on the desktop.

In your case, Excel should be associated with .xls, .csv and other typical extensions. If it is, Windows will launch Excel, passing your file to it.

Usage:

MyUtilClass.startApplication( "c:\\mydir\\myfile.csv", "my window title" );

file is the full path to the input file for Excel and title is the window title (the application may or may not take it - Excel changes the window title).

public static void startApplication( String file, String title )
{
  try
  {
     Runtime.getRuntime().exec( new String[] { "cmd", "/c", "start", title, file } );
  }
  catch( Exception e )
  {
     System.out.println( e.getMessage() );
  }
}
chr