Saturday, June 8, 2013

Print all digit in Unicode using java.lang.Character

The java.lang.Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.

In addition, this class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

Character information is based on the Unicode Standard, version 6.0.0.

Character.MAX_CODE_POINT is the maximum value of a Unicode code point, constant U+10FFFF.

Character.isDigit(int codePoint) method determines if the specified character (Unicode code point) is a digit.


It's a example to print all digit in Unicode:

Print all digit in Unicode
Print all digit in Unicode

package java_unicodecodepoint;

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

    public static void main(String[] args) {
        System.out.println("http://java-buddy.blogspot.com/");
        int maxCodePoint = Character.MAX_CODE_POINT;
        System.out.printf("MAX_CODE_POINT = %x\n", maxCodePoint);
        
        //print all digital Unicode
        for(int i=0; i<=maxCodePoint; i++){
            if(Character.isDigit(i)){
                System.out.printf("Unicode: %x\t%c\tName:%s\n", 
                    i, i, Character.getName(i));
            }
        }
    }
}


Sunday, June 2, 2013

JavaFX ComboBox for custom object

The post "Simple example of JavaFX ComboBox" with elements of String. This post demonstrate how to implement ComboBox for custom object, we have to override toString() method of the custom class to display text on the box, and also call setCellFactory() to supply Callback to display text on the drop-down ListView.

JavaFX ComboBox for custom object
JavaFX ComboBox for custom object


package javafxcombobox;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXComboBox extends Application {
    
    class Site{
        String name;
        String webaddr;
        
        Site(String n, String a){
            name = n;
            webaddr = a;
        }

        @Override
        public String toString() {
            return name;
        }

    }
    
    ObservableList<Site> siteList = 
            FXCollections.observableArrayList(
                new Site("Google", "http://www.google.com"),
                new Site("MicroSoft", "http://www.microsoft.com"),
                new Site("Apple", "http://www.apple.com"),
                new Site("Java-Buddy", "http://java-buddy.blogspot.com/")
            );

    @Override
    public void start(Stage primaryStage) {
        
        final ComboBox comboBox = new ComboBox(siteList);
        comboBox.getSelectionModel().selectFirst(); //select the first element
        
        comboBox.setCellFactory(new Callback<ListView<Site>,ListCell<Site>>(){

            @Override
            public ListCell<Site> call(ListView<Site> p) {
                
                final ListCell<Site> cell = new ListCell<Site>(){

                    @Override
                    protected void updateItem(Site t, boolean bln) {
                        super.updateItem(t, bln);
                        
                        if(t != null){
                            setText(t.name + ":" + t.webaddr);
                        }else{
                            setText(null);
                        }
                    }
 
                };
                
                return cell;
            }
        });
        
        final Label label = new Label();
        
        Button btn = new Button();
        btn.setText("Read comboBox");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                label.setText("selected: " + comboBox.getValue());
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(label, comboBox, btn);
        
        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);
    }
}


IntelliJ IDEA 12.1 now support JavaFX 2, Android UI designer, Java 8 and more...

IntelliJ IDEA 12.1 now support:
  • JavaFX 2.0
  • Android UI designer
  • Play framework 2.0
  • Spring frameworks
  • Java 8
  • Faster compiler
  • and many more...
Getting Started with Java 8 in IntelliJ IDEA 12.1

JavaFX 2 Support in IntelliJ IDEA 12.1