Junit 4 Tutorial and Example – Get Starting

21Aug2010 In: Junit

Junit 4 Tutorial and example

  1. If you use Idea or Eclipse, The Junit IDE plug-in is normally included as a part of the IDE distribution.
  2. If do not using Idea or Eclipse, you can still use Junit as a standalone testing tool by downloading it from(http://www.junit.org).

Creating a Junit Test case

  1. Open the IDE, create a test project, if needed, please import the Junit library to your current project dependency libraries.
  2. Create a test class named SimpleTest and extends junit.framework.TestCase in the test project. In generic, the best practice is to define the test class name to start with ‘test’ and plus class name which need to be tested. (in case you use Junit4, it is no longer necessary to extend junit.framework.TestCase)
  3. Write a test method and assert desired results. If you use the Junit 3, you have to conventional define string ‘test’ as the prefix of test method, but in junit 4 or later, you can use annotation @Test to declare it, then Junit engine will explain it as a test method. Read the rest of this entry »

Java Regular Expression to validate email address

29Jul2010 In: Core Java

A regular expression, specified as a string Pattern, be compiled into an instance of this class to search, edit or manipulate text and data. Today we guide how Java regular expression to validate the email address. we created a particular expression ^\\S+@\\S+$, then use it to match the email address:

Way 1: validate email using Java Regular Expression

import java.util.regex.*;
public class JavaRegular{
  public static void main (String [] args) {
    Pattern pattern = Pattern.compile("^\\S+@\\S+$");
    String email = "abc@12.com"
    Matcher matcher = pattern .matcher(email);
    if (matcher .matches()) {
              System.out.println (email  + " is a valid email address");
     } else {
              System.out.println (email  + " is not a valid email address");

Way 2: validate email using Java Regular Expression.
In this way, we use a different regular expression ^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$ to match an email address.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidateEmailAddress{
  public static void main(String args[]){
    String  expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence email = "ab.cd@xyz.com";
    Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(email );
      System.out.println(email + " is a valid email address.");
      System.out.println(email + " is an invalid email address.");

ButtonGroup example

20Jul2010 In: Swing

The ButtonGroup Class
The ButtonGroup class allows buttons to be logically grouped, guaranteeing that no more than one button in the group is selected at any given time. In fact, once one of the buttons is selected, the ButtonGroup ensures that exactly one button remains selected at all times. Note that this allows for an initial state (in which no button is selected) that can never be reached again once a selection is made, except programmatically.

As mentioned earlier, ButtonGroups typically hold JRadioButtons , but this is purely a convention and is not enforced by ButtonGroup. ButtonGroup’s add() method takes objects of type AbstractButton, so any button type may be added ven a mix of types. Of course, adding a JButton to a ButtonGroup would not be very useful since JButtons do not have selected and deselected states. In fact, JButtons added to ButtonGroups have no effect on the state of the other buttons if they are pressed.

ButtonGroup objects do not have any visual appearance; they simply provide a logical grouping of a set of buttons. You must add buttons in a ButtonGroup to a Container and lay them out as though no ButtonGroup were being used.

It’s worth noting that some methods in the ButtonGroup class deal with AbstractButton objects and some deal with ButtonModel objects. The add(), remove(), and getElements() methods all use AbstractButton, while the getSelection(), isSelected(), and setSelected() methods use ButtonModel objects.

ButtonGroup Properties
The buttonCount property is the number of buttons in the group. The elements property is an Enumeration of the AbstractButton objects contained by the group. The selection property contains the ButtonModel of the currently selected button.
Voting with a Button Group
The following example demonstrates the use of a ButtonGroup to ensure that only a single selection is made from a list of choices. Listeners are added to the buttons to show which events are fired each time a new button is selected. Read the rest of this entry »

Java Timer Tutorial

12Jul2010 In: Swing

The propose of this tutorial and example is to learn the Java timer basic knowledge and usage.

The Timer Class introduction

The Timer class provides a mechanism to generate timed events. It has properties and events, and thus can be used in application builders that understand JavaBeans. It fires an ActionEvent at a given time. The timer can be set to repeat, and an optional initial delay can be set before the repeating event starts.

Java timer Properties

The Timer class properties give you access to the timer delays and nature of the event firing loops. They are listed in Table The delay property dictates the length between repeated timer events (if repeats is true) and initialDelay determines how long to wait before starting the regular, repeating events. Both properties expect values in milliseconds. If your timer is not repeating, then the value of initialDelay determines when the timer fires its event. You can check to see if the timer is running with the running property. The coalesce property dictates whether or not the timer combines pending events into one single event (to help listeners keep up). For example, if the timer fires a tick every 10 milliseconds, but the application is busy and has not handled events for 100 milliseconds, 10 action events are queued up for delivery. If coalesce is false, all 10 of these are delivered in rapid succession. If coalesce is true (the default), only one event is fired. The logTimers property can be turned on to generate simple debugging information to the standard output stream each time an event is processed. Read the rest of this entry »

JToggleButton example

9Jul2010 In: Swing

This topic is about JToggleButton example and tutrial. Let’s you quickview how to use JToggleButton:

The JToggleButton Class(JToggleButton example and tutrial class introduction)

JToggleButton is an extension of AbstractButton and is used to represent buttons that can be toggled on and off (as opposed to buttons like JButton which, when pushed, “pop back up”). It should be noted that while the subclasses of JToggleButton (JCheckBox and JRadioButton) are the kinds of JToggleButtons most commonly used, JToggleButton is not an abstract class. When used directly, it typically (though this is ultimately up to the L&F) has the appearance of a JButton that does not pop back up when pressed.

The JToggleButton class inherits all of its properties and most of its default values from its superclass. The exceptions are shown in Table 5-10. The model property is set to a new instance of ToggleButtonModel when a JToggleButton is created. ToggleButtonModel (described in the next section) is a public inner class that extends DefaultButtonModel. Read the rest of this entry »

CXF vs AXIS2 Comparison

7Jul2010 In: Web Services

CXF VS AXIS2 Overview

Both CXF and axis are famous open web service framework, there are new next-generation which evolved from existing projects and introduced many innovative thoughts, therefore, it’s difficult to select one but give rid of another one. The two web services frameworks have been released several years and used widely, now they are under the umbrella of Apache. Read the rest of this entry »

How Do I Set JLabel Font Size and Color

3Jul2010 In: Swing

JLabel is a display area for a short text string or an image, or both, it is a basic GUI Component defined in Java Swing library. A label does not react to input events. As a result, it cannot get the keyboard focus. In this how-to, we will go over how to set appearance: JLabel font size and color.

Example Source Code to set JLabel font size Read the rest of this entry »

How to Set JLabel Background Color

1Jul2010 In: Swing

Traditionally the background color of JLable is transparent, we can change it by using following code:

JPanel panel = new JPanel();
JLabel label = new JLabel("JLabel Demo");
panel .add(label);

Code label.setOpaque(true) is required to paint the background color of JLabel.

JoptionPane Showinputdialog With Password

28Jun2010 In: Swing

The needs is that we need have a dialog to ask user user name and password so user are to be granted appropriate privilege to access resource, we will use JoptionPane Showinputdialog but with password textbox presented.

How JoptionPane showinputdialog dialog are construed?

Name: <jtextfield>
Password: <JPasswordField>
Button <OK> Button <NO>

Read the rest of this entry »

JoptionPane showmessagedialog example

26Jun2010 In: Swing

JOptionPane is used to prompt up a dialog to accept user input or informs a message, four types of dialog are supported: Message dialog, Confirmation dialog, Input dialog, and Options dialog.

Today we get learn how to use JoptionPane showmessagedialog – tutorial and example.

3 static methods are defined in class JOptionPane you can directly call neither one to create a dialog and display a message in dialog:

  • public static void showMessageDialog(Component parentComponent,
    Object message) throws HeadlessException;
  • public static void showMessageDialog(Component parentComponent,
    Object message, String title, int messageType) throws HeadlessException
  • public static void showMessageDialog(Component parentComponent,
    Object message, String title, int messageType, Icon icon) throws HeadlessException Read the rest of this entry »