Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Command-line tutorial



Overview

With this tutorial, we will learn to:
  • Manage the command-line arguments in a main application
We will use the sizeX and sizeY Command-line arguments to set the width and height of the application window.

Specify the xml file to specify the command-line arguments


First we will specify the xml file which will define our two sizeX and sizeY arguments:
      <arguments>
         <argument key="sizeX" type="int" />
         <argument key="sizeY" type="int" />
      </arguments>

Use the command-line framework in our application

First we define that we use our xml file in our application:
      public class CommandlineTutorialMDI extends AbstractMDIApplication {

        @Override
        public URL getCommandLineConfiguration() {
          return this.getClass().getResource("commandline.xml");
        }     
      }      
We now will use the arguments in the application:
      public class CommandlineTutorialMDI extends AbstractMDIApplication {
        private int sizeX = 200;
        private int sizeY = 200;

        public CommandlineTutorialMDI(String[] args) {
          super("Arguments SimpleExample");

          super.preparePanels();

          this.setCommandLineArguments(args);
             super.applyCommandLineArguments();
        }

        @Override
        public URL getCommandLineConfiguration() {
          return this.getClass().getResource("commandline.xml");
        }

        public static void main(String[] args) {
          CommandlineTutorialMDI mdi = new CommandlineTutorialMDI(args);
          mdi.setVisible(true);
        }    
      }
The last thing we must do is apply the arguments to set the width and height of the application window:
        public CommandlineTutorialMDI(String[] args) {
          super("Arguments SimpleExample");

          super.preparePanels();

          this.setCommandLineArguments(args);
          super.applyCommandLineArguments();
          this.setSize(sizeX, sizeY);
        }      
      
      @Override
      public void handleCommandLineArguments(Map<String, ArgumentGroup> argumentGroups, Map<String, Argument> arguments) {
        if (arguments.containsKey("sizeX")) {
          Argument arg = arguments.get("sizeX");
          sizeX = (Integer) arg.getValue();
        }
        if (arguments.containsKey("sizeY")) {
          Argument arg = arguments.get("sizeY");
          sizeY = (Integer) arg.getValue();
        }
      }      

Usage

We can start our application without argument, such as:
      java -jar CommandlineTutorialMDI.jar
Or we can use the sizeX and sizeY arguments, such as:
      java -jar CommandlineTutorialMDI.jar -sizeX=500 -sizeY=500

See also


Categories: swing | tutorials

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences