titbits from the world less travelled

Archive for June, 2009

How to display a text file using Java Swing

I stored my files using .txt format. At first read the files using a BufferedReader from the text file. Read each line one by one and then store them in an object array since this what you will need to pass it to the JTable constructor.

My Jtable would have just one column.
Each row of my text file would be places in a row of the jtable.
appointmentTable would be the name of my Jtable. I would pass the object array to my jtable as below:
appointmentTable.setModel(new javax.swing.table.DefaultTableModel(
map, new String [] { Appointment } ));
where appointment would be the name of my column.

Always remember to close the reader, otherwise you would have problems getting out the data to your object array. Its that easy.

try {

Object[][] map = new Object[1000][1000];
BufferedReader in = new BufferedReader(new FileReader(C:\bankOfDarshan\appointment.txt));
String subStr;

int i=0;
while ((subStr = in.readLine()) != null) {
map[i][0] = subStr;
i++;
}
in.close();

appointmentTable.setModel(new javax.swing.table.DefaultTableModel(
map, new String [] { Appointment } ));
} catch (IOException ex) {
Logger.getLogger(PostItView.class.getName()).log(Level.SEVERE, null, ex);
}

posted by admin in Uncategorized and have No Comments

common mistake when comparing two objects

I just needed to bring this to everyones notice. equals method in String is for comparing two strings only. Read this in fine letters since many a times, I have done the mistake of comparing two Objects and get a error or they dont get compared at all.

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

posted by admin in Uncategorized and have No Comments

SilkTest 2006 R2: Not able to record testcases using IE 7

Recently I started to learn silktest, I was trying to capture the framework of my application using IE 7.0 I was stuck with the problem of silktest recognizing my web based application as a .exe application. I downgraded my IE to 6.0, enabled the extensions using extension handler-which is the extension handler for the system level, and also the extensions at the project level.

Inspite of this I was not able to rectify this. I then tried using firefox 1.5. It was working as specified.I am still not able to figure it out what the problem was using IE 7. I could uninstall IE7 but, I tried variety of ways to uninstall IE6 but was unsuccessful. It would be awesome if someone out there could tell me how to do it.

posted by admin in Uncategorized and have No Comments

silktest: conversion Integer to String(Vice Versa)

Inorder to convert a int or Integer value to String you can use the built-in function in silktest:

Str(intVal) – Over here Str is the built-in function for converting the intVal variable to a string variable.

Vice versa to convert a String to a int or Integer value, you can use Val to do it.
Its usage is as follows:

Val(strObject) – Over here Val is the built-in function for conversion between string to integer values.

posted by admin in Uncategorized and have No Comments

SILKTEST: Error Unique window : How to bypass it

Consider this scenario, we have a table which gives us the value of a particular product in the first row and the total product value in the last row. Incase we buy just a single product, then the first row value and the last row value remains the same. In this case let us consider you are getting the value of a HtmlText like this,

String value = BrowserWindow.HTMLTextNameProductName.getText()

String total = BrowserWindow.HTMLTextNameTotalValue.getText()

Total = 10 and Value = 10 since we just bought a single product. Now when we run the testcase, we are sure to get a window not unique error since we have the same values in the particular table. One simple way to bypass this write this simple statement

Agent.SetOption(OPT_VERIFY_UNIQUE,false)

This makes the unique option set to false.

posted by admin in Uncategorized and have No Comments

Easy tool to compare two files or folders.

Try using this tool DiffMerge. Its available free of cost for download. I used it. Its pretty cool. Its got a good GUI, where in you can compare two folders, two files and so on. Its got a 6MB download, and its good to go..from the first second. The download link is http://www.sourcegear.com/diffmerge/downloads.html. Tell me how does it work for you.

posted by admin in Uncategorized and have No Comments

Python: Reading XML data using DOM parser

A simple and effective way to read a xml using the DOM parser in python is shown below:
Sample example XML:

Hello World.

Python program to print the XML:


#import the dom from the xml folder.
from xml.dom import minidom
xmldoc = minidom.parse(Greeting.xml)
#print the xml in the command prompt
print xmldoc.toxml()

posted by admin in Uncategorized and have No Comments

Python: common mistake while searching for a keyword in a set of files

Recently, I was developing a tool for my own use which could help me find a keyword in a heap of files based on the keyword importance,date modified and so on. I was developing this tool to increase my own productivity, so that I could find the keywords I need, and then deploy the files based on that on the server. Although I had done the program right, there was something missing in the way I searched for the strings.
for line in inp.readlines():
for word in line.split():
#print word
if regex.match(word):
print Keyword +word+ exists in : +f
#else:
#print No Match
inp.close()

Can you point out the mistake here? the mistake was that I was only matching the strings. Python offers two different primitive operations based on regular expressions: match and search. If you are accustomed to Perls semantics, the search operation is what youre looking for. See the search() function and corresponding method of compiled regular expression objects.

Note that match may differ from search using a regular expression beginning with ^: ^ matches only at the start of the string, or in MULTILINE mode also immediately following a newline. The “match operation succeeds only if the pattern matches at the start of the string regardless of mode, or at the starting position given by the optional pos argument regardless of whether a newline precedes it.

for line in inp.readlines():
for word in line.split():
#print word
if regex.search(word):
print Keyword +word+ exists in : +f
#else:
#print No Match
inp.close()

The above code just works fine for searching keyword in a set of files. You can use os.walk to go through a directory structure.

posted by admin in Uncategorized and have No Comments

Python: Quick recipe for reading a rar file

First of all, download the rarfile.py from http://grue.l-t.ee/~marko/src/rarfile/README.html. This python program contains all the classes and methods to handle a .rar file. Download it and save it in the LIB folder.

# check if it is rarfile where f is a file
if tarfile.is_rarfile(f):
# if it is a rar file then open the file for reading
z = rarfile.RarFile(f, r)
# get all the filenames in the .rar package
for filename in z.namelist():
print filename
In this way you can manipulate a rar file. In the same way you can manipulate a tar file or a zip file by using the zipfile class or tar file class in python. But in tarfile class we have the getnames() class object we should be using instead of nameslist() class object.

posted by admin in Uncategorized and have No Comments

SQL Server: View all columns of a table

This query lets select the 0th row, over here it seems to be the column names row.Change the table name to your name of choice.

select top 0 * from table_name

The below query uses the system objects to select the columns from the system objects.

SELECT name FROM sys.columns WHERE OBJECT_ID IN (SELECT OBJECT_ID (table_name))

posted by admin in Uncategorized and have No Comments