Wednesday, August 19, 2009

RegEx Test - Swing Application


If validation is part of your chore or you are a RegEx beginner, this tool could make life easier.

Download the source from here.


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegExTest extends JFrame {
private static final long serialVersionUID = 1L;
private static int WIDTH = 300;
private static int HEIGHT = 150;

private JFrame parent;
private JLabel lblPattern, lblTestString;
private JTextField tfPattern, tfTestString;
private JButton btnTest;

public RegExTest() {
super("RegEx Test");
parent = this;

lblPattern = new JLabel("Pattern");
lblTestString = new JLabel("Test String");
tfPattern = new JTextField();
tfTestString = new JTextField();
btnTest = new JButton("Test");

setLayout(new GridLayout(5,1));
add(lblPattern); add(tfPattern);
add(lblTestString); add(tfTestString);
add(btnTest);

setResizable(false);
setBackground(Color.lightGray);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLocation(200, 200);
setVisible(true);

btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pattern = tfPattern.getText().toString();
String testString = tfTestString.getText().toString();

if(testString.matches(pattern))
JOptionPane.showMessageDialog(parent, "POSITIVE", "RegEx Test Result!", JOptionPane.INFORMATION_MESSAGE, null);
else
JOptionPane.showMessageDialog(parent, "NEGATIVE", "RegEx Test Result!", JOptionPane.INFORMATION_MESSAGE, null);
}
});
}

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

Monday, August 10, 2009

Random Number Generator

import java.util.Random;

public class RandomDemo {
public static void main(String[] args) {
Random random = new Random();
System.out.println("boolean: " + random.nextBoolean());
System.out.println("int: " + random.nextInt());
System.out.println("double: " + random.nextDouble());
System.out.println("float: " + random.nextFloat());
System.out.println("int (scaled 0-99): " + random.nextInt(100));
}
}

iEvony Invitation Codes

Join iEvony and earn your free game cents to build your empire. Signup from ragunathjawahar.evony.com

Invitation CodeAdded DateStatus
au9xw2ddjmlb6o62009-08-09 10:07:19Active
v2xpi5xempeobww2009-08-09 10:07:19Active
xm66jipcw8exjk12009-08-09 10:07:19Active
5wkil2niwpnqez22009-08-09 10:07:20Active
qwodjnv8z7wekge2009-08-09 10:07:20Active
m1a6kvont0dhq7w2009-08-09 10:07:20Active
8x8wa7jmfytcse82009-08-09 10:07:20Active
7gph29d6dm6qjcd2009-08-09 10:07:20Active
fka4w1bfn7twzmq2009-08-09 10:07:20Active
nt6cqo2juf6glp82009-08-09 10:07:20Active