| Tech |
Title |
Updated |
|
 | Setting the Default Close Operation on a Java Application | 2012-04-11 11:30:31 |
When running a java Application which terminates, there can still be threads running that do not stop.
To ensure that everything is correctly stopped and all java instances are removed, you can add the following to your
JFrame.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This will terminate all threads and exit the application cleanly.
You can also use
JFrame.DISPOSE_ON_CLOSE
But this relies on there being a window, as it is called after the last window is closed. |
|
 | Simple Hello World Java Application | 2012-04-11 11:36:01 |
// a new JFrame
public class myClass() extends JFrame
{
// constructor for the class
public myClass()
{
super("Hello World"); // call the super constructor for the JFrame, and set the frame title
setBounds(0,0,200,200); // set the window size to 200x200
setVisible(true); // after this the window is visible. Without this call, the program will terminate.
}
// the entry point , this is the first bit of code that is executed.
public static void main( String[] args )
{
// create a new myClass object
new myClass();
}
}
|
|
 | How to delete a file in Java | 2012-04-12 16:26:03 |
Deleting a file is very simple in a Java application
File f1= new File("filename");
boolean itWorked = f1.delete();
itWorked will return true if the file is deleted.
If the file does not exist then you will get false. |
|
 | Load a Binary file into Java | 2012-04-17 09:55:36 |
A Simple way to load a binary file into a byte[] is as follows:
File file=new File("filename");
int size=file.length(); // get File Length
byte[] contents=new byte[size]; // allocate buffer
FileInputStream in=new FileInutStream(file); // input stream
in.read(contents); // Read the data
in.close(); // Close the file |
|
 | Get the current working directory in Java | 2012-04-17 10:09:26 |
to get the directory where the application was invoked use:
String curDir = System.getProperty("user.dir");
|
|
 | Loading a Class or Object from a file in Java | 2012-04-17 11:31:30 |
To load a class or object from Java, first the class must be serializable,
you can then follow the following to load it.
FileInputStream saveFile;
try
{
fileHandle = new FileInputStream(filename);
ObjectInputStream data = new ObjectInputStream(fileHandle);
MyClass myclass = (MyClass) data.readObject();
return hist;
}
|
|
 | Save a Class or Object to file in Java | 2012-04-17 11:31:47 |
to save a serialized class to disk, follow the example below:
try
{
FileOutputStream fileHandle;
fileHandle = new FileOutputStream(<filename>);
ObjectOutputStream save = new ObjectOutputStream(fileHandle);
save.writeObject(<myClass>);
save.close();
} |
|
 | Javascript reflection, using reflection to list members and variables | 2012-05-16 11:08:45 |
In java script we can use reflection to list all the members and variables of a class,
The concept is the same as iterating through an array
for ( var methods in object )
{
var meth = methods;
var value = object[methods];
}
A complete example of listing methods and variables using reflection
<script>
bob = function()
{
this.a = 1;
this.b = 2;
this.meth = function(test)
{
var c=a+b;
}
}
var bb = new bob();
for ( var methods in bb )
{
document.write("name: "+methods+"<br>");
document.write("value: "+bb[methods]+"<br><br>");
}
</script>
the output from the above code
name: a
value: 1
name: b
value: 2
name: meth
value: function (test) { var c=a+b; }
|
|