Thursday, November 27, 2014

Example of ListChangeListener for javafx.collections.ObservableList


package javafxobservablelistchangelistener;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXObservableListChangeListener extends Application {
    
    int count = 0;
    ObservableList observableList;
    
    @Override
    public void start(Stage primaryStage) {
        
        observableList = FXCollections.observableArrayList();
        observableList.addListener(new ListChangeListener(){

            @Override
            public void onChanged(ListChangeListener.Change c) {
                System.out.println("\nonChanged()");

                while(c.next()){
                    System.out.println("next: ");
                    if(c.wasAdded()){
                        System.out.println("- wasAdded");
                    }
                    if(c.wasPermutated()){
                        System.out.println("- wasPermutated");
                    }
                    if(c.wasRemoved()){
                        System.out.println("- wasRemoved");
                    }
                    if(c.wasReplaced()){
                        System.out.println("- wasReplaced");
                    }
                    if(c.wasUpdated()){
                        System.out.println("- wasUpdated");
                    }
                }
                
                for(Object i : observableList){
                    System.out.println(i);
                }
            }
        });

        Button btnAdd = new Button();
        btnAdd.setText("Add item");
        btnAdd.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                observableList.add(count);
                count++;
            }
        });
        
        Button btnRemove = new Button();
        btnRemove.setText("Remove item");
        btnRemove.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                
                int size = observableList.size();
                if(size > 0){
                    observableList.remove(size-1);
                }
            }
        });
        
        Button btnReplace = new Button();
        btnReplace.setText("Replace last item (+1)");
        btnReplace.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                
                int size = observableList.size();
                if(size > 0){
                    observableList.set(
                        size-1, 
                        (int)observableList.get(size-1)+1);
                }
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(btnAdd, btnRemove, btnReplace);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


Wednesday, November 19, 2014

Java in a Nutshell

Java in a Nutshell

The latest edition of Java in a Nutshell is designed to help experienced Java programmers get the most out of Java 7 and 8, but it’s also a learning path for new developers. Chock full of examples that demonstrate how to take complete advantage of modern Java APIs and development best practices, the first section of this thoroughly updated book provides a fast-paced, no-fluff introduction to the Java programming language and the core runtime aspects of the Java platform.

The second section is a reference to core concepts and APIs that shows you how to perform real programming work in the Java environment.
  • Get up to speed on language details, including Java 8 changes
  • Learn object-oriented programming, using basic Java syntax
  • Explore generics, enumerations, annotations, and lambda expressions
  • Understand basic techniques used in object-oriented design
  • Examine concurrency and memory, and how they’re intertwined
  • Work with Java collections and handle common data formats
  • Delve into Java’s latest I/O APIs, including asynchronous channels
  • Use Nashorn to execute JavaScript on the Java Virtual Machine
  • Become familiar with development tools in OpenJDK


Thursday, November 13, 2014

Building the Internet of Things with Java


Streamed live on Nov 6, 2014

It may seem hard to get started with the Internet of Things (IoT) with so many technologies, protocols, hardware platforms, … involved. In this session, Benjamin Cabé from the Eclipse Foundation will cover all you need to know to start building IoT solutions using Java and open technologies like MQTT, CoAP, OSGi, or LWM2M. A real-life application (smart greenhouse) will be used as an example and you will most likely leave the session craving for building a very similar application yourself!

Speaker: Benjamin Cabé

Benjamin Cabé is IoT evangelist at the Eclipse Foundation. He advocates the use of open and innovative technologies for the Internet of Things and likes building crazy machines with Arduino kits and all kinds of open hardware platforms. You can find him online on Twitter (@kartben) or on his blog: http://blog.benjamin-cabe.com.

Saturday, November 8, 2014

List and check ConditionalFeature

javafx.application.ConditionalFeature defines a set of conditional (optional) features. These features may not be available on all platforms. An application that wants to know whether a particular feature is available may query this using the Platform.isSupported() function. Using a conditional feature on a platform that does not support it will not cause an exception. In general, the conditional feature will just be ignored. See the documentation for each feature for more detail.

package javaconditionalfeature;

import javafx.application.ConditionalFeature;
import javafx.application.Platform;

public class JavaConditionalFeature {


    public static void main(String[] args) {
        for (ConditionalFeature c : ConditionalFeature.values()){
            System.out.print(c + " : ");
            if(Platform.isSupported(c)){
                System.out.println("supported");
            }else{
                System.out.println("not supported");
            }
        }
    }    
}