3

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);
  }

}
Community
  • 1
  • 1
CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50
  • 3
    Swing GUI objects should be constructed and manipulated _only_ on the [event dispatch thread](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Mar 22 '12 at 23:52

2 Answers2

4

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]);
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Seems there was alot I was mistaken about. Thanks :) – CaldwellYSR Mar 23 '12 at 17:12
  • Glad to help. Part of the problem is Mac OS specific, addressed in the [other thread](http://apple.stackexchange.com/a/45355/20589), but launching a new JVM works across platforms. See also [`Launcher`](http://stackoverflow.com/a/5696404/230513). – trashgod Mar 23 '12 at 17:19
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?

John3136
  • 28,809
  • 4
  • 51
  • 69