Thursday, August 13, 2015

Java Serializable Objects Usage

In order to break down Class object into bytes steam, Java.Io.Serializable is used. Whenever you want to send data over network, make sure to check if your class is implementing Serializable or not. Some points to remember to get rid of Serializable related errors.

  • If there is any inner class, it must be declared Serializable.
  • If there is any member variable of class which you don't want to send (or make serializable), declare it TRANSIENT.
  • If there is any arraylist declard e.g. Obseravable List from FX Collections, then remember that this type of list is not serializable and it will create errors. Use Simple Array List instead of FX Collections Observable List.


Example :

public class Patent implements java.io.Serializable 
{
   public String name;
   public String address;
   public transient int CNIC;
   public int number;

}

Friday, August 7, 2015

JavaFx - Validating Numeric Only Text Field

Earlier, I posted about validating IP in a Text Field. Thought of sharing information about Numeric Only validation too. So, its pretty simple. Here are some lines of codes

1. @FXML
    private TextField port;

2.  if (!validatePort(port.getText())) {
                showDialog("Invalid Port");
            }

3.  public boolean validatePort(final String port) {
        Pattern pattern;
        Matcher matcher;
        String PORT_PATTERN
                = "^([0-9]+)$";
        pattern = Pattern.compile(PORT_PATTERN);
        matcher = pattern.matcher(port);
        return matcher.matches();
    }

JavaFx - Validating Text Field with IP

Recently, I happen to come across IP validation problem in JavaFX code. So, I googled and googled. In the end, I found the relevant information. Here are some lines of codes, which is useful. Use it when you need it.

1. @FXML
    private TextField ip;

2.  if (!validateIP(ip.getText())) {
                showDialog("Invalid IP Address");
            }

3.  public boolean validateIP(final String ip) {
        Pattern pattern;
        Matcher matcher;
        String IPADDRESS_PATTERN
                = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
        pattern = Pattern.compile(IPADDRESS_PATTERN);
        matcher = pattern.matcher(ip);
        return matcher.matches();
    }