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++) {
-
}
-
}
-
-
}
-
-
}
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);
-
}
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

Pro *C Programming Do’s and Don’t
November 18, 2007
- Use one array per action such as inserting, updated and deleting records.
- Always increment the array counter every time you add new records.
- Always resize array every time you add new records.
- Always reset the array counter after the bulk update, insert and delete.
- Inserting or updating records should comes last if the value of column in comes from a separate logic such as calculation, sequence retrieval.
Software Best Practices Conference
September 23, 2007
I want to share ideas from Youdon. His presentation for Software Best Practices Conference is about Ten Most Important Ideas in Software Engineering. Here is what he talks about:
- You can't control what you can't measure
- Peopleware
- Incrementalism
- Iteration
- Repair costs increase
- Tradeoffs are non-linear
- Reuse is important
- Risk management provides insights
- Consistency trumps brillance + death march
- Don't reinvent the wheel
You can download the pdf here.






Recent Comments