Java Threading
November 21, 2009
TimerTextField.java
-
import java.util.Date;
-
-
import javax.swing.JTextField;
-
-
-
private boolean gogo = true;
-
@Override
-
public void run() {
-
// TODO Auto-generated method stub
-
Date d;
-
-
while(gogo)
-
{
-
this.setText(d.toString());
-
this.repaint();
-
try {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
-
-
public void closeMe()
-
{
-
gogo = false;
-
}
-
}
Apps.java
-
import java.awt.Component;
-
import java.awt.Rectangle;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import java.util.concurrent.ExecutorService;
-
import java.util.concurrent.Executors;
-
-
import javax.swing.JButton;
-
import javax.swing.JFrame;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextArea;
-
-
-
-
private TimerTextField ttf;
-
private JTextArea ta;
-
private JScrollPane jp;
-
private JButton jb;
-
public Apps()
-
{
-
-
this.setVisible(true);
-
this.setLayout(null);
-
this.setBounds(10,20,500,400);
-
init();
-
}
-
-
protected void init()
-
{
-
ttf = new TimerTextField();
-
-
-
jb.setText("Click Me");
-
-
-
@Override
-
// TODO Auto-generated method stub
-
jb_method(e);
-
-
}
-
-
});
-
-
}
-
-
{
-
ExecutorService es = Executors.newSingleThreadExecutor();
-
es.execute(ttf);
-
//ttf.run();
-
}
-
-
{
-
this.add(c);
-
c.setBounds(r);
-
-
}
-
-
{
-
new Apps();
-
}
-
}
Java Navigation
November 14, 2009
Main.java
-
public class Main {
-
new Apps();
-
}
-
}
Apps.java
-
import java.awt.Component;
-
import java.awt.Rectangle;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import java.util.List;
-
import java.util.ListIterator;
-
import javax.swing.JButton;
-
import javax.swing.JFrame;
-
import javax.swing.JLabel;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextArea;
-
import javax.swing.JTextField;
-
-
private JButton jbsave;
-
private JButton jbdisplay;
-
private JButton jbprev;
-
private JButton jbnext;
-
private JTextField team;
-
private JTextField player;
-
private JTextField salary;
-
private JTextField status;
-
private JLabel lblteam;
-
private JLabel lblplayer;
-
private JLabel lblsalary;
-
private List <Player> lm;
-
-
private HashMap<String,List <Player>> hm;
-
private int num = 1;
-
private String currTeam;
-
int TotalPlayer = 0;
-
int TotalTeam = 0;
-
int currPlayerIndex = 0;
-
Iterator<String> team_iterator;
-
Iterator<Player> player_iterator;
-
ListIterator <Player>li;
-
-
public Apps() {
-
init();
-
this.setLayout(null);
-
this.setVisible(true);
-
this.setBounds(10,20,400,400);
-
-
lm = new ArrayList<Player>();
-
}
-
private void init() {
-
-
-
addComponents(lblteam,40,40,100,25);
-
addComponents(lblplayer,40,70,100,25);
-
addComponents(lblsalary,40,100,100,25);
-
addComponents(team,150,40,100,25);
-
addComponents(player,150,70,100,25);
-
addComponents(salary,150,100,100,25);
-
addComponents(status,40,130,210,25);
-
addComponents(jbsave,40,170,100,25);
-
addComponents(jbdisplay,150,170,100,25);
-
addComponents(jbprev,40,200,100,25);
-
addComponents(jbnext,150,200,100,25);
-
-
lblteam.setText("Team");
-
lblplayer.setText("Player Name");
-
lblsalary.setText("Salary");
-
jbprev.setText("Previous");
-
jbnext.setText("Next");
-
-
jbsave.setText("Save");
-
jbdisplay.setText("Display");
-
status.setEnabled(false);
-
/*jbprev.setEnabled(false);
-
jbnext.setEnabled(false);*/
-
jbsave_actionPerformed(e);
-
}
-
});
-
-
jbdisplay_actionPerformed(e);
-
}
-
});
-
jbprev_actionPerformed(e);
-
}
-
});
-
jbnext_actionPerformed(e);
-
}
-
});
-
}
-
-
currPlayerIndex = 0;
-
TotalPlayer = 0;
-
team_iterator = hm.keySet().iterator();
-
TotalTeam = hm.size();
-
-
display();
-
jbnext.setEnabled(true);
-
}
-
-
display();
-
}
-
display();
-
}
-
private void display()
-
{
-
Player p;
-
if (team_iterator.hasNext())
-
{
-
if (TotalPlayer == 0)
-
{
-
currTeam = team_iterator.next();
-
TotalPlayer = lm.size();
-
li = lm.listIterator();
-
}
-
-
if (li.hasNext())
-
{
-
currPlayerIndex ++;
-
p = (Player) li.next();
-
team.setText(currTeam);
-
player.setText(p.getPlayer());
-
salary.setText(p.getSalary());
-
status.setText("Player " + currPlayerIndex + " of " + lm.size());
-
if (TotalPlayer == currPlayerIndex)
-
TotalPlayer = 0;
-
}
-
}
-
else
-
TotalTeam = 0;
-
}
-
Player p = new Player();
-
p.setPlayer(player.getText());
-
p.setSalary(salary.getText());
-
-
lm = hm.get(team.getText());
-
if (lm == null)
-
{
-
lm = new ArrayList<Player>();
-
}
-
lm.add(p);
-
-
hm.put(team.getText(), lm);
-
status.setText("Record Saved");
-
team.setText("");
-
player.setText("");
-
salary.setText("");
-
-
}
-
this.add(comp);
-
comp.setBounds(x, y, width, height);
-
}
-
}
Player.java
Java Hashmap
November 13, 2009
Pet.java
Apps.java
-
import java.awt.Component;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import java.io.BufferedReader;
-
import java.io.FileReader;
-
import java.io.FileWriter;
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import java.util.List;
-
import java.util.ListIterator;
-
-
import javax.swing.JButton;
-
import javax.swing.JFrame;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextArea;
-
import javax.swing.JTextField;
-
-
-
{
-
private JButton jbsave;
-
private JButton jbdisplay;
-
private JTextField name;
-
private JTextField age;
-
private JTextArea ta;
-
private HashMap<String,Integer> hm;
-
private List <Pet> lm;
-
private JScrollPane jscroll;
-
-
public Apps()
-
{
-
init();
-
this.setLayout(null);
-
this.setVisible(true);
-
this.setBounds(10,20,400,300);
-
-
hm = new HashMap<String,Integer>();
-
lm = new ArrayList<Pet>();
-
}
-
-
private void init()
-
{
-
-
addComponents(name,40,40,100,25);
-
addComponents(age,40,70,50,25);
-
addComponents(jbsave,40,100,70,25);
-
addComponents(jbdisplay,120,100,100,25);
-
addComponents(jscroll,40,160,150,100);
-
-
jscroll.getViewport().add(ta);
-
jbsave.setText("Save");
-
jbdisplay.setText("Display");
-
{
-
jbsave_actionPerformed(e);
-
}
-
});
-
-
{
-
{
-
try
-
{
-
jbdispaly_actionPerformed(e);
-
}
-
{
-
e1.printStackTrace();
-
}
-
}
-
});
-
}
-
-
{
-
String key;
-
ta.setText("HashMap size : " + hm.size());
-
Iterator<String> iterator = hm.keySet().iterator();
-
-
while( iterator. hasNext() )
-
{
-
key = iterator.next();
-
ta.append("\n"+key + " , " + hm.get(key));
-
}
-
-
Pet p;
-
ta.append("\nList size : " + lm.size());
-
-
while(li.hasNext())
-
{
-
p = (Pet) li.next();
-
ta.append("\n" + p.getName() + " , " + p.getAge());
-
}
-
}
-
{
-
Pet p = new Pet();
-
int i_age;
-
p.setName(name.getText());
-
p.setAge(i_age);
-
-
hm.put(name.getText(), i_age);
-
lm.add(p);
-
}
-
-
{
-
this.add(comp);
-
comp.setBounds(x, y, width, height);
-
}
-
}
Main.java
-
public class Main
-
{
-
{
-
new Apps();
-
}
-
}
Java Array
November 13, 2009
-
import java.util.Arrays;
-
-
public class MainProg {
-
-
/**
-
* @param args
-
*/
-
// TODO Auto-generated method stub
-
int num1[] = {1,2,3,4};
-
int num2[] = new int[4];
-
int num3[] = new int[4];
-
-
// to populate
-
for (int i=0 ; i <4 ; i++) {
-
num2[i] = i;
-
}
-
-
-
// to display
-
for ( int value : num2) {
-
}
-
-
int value;
-
for (int x=0 ; x <4 ; x++) {
-
value = num2[x];
-
}
-
-
}
-
}
-
import java.util.Arrays;
-
import java.util.Random;
-
-
-
public class ArrayEx2 {
-
-
/**
-
* @param args
-
*/
-
// TODO Auto-generated method stub
-
int e_matrix[][] = new int[3][3] ;
-
int w_matrix[][] = new int[4][];
-
int n_items;
-
-
-
for (int i=0 ; i <e_matrix.length; i++) {
-
}
-
display(e_matrix);
-
-
for (int i=0 ; i <w_matrix.length; i++) {
-
n_items = num.nextInt(4) + 1 ;
-
w_matrix[i] = new int[n_items];
-
-
for (int x=0 ; x <w_matrix[i].length ; x++) {
-
w_matrix[i][x] = num.nextInt(50);
-
}
-
}
-
display(w_matrix);
-
}
-
-
public static void display(int the_matrix[][]){
-
for (int y=0 ; y <the_matrix.length ; y++) {
-
for (int x=0 ; x <the_matrix[y].length ; x++) {
-
}
-
}
-
-
}
-
-
}
New Oracle SOA Book
June 25, 2009
Are you new to SOA and would like to learn how Oracle SOA Suite fits into the SOA world ? Or are you a SOA professional and looking for an all in one book on Oracle SOA Suite? Then wait no more, after a few months of waiting, Matt Wright and Antony Reynolds finished the Oracle SOA Suite Developer's Guide book and now available in the market.
I was assigned to research on how to integrate Oracle Retail application and our client's legacy system. I have done some research and found some online tutorials in Oracle website but I found most of them are using the BPEL Designer plugin of Eclipse. I have no luck of finding this plugin so I installed JDeveloper to create simple BPEL modules. Good thing Oracle SOA Suite Developer's Guide book came in to save my time finding the equivalent steps in the tutorial to JDeveloper because thoughout the book, the examples are using JDeveloper.
I'm familiar to basic SOA concepts because I have been reading IBM's SOA Redbook in the past. With my limited time to read the whole book, I jumped reading Chapter 3: Service-enabling existing systems which covers integration of new and legacy system. As most system designers and architects know, to integrate a legacy system to the another application, the integration modules are usually reading files to transfer data from one system to another.
Chapter 3 covers File Adapter for reading local files, FTP adapter for remotes files and DB Adapter which is use to data manipulation in the database. The steps are easy to follow and you will not get lost because there are screenshots provided to guide you.
For full details of the book, please visit the official book page in PackPub website.
Reading CSV File Using C Program
June 2, 2008
Since I'm on an integration project, I'm mostly dealing with transferring of data from two or more systems whether it's from the legacy system or newly implemented system. Most of the type of data movement is sending CSV files to and from the different system. Here is a simple tutorial on how to read CSV. The CSV file contains the following:
-
1111,1414,1000
-
1112,1010, 1001
-
1113,1112,1002
The fields are Item Number, Class Number and Supplier Number.
The example below is the simple code of reading CSV file in C.
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int main(int argc, char* argv[])
-
{
-
if (argc <2)
-
{
-
fprintf(stderr,"Usage: %s csv_file\n",argv[0]);
-
return(1);
-
}
-
-
FILE *f = fopen(argv[1], "rt");
-
char Line[256];
-
unsigned int AllocSize = 0, Size = 0, n;
-
char *L_text;
-
-
while(fgets(Line, sizeof(Line), f))
-
{
-
-
printf("Item = %s \n",strtok(Line, ", "));
-
printf("Class = %s \n",strtok(NULL, ", "));
-
printf("Supplier = %s \n",strtok(NULL, ", "));
-
-
}
-
return(0);
-
}
Determine the File Count in Unix Directory
May 21, 2008
To determine the number of files in the Unix directory, use the following command.
-
ls -l | grep -c "^-.*"
Merging Files in Unix
May 9, 2008
You have multiple files in containing data in similar format and wanted to consolidate in a single file. This is usually the case if you have a daily log that you want to place an archive copy in a single file in weekly or in monthly, yearly.
The basic syntax of merging file is
-
cat <source file> >> <target file>
If you have more than 10 files to merge it is better to write a shell script that will loop through the files and merge in a file. The following is the code for merging the files in a folder into a single file.
-
#!/bin/sh
-
echo enter file name
-
read filename
-
-
for arg in `ls;`
-
do
-
echo $arg
-
cat $arg >> $filename
-
done
Lines 2 and 3 prompts the user for the filename of the file which will contain the consolidated contents of all the files in the current folder.
Lines 5 to 9 loops through the files in the current folder and merge the files to a single file.
Simplest File Merging
-
#!/bin/sh
-
echo enter file name
-
read source_filemask
-
read master_filename
-
-
cat $arg >> $filename
A simple file merging
-
cat $<filemask>*> $<filename>
Example
-
cat $Test*> $FinalTest.txt
Writing Files in Unix
May 9, 2008
To write files in Unix, the syntax is
-
echo <text> > <filename>
Example
-
echo "This is my message." > myfile.txt
To add another line in the myfile.txt, use the following syntax.
-
echo "This is my second message." >> myfile.txt
You may notice that double greater than sign (>>) is used instead of single greater than sign. This means that the message is written in append mode.
Let's open the myfile.txt by using the following command.
-
tail myfile.txt
The content of the myfile.txt should have the following.
-
This is my message.
-
This is my second message.
How to display newline in Excel?
January 12, 2008
The commonly used to display newline in Visual Basic is using VbCrLF. But the problem is you will see a small rectangle at the end of the line.

-
Dim strFirstLine As String
-
Dim strSecondLine As String
-
-
strFirstLine = "This is the first line"
-
strSecondLine = "This is second line"
-
-
Worksheets("Sheet1").Range("b4").Value = _
-
strFirstLine & vbCrLf & strSecondLine
To solve this problem, you will need to change the code from VbCrLF to VbLf.
-
Dim strFirstLine As String
-
Dim strSecondLine As String
-
-
strFirstLine = "This is the first line"
-
strSecondLine = "This is second line"
-
-
Worksheets("Sheet1").Range("b4").Value = _
-
strFirstLine & vbLf & strSecondLine







Recent Comments