eTechtips

Tech Tips and Insights

Category: Programming

  • Java: Creating a Window

    This is the First of some basic articles about creating a GUI in Java. The following code is the basis of the following series of articles. I am using the Java Swing window toolkit.

    import java.awt.*;
    import java.swing.*;
    
    class FirstWindow 
    {
    
      public static void main(String[] args)
      {
          //Create Window object and set title bar text
          JFrame frame = new frame("First Window");
    
          // Set the Default operation when you close the window
          // This exits the program on closing the window
          // The default behavior is to HIDE_ON_CLOSE
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          // Now we need to Display the window
          // The null argument will place the element in the center of the screen.
          frame.setLocationRelativeTo(null);
          frame.pack();
          frame.setVisible(true);
       }
    }
    

    This is enough to display a simple window, but it will probably not have any useful size associated with it and it does not do anything.

    The next step will be to add some useful widgets and eventually functionality to make the window useful.

    Helpful info:
    Java: Great link to Swing Examples

    0 Comments
  • Testing perl modules

    A quick way to test a perl module with a new method is to use a command like the following:

    $ perl -MeTechTips::IssueTracking::ClearQuest -we '$tr =  eTechtips::IssueTracking::ClearQuest->new();'
    

    This will test the new and ensure that you have the correct syntax if you do not have an easy test environment for your code, for example as I do not have a ClearQuest instance to test with.

    0 Comments
  • Perl find location of a library

    If you are not sure of the location of where a library is being included from in Perl you can use the following command:

    perl -MXML::SAX -le 'print $INC{"XML/SAX.pm"}'
    

    This search is for the XML::SAX library and it displays the location of the SAX.pm file.

    0 Comments
  • Perl: Missing ParserDetails.ini

    I have some scripts that were giving me the following message:
    could not find ParserDetails.ini in /usr/lib/perl5/vendor_perl/5.10/XML/SAX

    The way that I worked around this message was to create the ParserDetails.ini file in the /usr/lib/perl5/vendor_perl/5.10/XML/SAX directory with the following contents.

    [XML::SAX::PurePerl]
    http://xml.org/sax/features/namespaces = 1
    
    2 Comments
  • HTML: How to use an external StyleSheet

    There are two ways to include an external stylesheet.  Using a <link> tag in the <head> section of your web page or using the @import tag in a <style> section.  The @import tag can also be placed inside a CSS file to include other CSS files.

    Example of an externally linked stylesheet named anyname.css

    <head>
    
    <link rel="stylesheet" type="text/css" name="anyname.css" href="url" media="all">
    
    </head>
    

    The name field is the filename and can be set to any path that is available to your Web Server. The media type can be specified in the link tag.

    An example of a @import statement to include a CSS file:

    <style>
    
    @import('anyfile.css');
    
    </style>
    

    You can also specify the type of stylesheet with the @import statement:

    @import('anyfile') screen;
    
    @import('anyfile') print;
    

    The @import statement is not understood by many older browsers, Netscape 4 ignores them completely and Internet Explorer 4 requires you to use parenthesis, even though they are optional.  Luckily, there should be very few people still using these browsers and this is a useful technique to not include features that would not work in those browsers.

    The @import directives must be the first items in your CSS. Even comments should not appear before these statements.

    Internet Explorer versions 4-7 have the limitation that they do not like specifying the type on the @import line.  I have not researched if this is the case with versions 8 and above.

    0 Comments
  • Html: how to use an internal stylesheet

    In order to use an internal stylesheet, place the following in the <head> section of your html document.

    <style type="text/css" media="all">
    
    <!--
    
    Place your css here
    
    -->
    
    </style>

    The <!– –> (xml style comment) is so that older browsers, though very unlikely, will not get confused by the CSS code within the page.

    The media=”all” entry tells the rendering engine which style sheets to use, in this case “all” means to use it for all rendering engines.

    0 Comments
  • Python: AttributeError: ‘getopt’ module has no attribute ‘GetoptError’

    So I was figuring out how to parse command line arguments with python and using the getopt module with the following code

    
    #!/usr/bin/env python
    import sys
    import getopt
    
    def main(argv):
       infile = "some.xml"
       try:
          opts,args = getopt.getopt(argv,"hi:d",["help","infile="]
       except getopt.GetoptError:
          usage()
          sys.exit(2)
    
    if __name__ == "__main__":
        main(sys.argv[1:])
    

    And I was getting the following error:

    AttributeError: ‘getopt’ module has no attribute ‘GetoptError’

    So my first problem was that I had named my script getopt.py and after renaming the script, I got the same error. Well you now need to remove the getopt.pyc file that was generated from your earlier running of the poorly named script.

    4 Comments
  • How to address perl arguments

    If you are using perl, you may find it necessary to take some command line arguments to your scripts.  When that time comes, there is a very handy variable ARGV that contains the passed in arguments.

    To get the total number of arguments passed into the script you will need to use the following:

    $totcommandargs = #$ARGV + 1

    is equal to the number of arguments passed in to the Perl script.

    Note: you need to add one to the count to get the correct number of variables.

    To address the variables you use the following to address the first argument: $ARGV[0]

    will address the first argument

    Since arrays are addressed by n-1, to get the first element you use 0 {zero}, for the second 1, for the third {2}, and so on.

    $0 will give the name of the currently executing script.

    
    #!/usr/bin/perl
    
    $argcount =  $#ARGV +1;
    print "The script $0 has $argcount argumentsn";
    
    for( $i = 0; $i < $argcount;$i++)
    {
    print $ARGV[$i] . "n";
    }
    
    
    0 Comments
  • Passing JVM options to Maven

    If you are using maven to build your projects , then you might have found an out of memory condition in your tests.  Well, setting the MAVEN_OPTS variable with JVM specific options will help with this.

    Example:

    MAVEN_OPTS=-Xmx1024

    Depending on what environment you are in, you will need to set this variable.

    This will give the JVM more 1024 megs to work with and you can customize the number to fit your needs.

    0 Comments
  • Java formatting a number with leading zeroes

    Recently, I had a Java project that needed to encode a set of numbers and I needed a fixed number of digits used for each number. I found the DecimalFormat class a good solution for my needs.

    Here is a snippet of code to show a simple way to format a number with a fixed number of digits.

    import java.text.DecimalFormat;
    
    class TestEncoder
    {
            public static void main(String[] args)
            {
                    // This program expects one integer 
                    // argument and places it in the num1 
                    // variable. This program is not very 
                    // robust as to watch for no arguments
                    // or to check if the argument is not 
                    // an integer.
                    String num1 = args[0];
                    String output;
    
                    // This creates the Decimal Format instance 
                    // and assigns the formatting that we want 
                    // to use, in this case three characters .
                    DecimalFormat dfmt = 
                                 new DecimalFormat("000");
    
                    // This does the work of formatting the number
                    // passed in on the command line to be at least
                    // three digits. 
                    output = dfmt.format(new Integer(num1));
                   
                    // This just displays the results of the 
                    // dfmt.format(new Integer(num1)) command.
                    System.out.println("Output: " + output);
    }
    }
    

    The Decimal format allows for many more formatting options for numbers.

    Make sure to look a the API docs for the version of Java that you are using:
    http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

    0 Comments