In Java, many of the swings applications by default opens the window at the top left corner of the screen. In addition to setting the size and making it visible, we also need to set the coordinates where the new window has to be displayed. To show the window in the center of the given screen/monitor, I have written an example explained below.
This example automatically takes the current screen resolution and depending on the size (of the window) provided, it adjusts the start point of the screen.
Thursday, October 4, 2012
How to display Window in the center of the screen
Friday, June 24, 2011
Parsing Multiple XML Tags
Multiple XML Tags |
<?xml version="1.0" encoding="UTF-8"?> <rootTag Name="Root"> <ChildOne activityType="Task"> <Name>P1</Name> <Incoming>0</Incoming> <Outgoing>P1P2</Outgoing> </ChildOne> <ChildOne activityType="Task"> <Name>P2</Name> <Incoming>P1P2</Incoming> <Outgoing>P2G1</Outgoing> </ChildOne> <ChildOne activityType="GatewayParallel"> <Name>G1</Name> <Incoming>P2G1</Incoming> <Outgoing>G1P3 G1P4</Outgoing> </ChildOne> <ChildTwo type="bpmn:SequenceEdge"> <Name>P1P2</Name> <Source>P1</Source> <Destination>P2</Destination> <Path>P1P2</Path> </ChildTwo> <ChildTwo type="bpmn:SequenceEdge"> <Name>P2G1</Name> <Source>P2</Source> <Destination>G1</Destination> <Path></Path> </ChildTwo> <ChildTwo type="bpmn:SequenceEdge"> <Name>G1P3</Name> <Source>G1</Source> <Destination>P3</Destination> <Path>P2P3</Path> </ChildTwo> </rootTag>
Monday, March 7, 2011
Difference in two Calendar Dates
There are many ways to calculate the difference between two Calendar dates in Year, Months, weeks or days by simply dividing them with a fixed constant. DateUtil.java below has a method for getting the difference in Days. this method can similarly be modified to get in Weeks, Months or Years.
But some times the challenge is to display the difference in two java.util.Calendar or java.util.Date in more user friendly manner. Like in format X Years, X months, X weeks, X days. Where X is positive number greater than zero.
Example: If the number of Full Years < 1, the format should be X months, X weeks, X days. If the number of Full Years >= 1 but the number of Full Months < 1, the format should be X Years, X weeks, X days and so on. Values of 0 is not written. Like "2 months, 2 days" rather than "2 months, 0 weeks, 2 days"
Thursday, February 3, 2011
How to read file without locking in Java
In java, we can read or write to a file without acquiring locks to it. These are mostly used for log files. Recently I was tying to mimic the tail command (of Linux) in java and wrote a simple program below.
Using java.io.RandomAccesFile we can read/write to a file without obtaining locks. Let me demonstrate with example step by step how it works.
RandomAccessFile accessFile = new RandomAccessFile(file, "r");
Create an instance of the RandomAccessFile by either passing java.io.File or a path of a file. The second constructor argument is the type of access needed. In this case it is read only "r". If you want to make it read/write pass "rw". Like Buffers of java.nio package, this class also works with the pointers. it reads the bytes and move the pointer as it progresses. The current pointer gives the information about how much of the file have bean read or has to be read.
public void print(OutputStream stream) throws IOException {
long length = accessFile.length();
while (accessFile.getFilePointer() < length) {
stream.write(accessFile.read());
}
stream.flush();
}
Monday, August 23, 2010
Parsing complex nested XML with SAXParser and collections
Friday, August 20, 2010
XML parsing with SAX Parser Demo
Using Sax parser is simple. All we need to do is create a Handler class which extends DefaultHandler class of org.xml.sax.helpers package (part of JDK). In this Handler class, we define how tags are processed and stored into in memory object.
Once the Handler is developed, Create a Parser class which uses SAXParser abstract class from javax.xml.parsers package (also part of JDK). Use this Parser class and parse the document with help of the Handler class which we created. Current example demonstrate simple XML, for parsing nested XML with hierarchy, read a another article as how to parse nested XML.
Below is a snippet of simple XML file which needs to be parsed.