Event handling in Java December 19, 2006
Posted by deltawing in Java, Programming.1 comment so far
Event handling has always confused me. So I’m going to summarise it all here.
//Handling mouse events
public boolean mouseDown (Event e, int x, int y)
{
//code here
- return true;
}
- what is an Actionlistener why is it needed?
- MouseListener? why is it needed
-actionPerformed? getSrc?
If there are several buttons, textfields, etc that need to be handled, use actionPerformed(ActionEvent e)
public void actionPerformed (ActionEvent event) {
String cmd = event.getActionCommand ();
if ( cmd.equals (“A”)){
fPushACount++;
showStatus (event.getActionCommand () +
” pushed ” + fPushACount + ” times”);
}else{
fPushBCount++;
showStatus (event.getActionCommand () +
” pushed ” + fPushBCount + ” times”);
}
} // actionPerformed()
Don’t forget to attach the listeners to the event source (ie buttons or else it wont work)!!
Passing arrays to methods (Java) September 26, 2006
Posted by deltawing in Java.add a comment
This confuses people from time to time. Let’s clear it up now.
to declare an array you do this:
int[] somearray = new int[20];
or if you prefer,
int somearray[] = new int[20] ;
doSomething( somearray ); //when passing an array, do not include the square backets
public void doSomething( int somearray [] ) //when receiving an array, have the brackets in place. Also, do not include the array size in the brackets.
{..}
Dealing With Files & Directories with Java August 30, 2006
Posted by deltawing in Java.2 comments
Short post:
Had a lab today where one of the exercises was to write a simple application that finds a file in the specified directory. I didn’t know how to deal with files and directories so I went searching online.
A very useful resource that I came across:
http://javaalmanac.com/egs/java.io/TraverseTree.html?l=rel
The almanac has tons of useful resources, just search!
Example of a LinkedList August 23, 2006
Posted by deltawing in Computer Science, Java.2 comments
Here is a very simple example of a LinkedList to get started with:
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
public class LinkedListTest
{
public static void main(String[] args)
{
//create a new LinkedList
List list = new LinkedList();
//adding elements to the list. We are adding Strings.
list.add(“i”);
list.add(“like”);
list.add(“to”);
list.add(“code with”);
list.add(“Java 5″);
//the list object calls the iterator() method of the LinkedList to obtain the iterator
Iterator iter = list.iterator();
while(iter.hasNext())
{
System.out.println(iter.next());
}
}
}
—————————————————————————————–
LinkedLists are:
- Very efficient for inserting and removing elements.
- Not so fast when accessing elements in order (though not slower than a normal ArrayList)
- Slow when randomly accessing elements
- Java 1.5 version supports Autboxing
- Uses an Iterator object to traverse through the elements
- Comes in more flavours – Doubley Linked Lists and Circular Linked Lists.
Please, corrections if mistakes have been made!