Monday, 4 June 2018

JavaFX: Create ChoiceBox

ChoiceBox class provides below constructors to create choice box widget.

public ChoiceBox()
public ChoiceBox(ObservableList<T> items)

Ex
ChoiceBox<String> choiceBox1 = new ChoiceBox<>();
choiceBox1.setItems(FXCollections.observableArrayList("Delhi", "Bombay", "Hyderabad"));

ChoiceBox<String> choiceBox2 = new ChoiceBox<>(FXCollections.observableArrayList("Chess", "Cricket", "Tennis"));

Find the below working application.

ChoiceBoxApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ChoiceBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  ChoiceBox<String> choiceBox1 = new ChoiceBox<>();
  choiceBox1.setItems(FXCollections.observableArrayList("Delhi", "Bombay", "Hyderabad"));

  ChoiceBox<String> choiceBox2 = new ChoiceBox<>(FXCollections.observableArrayList("Chess", "Cricket", "Tennis"));

  VBox vbox = new VBox(10, choiceBox1, choiceBox2);

  Scene scene = new Scene(vbox, 400, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Choice Box Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java

package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ChoiceBoxApp.class, args);
 }
}






Previous                                                 Next                                                 Home

JavaFX: ChoiceBox widget

By using ChoiceBox widget, you can able to select one among few options.
      Create ChoiceBox
      Add separator between choice elements
      Set tooltip to choice box
      Set the behavior to choice box

Previous                                                 Next                                                 Home

Sunday, 3 June 2018

Java: YAML utility class


package com.sample.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class YAMLUtil {

 private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

 public static void printYAML(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
  mapper.writeValue(System.out, obj);
 }

 public static String getYAML(Object obj) throws JsonProcessingException {
  return mapper.writeValueAsString(obj);
 }

 public static <T> T getObject(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  return mapper.readValue(yaml, clazz);

 }

 public static <T> List<T> getListOfObjects(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
  List<T> listOfObjects = mapper.readValue(yaml, listType);
  return listOfObjects;
 }

 public static <K, V> Map<K, V> getMapOfObjects(String yaml, Class<K> keyClazz, Class<V> valueClazz)
   throws JsonParseException, JsonMappingException, IOException {
  TypeFactory typeFactory = mapper.getTypeFactory();
  MapType mapType = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
  return mapper.readValue(yaml, mapType);
 }
}

Previous                                                 Next                                                 Home

Jackson: deserialise a map from yaml

Below statements convert the yaml string to a map.

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, Interger.class, Employee.class);
Map<String, Employee> map = mapper.readValue(yaml, mapType);

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {

 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}

YAMLUtil.java
package com.sample.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class YAMLUtil {

 private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

 public static void printYAML(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
  mapper.writeValue(System.out, obj);
 }

 public static String getYAML(Object obj) throws JsonProcessingException {
  return mapper.writeValueAsString(obj);
 }

 public static <T> T getObject(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  return mapper.readValue(yaml, clazz);

 }

 public static <T> List<T> getListOfObjects(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
  List<T> listOfObjects = mapper.readValue(yaml, listType);
  return listOfObjects;
 }

 public static <K, V> Map<K, V> getMapOfObjects(String yaml, Class<K> keyClazz, Class<V> valueClazz)
   throws JsonParseException, JsonMappingException, IOException {
  TypeFactory typeFactory = mapper.getTypeFactory();
  MapType mapType = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
  return mapper.readValue(yaml, mapType);
 }
}

Test.java
package com.sample.app;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.sample.model.Employee;
import com.sample.util.YAMLUtil;

public class Test {

 public static void main(String args[]) throws IOException {
  Map<Integer, Employee> map = new HashMap<>();

  Employee emp1 = new Employee();
  emp1.setId(1);
  emp1.setFirstName("Krishna");
  emp1.setLastName("Gurram");

  Employee emp2 = new Employee();
  emp2.setId(2);
  emp2.setFirstName("Rama");
  emp2.setLastName("Gm");

  map.put(emp1.getId(), emp1);
  map.put(emp2.getId(), emp2);

  System.out.println("Converting map to yaml");
  String yaml = YAMLUtil.getYAML(map);
  System.out.println(yaml);

  System.out.println("Converting yaml to map");
  Map<Integer, Employee> result = YAMLUtil.getMapOfObjects(yaml, Integer.class, Employee.class);

  for (Integer key : result.keySet()) {
   Employee emp = result.get(key);
   System.out.println("id : " + emp.getId());
   System.out.println("first name : " + emp.getFirstName());
   System.out.println("last name : " + emp.getLastName() + "\n");
  }

 }
}


Output
Converting map to yaml
---
1:
  id: 1
  firstName: "Krishna"
  lastName: "Gurram"
2:
  id: 2
  firstName: "Rama"
  lastName: "Gm"

Converting yaml to map
id : 1
first name : Krishna
last name : Gurram

id : 2
first name : Rama
last name : Gm







Previous                                                 Next                                                 Home

Jackson: deserialise list of objects from yaml

Below statements convert yaml to list of employees.

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
List<T> listOfObjects= mapper.readValue(yaml, listType);

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {

 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}

YAMLUtil.java

package com.sample.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class YAMLUtil {

 private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

 public static void printYAML(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
  mapper.writeValue(System.out, obj);
 }

 public static String getYAML(Object obj) throws JsonProcessingException {
  return mapper.writeValueAsString(obj);
 }

 public static <T> T getObject(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  return mapper.readValue(yaml, clazz);

 }

 public static <T> List<T> getListOfObjects(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
  List<T> listOfObjects = mapper.readValue(yaml, listType);
  return listOfObjects;
 }

 public static <K, V> Map<K, V> getMapOfObjects(String yaml, Class<K> keyClazz, Class<V> valueClazz)
   throws JsonParseException, JsonMappingException, IOException {
  TypeFactory typeFactory = mapper.getTypeFactory();
  MapType mapType = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
  return mapper.readValue(yaml, mapType);
 }
}

Test.java

package com.sample.app;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.sample.model.Employee;
import com.sample.util.YAMLUtil;

public class Test {

 public static void main(String args[]) throws IOException {
  Employee emp1 = new Employee();
  emp1.setFirstName("Krishna");
  emp1.setLastName("Gurram");
  emp1.setId(1);

  Employee emp2 = new Employee();
  emp2.setFirstName("Sailu");
  emp2.setLastName("N");
  emp2.setId(2);

  List<Employee> emps = Arrays.asList(emp1, emp2);

  String yaml = YAMLUtil.getYAML(emps);
  System.out.println("Yaml data of emps is : " + yaml);

  List<Employee> employees = YAMLUtil.getListOfObjects(yaml, Employee.class);

  System.out.println("\n\nData after deserialization");
  for (Employee emp : employees) {
   System.out.println("id : " + emp.getId());
   System.out.println("first name : " + emp.getFirstName());
   System.out.println("last name : " + emp.getLastName() + "\n");
  }

 }
}


Output

Yaml data of emps is : ---
- id: 1
  firstName: "Krishna"
  lastName: "Gurram"
- id: 2
  firstName: "Sailu"
  lastName: "N"



Data after deserialization
id : 1
first name : Krishna
last name : Gurram

id : 2
first name : Sailu
last name : N



Previous                                                 Next                                                 Home

Jackson: Convert yaml string to java object

Below statements convert yaml string to an employee object.

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Employee emp = mapper.readValue(yaml, Employee.class);

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {

 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}


YAMLUtil.java
package com.sample.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class YAMLUtil {

 private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

 public static void printYAML(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
  mapper.writeValue(System.out, obj);
 }

 public static String getYAML(Object obj) throws JsonProcessingException {
  return mapper.writeValueAsString(obj);
 }

 public static <T> T getObject(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  return mapper.readValue(yaml, clazz);

 }

 public static <T> List<T> getListOfObjects(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
  List<T> ts = mapper.readValue(yaml, listType);
  return ts;
 }

 public static <K, V> Map<K, V> getMapOfObjects(String yaml, Class<K> keyClazz, Class<V> valueClazz)
   throws JsonParseException, JsonMappingException, IOException {
  TypeFactory typeFactory = mapper.getTypeFactory();
  MapType mapType = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
  return mapper.readValue(yaml, mapType);
 }
}

Test.java
package com.sample.app;

import java.io.IOException;

import com.sample.model.Employee;
import com.sample.util.YAMLUtil;

public class Test {

 public static void main(String args[]) throws IOException {
  Employee emp = new Employee();
  emp.setFirstName("Krishna");
  emp.setLastName("Gurram");
  emp.setId(1);

  String yaml = YAMLUtil.getYAML(emp);

  Employee emp1 = YAMLUtil.getObject(yaml, Employee.class);

  System.out.println("id : " + emp1.getId());
  System.out.println("first name : " + emp1.getFirstName());
  System.out.println("last name : " + emp1.getLastName());
 }
}


Output
id : 1
first name : Krishna
last name : Gurram







Previous                                                 Next                                                 Home