tags:

views:

318

answers:

5

Hello.

I just wanted to know if there is some way to switch between console mode and graphical mode. I am using java on swing.

I'd like to know if I can type something in the shell to go to a console mode, then type something to go back to the desktop. Or if I can press some key at boot time, or something. The idea is to make my server run in console mode, but have desktop available when I want to easier make my tasks.

Thanks

+3  A: 

You can divide the project in two: the server and the GUI. You can run your server as a service (Windows) o daemon (Linux) and when you want the GUI, you have to launch and operate with it.

It´s like applications as MlDonkey, Ettercap, etc.

Onir
A: 

In the program you can define commands to hide the GUI. Something like gui.setVisible(false) should do the trick, it hides the window referenced by gui and all elements of it.

Mnementh
+2  A: 

You can pass parameters on the command line and examine them in main(String[] args). They'll end up in args. So the most simply way is to check for args.length > 0 && "-c".equals (args[0]) to tell the program to run in console mode and to open a UI otherwise.

Another option is to write two main() methods (in different classes) and use the right one.

Aaron Digulla
+2  A: 

You can use java.awt.GraphicsEnvironment.isHeadless() to check whether the environment where your program is running supports GUIs or not:

public static void main(String[] args){
    if (GraphicsEnvironment.isHeadless()){
        // Run console mode
    } else {
        // Start in GUI mode
    }
}

If I were you, though, I'd make this a command-line switch so you can use the console mode in graphic environments, too. For maximum convenience, this would be a non-mandatory option which defaults to some kind of "auto" option, which uses the isHeadless check, like:

public static void main(String[] args){
    final List<String> arguments = Arrays.asList(args);
    final int modeIndex = arguments.indexOf("-mode");
    final String mode = modeIndex == -1 ? "auto" : argument.get(modeIndex);
    if ("auto".equals(mode)) runAuto();
    else if ("console".equals(mode)) runConsole();
    else if ("gui".equals(mode)) runGui();
    else System.err.println("Bad mode: " + mode);
}

private static void runGui(){ ... }
private static void runConsole(){ ... }
private static void runAuto(){
    if (GraphicsEnvironment.isHeadless()) runConsole();
    else runGui();
}

(TODO: Add error handling, remove magic string literals, etc.)

So, start your program with java YourMainClass or java YourMainClass -mode auto and it makes an educated guess whether you want GUI or console, use java YourMainClass -mode console to force console mode, or java YourMainClass -mode gui to force GUI mode.

gustafc
A: 

You could create a command line command that starts a GUI wizard that enables you to easily create the components/ configuration you want, and then enable the GUI to pass the complicated instruction back into your single application thread. Whether it is useful to have the GUI component destroyed after you have used it, or just hidden, is a decision you will have to make, regarding how often you will be using the GUI component.

hasalottajava