Preparing for Emigration (1)


Introduction

CDO Office
CDO Office

Yes, it’s coming closer now. Next Sunday I will be airborne and on my way to Cagian de Oro. Why this place? Of all places in the world I chose this place for the following reasons:

  1. My future wife lives there. Actually we met there during my holyday last June.
  2. Nice climate. I don’t like Dutch winters, the shortening of days makes me depressive. There is one known cure: Light therapy but as entrepreneur I cannot afford lying 10 hours per week on a sun bank.
  3. Talented and creative people. When I just look at my fiancé I know enough. It’s truly amazing what she can do with a Samsung Phone. I want to see what she is able to do with Adobe Premiere or Apples Final Cut Pro.
  4. Office in the Open air. Sounds crazy? Not for me. My office has a roof and a bar. Actually it’s a Hotel. Many foreign entrepreneurs have.
  5. Costs of living – Lots cheaper than here! Not just because things cost less. We also don’t have to pay taxes in the Philippines. For the price of a single room here (including income taxes) I can rent 10 rooms over there! It’s a nice place to work in.

Why would you want Java FX?



Introduction

The reasons why you should switch to JavaFX are many. Here are just a few:

  • The GUI has a much more fresh and modern look compared to Swing or AWT. (To me it looks more to MacOS Aqua).
  • Lightweight and hardware accelerated.
  • Easier and faster to develop thanks to Oracle Theme Builder.
  • CSS Support (Desktop and Web).
  • Ready to use GUI Widgets (like the calendar).
  • Accurate and fast positioning of Desktop (and web) widgets.
  • 3D support.
  • Supports a native XML format named FXML. It’s somewhat similar to HTML, Which makes it easy for we developers to work with.

References

 

JavaFX


Introduction

JavaFX is a software platform for creating and delivering desktop applications, as well as rich Internet applications (RIAs) that can run across a wide variety of devices. JavaFX is intended to replace Swing as the standard GUI library for Java SE, but both will be included for the foreseeable future. JavaFX has support for desktop computers and web browsers on Microsoft Windows, Linux, and macOS.

JavaFX
JavaFX

Similar to Eclipse SWT, It’s written for the Desktop and replaces Swing and AWT. Next to the Desktop, JavaFX can also be used to be used with Webbrows.

The code below shows how a Window named ‘HelloWorld’ with a button ‘HelloWorld’ named  is created. When the button is clicked, the console prints Hello World.

package javafxtuts;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFxTuts extends Application {
    public JavaFxTuts()
    {
        //Optional constructor
    }
    @Override
    public void init()
    {
         //By default this does nothing, but it
         //can carry out code to set up your
         //app.
         //It runs once before the start
         //method and after the constructor.
    }
    
    @Override
    public void start(Stage primaryStage) {
        // Create the Java button
        final Button button = new Button();
        // Setting text to button
        button.setText("Hello World");
        // Registering a handler for button
        button.setOnAction((ActionEvent event) -> {
            // Printing Hello World! to the
            // console
            System.out.println("Hello World!");
        });
        // Please note that this Synax is a
        // Lambda Expression 
        // Initializing the StackPane class
        final StackPane root = new StackPane();
        // Adding all the nodes to the StackPane
        root.getChildren().add(button);
        // Creating a scene object
        final Scene scene = new Scene(root, 300,
         250);
        // Adding the title to the window
        // (primaryStage)
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        // Show the window(primaryStage)
        primaryStage.show();
    }
    @Override
    public void stop()
    {
        //By default this does nothing
        //It runs if the user clicks the 
        //go-away button
        //closing the window or if 
        //Platform.exit() is called.
        //Use Platform.exit() instead of
        //System.exit(0).
        //This is where you should offer to save
        //any unsavedstuff that the user 
        //may have generated.
    }
 
    /**
     * Main function that opens the "Hello
     * World!" window
     * Note that Main starts the execution of the
     * Application, just as in any regular 
     * Java Application.
     * @param args the command line arguments
     */        public static void main(final String[] 
        arguments) {
    // The actual start takes place with the
    //  launch(arguments);
    //  launch(arguments); Command:
        launch(arguments);
    }
}
References and Tutorials

There exist many on the web:

Getting the http Request Parameters in a Servlet

Introduction

The implicit HttpServlet interface comes with two methods which must be implemented.  These are doGet(HttpServletRequest req, HttpServletResponse res) and doPost(HttpServletRequest req, HttpServletResponse res). The example below shows how to get both.

// See also The HttpServlet

// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

// This method is called by the servlet container to process a POST request.
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

// This method handles both GET and POST requests.
private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Get the value of a request parameter; the name is case-sensitive
    String name = "param";
    String value = req.getParameter(name);
    if (value == null) {
        // The request parameter 'param' was not present in the query string
        // e.g. http://hostname.com?a=b
    } else if ("".equals(value)) {
        // The request parameter 'param' was
       // present in the query string but has no
       // value
       // e.g. http://hostname.com?param=&a=b
    }

    // The following generates a page showing all
    // the request parameters
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/plain");

    // Get the values of all request parameters
    Enumeration enum = req.getParameterNames();
    for (; enum.hasMoreElements(); ) {
        // Get the name of the request parameter
        name = (String)enum.nextElement();
        out.println(name);

        // Get the value of the request parameter
        value = req.getParameter(name);

        // If the request parameter can appear
        // more than once in the query string,
        // get all values
        String[] values =
        req.getParameterValues(name);

        for (int i=0; i<values.length; i++) {
            out.println("    "+values[i]);
        }
    }
    out.close();
}

 

Getting the Manifest of a Jar file.

Introduction

JAR files support a wide range of functionality, including electronic signing, version control, package sealing, and others. What gives a JAR file this versatility? The answer is the JAR file’s manifest.

The manifest is a special file that can contain information about the files packaged in a JAR file. By tailoring this “meta” information that the manifest contains, you enable the JAR file to serve a variety of purposes.

This example  will explain the contents of the manifest file and show you how to work with it, with examples for the basic features.

For more information about the MANIFEST See https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

try {
    // Open the JAR file
    JarFile jarfile = new JarFile("filename.jar");

    // Get the manifest
    Manifest manifest = jarfile.getManifest();

    // Get the manifest entries
    Map map = manifest.getEntries();

    // Enumerate each entry
    for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
        // Get entry name
        String entryName = (String)it.next();

        // Get all attributes for the entry
        Attributes attrs = (Attributes)map.get(entryName);

        // Enumerate each attribute
        for (Iterator it2=attrs.keySet().iterator(); it2.hasNext(); ) {
            // Get attribute name
            Attributes.Name attrName = (Attributes.Name)it2.next();

            // Get attribute value
            String attrValue = attrs.getValue(attrName);
        }
    }
} catch (IOException e) {
}

 

Creating a Temp File

Introduction

Every Operating System has it’s own location for storing temporary files. In UNIX/Linux this is /tmp. In Windows it’s C:\temp.

The example below shows how it can be done in the (platform independent way with Java:

try {
    // Create temp file.
    File temp = File.createTempFile("pattern", ".suffix");

    // Delete temp file when program exits.
    temp.deleteOnExit();

    // Write to temp file
    BufferedWriter out = new BufferedWriter(new FileWriter(temp));
    out.write("aString");
    out.close();
} catch (IOException e) {
}

 

Creating a password field in Java



Introduction

JPasswordField is the Swing Password in Java. Just like the other javax.swing Components it’s a JavaBean. Here’s how to implement it:

JPasswordField textfield = new JPasswordField("Initial Text");
textfield.setEchoChar('#');
textfield.addActionListener(actionListener);

 

HiLighting words in a JTextPane

Introduction

This example shows how text can be HiLighted in the text of a JTextPane.

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

// Makes text red
Style style = textPane.addStyle("Red", null);
StyleConstants.setForeground(style, Color.red);

// Inherits from "Red"; makes text red and underlined
style = textPane.addStyle("Red Underline", style);
StyleConstants.setUnderline(style, true);

// Makes text 24pts
style = textPane.addStyle("24pts", null);
StyleConstants.setFontSize(style, 24);

// Makes text 12pts
style = textPane.addStyle("12pts", null);
StyleConstants.setFontSize(style, 12);

// Makes text italicized
style = textPane.addStyle("Italic", null);
StyleConstants.setItalic(style, true);

// A style can have multiple attributes; this one makes text bold and italic
style = textPane.addStyle("Bold Italic", null);
StyleConstants.setBold(style, true);
StyleConstants.setItalic(style, true);

// Set text in the range [5, 7) red
doc.setCharacterAttributes(5, 2, textPane.getStyle("Red"), true);

// Italicize the entire paragraph containing the position 12
doc.setParagraphAttributes(12, 1, textPane.getStyle("Italic"), true);

 

Getting the Request header



Introduction

The Request Header contains all kinds of useful data, like the requesters method (GET or POST) but also the requester’s browser an OS. This example shows how to get it;

The request method is <%= request.getMethod() %>
The request URI is <%= request.getRequestURI() %>
The request protocol is <%= request.getProtocol() %>
The browser is <%= request.getHeader("user-agent") %>