I'm having some trouble with getting input from the command line before opening a GUI window. I asked this question previously on Apple Exchange but was sent here after we determined it to be a Programming problem. Basically I'm running a Scanner to get user input before I open up a window but it starts the program, switching spaces on my Mac, and then I have to switch back to the workspace with the terminal in it to answer the question. Here's a link to the original question.
https://apple.stackexchange.com/questions/45058/lion-fullscreen-desktop-switching-quirk/45065#comment51527_45065
Here's the code I've tested with...
public class Client extends JFrame {
public static void main(String[]args) {
Scanner in = new Scanner(System.in);
System.out.printf("\nGive me a size for the screen: ");
String response = in.nextLine();
new Client(response);
}
public Client(String title) {
super(title);
super.setVisible(true);
}
}
Use invokeLater()
to start the GUI after you get the input.
final String response = in.nextLine();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Client(response);
}
});
Note that your example runs fine on my platform due to timing differences. Also consider using the args
array to pass parameters, or ask the implementation, as shown in FullScreenTest
Addendum: Reading your other thread a little closer, you can use the following approach that launches a NamedFrame
in a separate JVM.
package cli;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
/** @see https://stackoverflow.com/q/9832252/230513 */
public class CommandLineClient {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Give me a name for the screen: ");
final String response = in.nextLine();
try {
ProcessBuilder pb = new ProcessBuilder(
"java", "-cp", "build/classes", "cli.NamedFrame", response);
Process proc = pb.start();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
}
class NamedFrame extends JFrame {
public NamedFrame(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setVisible(true);
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new NamedFrame(args[0]);
}
});
}
}
Code appears to be ok. Is there any class level stuff in Client that you haven't shown here (e.g. static members etc?)
The whole switching workspaces description in your link is an OS level thing NOT java specifically.
Is there options to the java command or something on mac you could use?