Tuesday, 11 September 2012

Checking for Null or Empty or White Space Only String in Java

/**
 * Demonstrate checking for String that is not null, not empty, and not white
 * space only using standard Java classes.
 *
 * @param string String to be checked for not null, not empty, and not white
 *    space only.
 * @return {@code true} if provided String is not null, is not empty, and
 *    has at least one character that is not considered white space.
 */
public static boolean isNotNullNotEmptyNotWhiteSpaceOnlyByJava(
   final String string)
{
   return string != null && !string.isEmpty() && !string.trim().isEmpty();
}


Refer:
http://marxsoftware.blogspot.in/2011/09/checking-for-null-or-empty-or-white.html

Struts2 Date Format

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Format Date Example!</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>

</head>
<body>


<s:form action="FormatDate" method="POST">


<tr>
<td>Date in (yyyy-MM-dd) Format:</td>
<td>
<b><s:date name="todayDate" format="yyyy-MM-dd" /></b>
</td>
</tr>

<tr>
<td>Date in (MM/dd/yyyy hh:mm AM/PM) Format:</td>
<td>
<b><s:date name="todayDate" format="MM/dd/yyyy 'at' hh:mm a" /></b>
</td>
</tr>

<tr>
<td>Date in (dd-MMM-yyyy hh:mm AM/PM) Format:</td>
<td>
<b><s:date name="todayDate" format="dd-MMM-yyyy 'at' hh:mm a" /></b>
</td>
</tr>

<tr>
<td>Date in (dd/MM/yyyy hh:mm) Format:</td>
<td>
<b><s:date name="todayDate" format="dd/MM/yyyy hh:mm" /></b>
</td>
</tr>

<tr>
<td>Nice Format:</td>
<td>
<b><s:date name="todayDate" format="yyyy-MM-dd" nice="true"/></b>
</td>
</tr>

</s:form>

</body>

</html>



Monday, 28 May 2012

Upload an image in swings


package basic;

import java.io.*;

import javax.swing.*;

import java.awt.Button;
import java.awt.Label;
import java.awt.event.*;

public class JLabelUploadImage {
static File file;
static JFrame frame;
private static JLabel label1;
static ImageIcon icon;


public static void main(String[] args) {
// final JPanel panel = new JPanel();
frame = new JFrame("Upload Image");
frame.setLayout(null);
label1 = new JLabel();

label1.setBounds(300, 300, 300, 100);
frame.add(label1);

final JFileChooser fc = new JFileChooser();
Button browse = new Button("Browse File");
browse.setBounds(200, 300, 40, 30);
frame.add(browse);
browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int returnval = fc.showOpenDialog(null);
if (returnval == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
System.out.println(file.getName());
}
}
});
Button button = new Button("Upload Image");
button.setBounds(100, 300, 40, 30);
frame.add(button);

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {
System.out.println("upload");
String path = file.getAbsolutePath();
System.out.println(path);
icon = new ImageIcon(path);
label1.setIcon(icon);
frame.add(label1);

System.out.println(icon.getIconHeight());

}

});
frame.add(browse);
frame.add(button);
frame.setSize(700, 700);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Saturday, 12 May 2012

Overriding


Q>What is the Output of this code?

class Parent {
    Number get() {
        return 1;
    }
}

class Child extends Parent {
    Integer get() {
        return 2;
    }
}

public class Test {
    public static void main(String[] args) {
        Parent yo = new Child();
        System.out.println(yo.get());
    }
}

***Output is in comment.

SCJP questions on Exception Handling


Q>What is the Output of this code?

public class Test {
public static void main(String[] args) {
for (int a = 0; a < 10; ++a) {
try {
if (a % 3 == 0)
throw new Exception("Except1");
try {
if (a % 3 == 1)
throw new Exception("Except2");
System.out.println(a);
} catch (Exception inside) {
a *= 2;
} finally {
++a;
}
} catch (Exception outside) {
a += 3;
} finally {
++a;
}
}
}
}


***Answer is in Comment