Sunday, July 29, 2012

Java 7 new feature switch-case for String

In Java 7, you can use String in switch-case conditional statement:

package javaexercise;

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

    public static void main(String[] args) {
            String result;
            result = doSwitch("Monday");
            System.out.println(result);
        }
    
    private static String doSwitch(String in){
        String out;
        switch (in){
            case "Sunday":
                out = "Sunday";
                break;
            case "Monday":
                out = "Monday";
                break;
            case "Tuesday":
                out = "Tuesday";
                break;
            case "Wednesday":
                out = "Wednesday";
                break;
            case "Thursday":
                out = "Thursday";
                break;
            case "Friday":
                out = "Friday";
                break;
            case "Saturday":
                out = "Saturday";
                break;
            default:
                out = "unknown!";
        }
        return out;
    }
}


Thursday, July 26, 2012

NetBeans IDE 7.2 released

NetBeans IDE 7.2 released: http://netbeans.org/


NetBeans IDE 7.2 provides a significantly improved performance and coding experience, with new static code analysis capabilities in the Java Editor and smarter project scanning. Other notable features include integration with JavaFX Scene Builder; support for multiple PHP frameworks; updated Groovy support; and enhancements for Java EE, Maven, C/C++ and the NetBeans Platform.


Tuesday, July 3, 2012

JavaFX TreeView with icon

Example of JavaFX TreeView with icon:

JavaFX TreeView with icon


package javafxtest;

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXtestUI extends Application {
    
    Image nodeImage = new Image(
            getClass().getResourceAsStream("duke_16x16.png"));

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        
        TreeItem<String> treeItemRoot = new TreeItem<> ("Root");
        
        TreeItem<String> nodeItemA = new TreeItem<>("Item A");
        TreeItem<String> nodeItemB = new TreeItem<>("Item B");
        TreeItem<String> nodeItemC = new TreeItem<>("Item C");
        treeItemRoot.getChildren().addAll(nodeItemA, nodeItemB, nodeItemC);
        
        TreeItem<String> nodeItemA1 = new TreeItem<>("Item A1", 
                new ImageView(nodeImage));
        TreeItem<String> nodeItemA2 = new TreeItem<>("Item A2", 
                new ImageView(nodeImage));
        TreeItem<String> nodeItemA3 = new TreeItem<>("Item A3", 
                new ImageView(nodeImage));
        
        nodeItemA.getChildren().addAll(nodeItemA1, nodeItemA2, nodeItemA3);
        
        TreeView<String> treeView = new TreeView<>(treeItemRoot);
        StackPane root = new StackPane();
        root.getChildren().add(treeView);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}


Monday, July 2, 2012

Simple JavaFX TreeView example

JavaFX provide TreeView control, a view on to a tree root (of type TreeItem). By using a TreeView, it is possible to drill down into the children of a TreeItem, recursively until a TreeItem has no children (that is, it is a leaf node in the tree).

Simple TreeView example


package javafxtest;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXtestUI extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        
        TreeItem<String> treeItemRoot = new TreeItem<> ("Root");
        
        TreeItem<String> nodeItemA = new TreeItem<>("Item A");
        TreeItem<String> nodeItemB = new TreeItem<>("Item B");
        TreeItem<String> nodeItemC = new TreeItem<>("Item C");
        treeItemRoot.getChildren().addAll(nodeItemA, nodeItemB, nodeItemC);
        
        TreeItem<String> nodeItemA1 = new TreeItem<>("Item A1");
        TreeItem<String> nodeItemA2 = new TreeItem<>("Item A2");
        TreeItem<String> nodeItemA3 = new TreeItem<>("Item A3");
        nodeItemA.getChildren().addAll(nodeItemA1, nodeItemA2, nodeItemA3);
        
        TreeView<String> treeView = new TreeView<>(treeItemRoot);
        StackPane root = new StackPane();
        root.getChildren().add(treeView);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}


Next:
- JavaFX TreeView with icon