Saturday, December 13, 2014

get NetworkInterface and my IP address

Example to display name and ip address of NetworkInterface.

reference: https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html


package javamyipaddress;

import static java.lang.System.out;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

/**
 *
 * @web http://java-buddy.blogspot.com/
 * ref: https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html
 */
public class JavaMyIpAddress {

    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %s\n", inetAddress);
        }
        out.printf("\n");
     }

}

Saturday, December 6, 2014

JavaFX example, setCursor() and getCursor()

JavaFX example to set cursor type to node by calling setCursor() and get cursor type by calling getCursor().


package javafxhello;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXHello extends Application {

    Scene scene;

    Button createButton(Cursor cursor) {
        Button btn = new Button();
        btn.setMnemonicParsing(false);
        //Sets the value of the property mnemonicParsing false to display '_'.
        /*
         MnemonicParsing property to enable/disable text parsing. 
         If this is set to true, then the Label text will be parsed 
         to see if it contains the mnemonic parsing character '_'. 
         When a mnemonic is detected the key combination will be 
         determined based on the succeeding character, 
         and the mnemonic added.
         */
        btn.setCursor(cursor);
        btn.setText(btn.getCursor().toString());
        
        btn.setOnAction((ActionEvent event) -> {
            Node src = (Node) event.getSource();
            scene.setCursor(src.getCursor());
        });
        return btn;
    }

    @Override
    public void start(Stage primaryStage) {

        VBox vBox = new VBox();
        scene = new Scene(vBox, 300, 250);

        Button btnCLOSED_HAND = createButton(Cursor.CLOSED_HAND);
        Button btnCROSSHAIR = createButton(Cursor.CROSSHAIR);
        Button btnDEFAULT = createButton(Cursor.DEFAULT);
        Button btnDISAPPEAR = createButton(Cursor.DISAPPEAR);
        Button btnE_RESIZE = createButton(Cursor.E_RESIZE);
        Button btnH_RESIZE = createButton(Cursor.H_RESIZE);
        Button btnHAND = createButton(Cursor.HAND);
        Button btnMOVE = createButton(Cursor.MOVE);
        Button btnN_RESIZE = createButton(Cursor.N_RESIZE);
        Button btnNE_RESIZE = createButton(Cursor.NE_RESIZE);
        Button btnNONE = createButton(Cursor.NONE);
        Button btnNW_RESIZE = createButton(Cursor.NW_RESIZE);
        Button btnOPEN_HAND = createButton(Cursor.OPEN_HAND);
        Button btnS_RESIZE = createButton(Cursor.S_RESIZE);
        Button btnSE_RESIZE = createButton(Cursor.SE_RESIZE);
        Button btnSW_RESIZE = createButton(Cursor.SW_RESIZE);
        Button btnTEXT = createButton(Cursor.TEXT);
        Button btnV_RESIZE = createButton(Cursor.V_RESIZE);
        Button btnW_RESIZE = createButton(Cursor.W_RESIZE);
        Button btnWAIT = createButton(Cursor.WAIT);

        vBox.getChildren().addAll(
                btnCLOSED_HAND, btnCROSSHAIR, btnDEFAULT,
                btnDISAPPEAR, btnE_RESIZE, btnH_RESIZE,
                btnHAND, btnMOVE, btnN_RESIZE, btnNE_RESIZE,
                btnNONE, btnNW_RESIZE, btnOPEN_HAND, btnS_RESIZE,
                btnSE_RESIZE, btnSW_RESIZE, btnTEXT, btnV_RESIZE,
                btnW_RESIZE, btnWAIT);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

Wednesday, December 3, 2014

JavaFX example of using scene.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT)



package javafxhello;

import javafx.application.Application;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXHello extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        HBox hBox123 = new HBox();
        hBox123.getChildren().addAll(
            new Label(" Label 1 "), 
            new Label(" Label 2 "),
            new Label(" Label 3 ")
        );
        
        HBox hBoxABC = new HBox();
        hBoxABC.getChildren().addAll(
            new Label(" Label A "), 
            new Label(" Label B "),
            new Label(" Label C ")
        );
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox123, hBoxABC);
        
        Scene scene = new Scene(vBox, 300, 250);
        
        scene.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT);
        //hBox123.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT);
        //hBoxABC.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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