Thursday, March 27, 2014

What's New for JavaFX in Java SE 8

Ported to new embedded platforms, JavaFX 8 contains many new and exciting features including enhancements to WebView's HTML5 support , 3D, embedding Swing nodes inside a JavaFX Scene Graph, new UI controls, a new visual theme and much more.

Tuesday, March 18, 2014

Java SE 8 released

Java SE 8 contains several new features and enhancements that increase the performance of existing applications, make it easier to develop applications for modern platforms, and increase maintainability of code.

Download Java Platform (JDK) 8 or JDK 8 & NetBeans 8.0 HERE.

Thursday, March 13, 2014

Implement event handler using Lambda Expression

The following code replace traditional EventHandler with Lambda Expression in GUI application, JavaFX:
package javafx8_hello;

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;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX8_Hello extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        
        /* Normal 
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        */
        
        /*
        In Lambda Expression
        */
        btn.setOnAction((ActionEvent event) -> {
            System.out.println("Hello World!");
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}

Monday, March 10, 2014

Java 8 Launch Webcast, on March 25th 2014

To celebrate the launch of Java SE 8, Oracle will host a live webcast on March 25th 2014, 10am PST. This event will feature:
  • Welcome from Mark Reinhold, Chief Architect, Java Platform Group.
  • A panel discussion look at Java SE 8 new features and enhancements by members of Java Engineering
  • Short comments from companies using Java and community members
  • A panel discussion on Java 8 and the Internet of Things (IoT)
  • A LIVE chat to answer your questions
    - You can post questions now
    - Use #Java8QA during the event
  • Over 25 videos introducing the features of Java 8 will be available
Details and Register HERE.










Sunday, March 9, 2014

Get second of Day and nano decond of day with LocalTime

java.time.LocalTime is a time without time-zone in the ISO-8601 calendar system, such as 10:15:30.

LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision. For example, the value "13:45.30.123456789" can be stored in a LocalTime.

The method toSecondOfDay() and toNanoOfDay() extracts the time as seconds of day/nanos of day.

Example:
package java8localtime;

import java.time.LocalTime;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class Java8LocalTime {

    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        System.out.println("now.toString(): " + now.toString());
        System.out.println("now.toSecondOfDay(): " + now.toSecondOfDay());
        System.out.println("now.toNanoOfDay(): " + now.toNanoOfDay());
    }
}

Get second of Day and nano decond of day with LocalTime
Get second of Day and nano decond of day with LocalTime

Java SE 8 for the Really Impatient

Eagerly anticipated by millions of programmers, Java SE 8 is the most important Java update in many years. The addition of lambda expressions (closures) and streams represents the biggest change to Java programming since the introduction of generics and annotations.

Now, with Java SE 8 for the Really Impatient , internationally renowned Java author Cay S. Horstmann concisely introduces Java 8’s most valuable new features (plus a few Java 7 innovations that haven’t gotten the attention they deserve). If you’re an experienced Java programmer, Horstmann’s practical insights and sample code will help you quickly take advantage of these and other Java language and platform improvements. This indispensable guide includes
  • Coverage of using lambda expressions (closures) to write computation “snippets” that can be passed to utility functions
  • The brand-new streams API that makes Java collections far more flexible and efficient
  • Major updates to concurrent programming that make use of lambda expressions (filter/map/reduce) and that provide dramatic performance improvements for shared counters and hash tables
  • A full chapter with advice on how you can put lambda expressions to work in your own programs
  • Coverage of the long-awaited introduction of a well-designed date/time/calendar library (JSR 310)
  • A concise introduction to JavaFX, which is positioned to replace Swing GUIs, and to the Nashorn Javascript engine
  • A thorough discussion of many small library changes that make Java programming more productive and enjoyable
This is the first title to cover all of these highly anticipated improvements and is invaluable for anyone who wants to write tomorrow’s most robust, efficient, and secure Java code. 

Java SE 8 for the Really Impatient

Monday, March 3, 2014

JavaFX + java-simple-serial-connector + Arduino



A simple Java application using java-simple-serial-connector library (jSSC) , with JavaFX user interface, send bytes to Arduino Esplora via USB.

http://arduino-er.blogspot.com/2014/03/javafx-java-simple-serial-connector.html

Example of using AudioSystem to play wav file

The javax.sound.sampled.AudioSystem class acts as the entry point to the sampled-audio system resources.

Example to play wav file using AudioSystem:
package javafx_audio;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
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;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Audio extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Play 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
                playSound();
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void playSound(){
        AudioInputStream audio = null;
        try {
            String clipPath = "/home/eric/tmp/HelloWorld.wav";
            audio = AudioSystem.getAudioInputStream(new File(clipPath));
            Clip clip = AudioSystem.getClip();
            clip.open(audio);
            clip.start();
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(JavaFX_Audio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JavaFX_Audio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LineUnavailableException ex) {
                Logger.getLogger(JavaFX_Audio.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                audio.close();
            } catch (IOException ex) {
                Logger.getLogger(JavaFX_Audio.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}