Two Columned Layout for standart forms

I have implemented a layout for panels with two columns. It calculates the width of the first column according to the widest component of first column. And, uses remained space for second column components. Besides, each row height is calculated according to each row's two inner components' heights. It may be useful for some cases.

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;

public class TwoColumnedLayout implements LayoutManager {
private int horizontalGap = 0;
private int verticalGap = 0;

public TwoColumnedLayout() {
}

public TwoColumnedLayout(int horizontalGap, int verticalGap) {
this.horizontalGap = horizontalGap;
this.verticalGap = verticalGap;
}

@Override
public Dimension preferredLayoutSize(Container parent) {
int count = parent.getComponentCount();
if (count == 0) {
return minimumLayoutSize(parent);
}
int totalHeight = 0;
int oddComponentHeight = 0;
int maxOddWidth = 1;
int maxEvenWidth = 1;
for (int i = 0; i < count; ++i) {
Component subComponent = parent.getComponent(i);
Dimension size = subComponent.getPreferredSize();
if (i % 2 == 0) {
maxOddWidth = Math.max(maxOddWidth, size.width);
oddComponentHeight = size.height;
if (i > 0) {
totalHeight += verticalGap;
}
}
else {
maxEvenWidth = Math.max(maxEvenWidth, size.width);
int maxHeight = Math.max(oddComponentHeight, size.height);
totalHeight += maxHeight;
oddComponentHeight = 0;
}
}
totalHeight += oddComponentHeight;
return new Dimension(maxOddWidth + horizontalGap + maxEvenWidth, totalHeight);
}

@Override
public void layoutContainer(Container parent) {
int count = parent.getComponentCount();
if (count == 0) {
return;
}
Dimension parentSize = parent.getSize();
int parentWidth = parentSize.width;
int oddWidth = 1;
for (int i = 0; i < count; ++i) {
Component subComponent = parent.getComponent(i);
Dimension componentSize = subComponent.getPreferredSize();
if (i % 2 == 0) {
oddWidth = Math.max(oddWidth, componentSize.width);
}
}
int evenWidth = parentWidth - oddWidth - horizontalGap;
if (evenWidth < 0) {
evenWidth = 1;
}
int y = 0;
int oddHeight = 0;
for (int i = 0; i < count; ++i) {
Component subComponent = parent.getComponent(i);
Dimension componentSize = subComponent.getPreferredSize();
int componentHeight = componentSize.height;
if (i % 2 == 0) {
subComponent.setSize(oddWidth, componentHeight);
subComponent.setLocation(0, y);
}
else {
subComponent.setSize(evenWidth, componentHeight);
subComponent.setLocation(oddWidth + horizontalGap, y);
int rowHeight = Math.max(oddHeight, componentHeight);
y += rowHeight + verticalGap;
}
}
}

@Override
public Dimension minimumLayoutSize(Container parent) {
return new Dimension(1, 1);
}

@Override
public void addLayoutComponent(String name, Component comp) {
}

@Override
public void removeLayoutComponent(Component comp) {
}
}

Setting focus traversal keys for JTextArea

It's unable to use the "Tab" key for JTextArea to focus next swing component. The next code fixes this problem, and makes the Tab and Shift-Tab keys usable to focus forward or backward.
JTextArea textArea = new JTextArea();
textArea.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,null);
textArea.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,null);

How to read Excel sheet in Java?

An example of reading and parsing an Excel file.


We have a class (HomeObject) and this class has two attributes ('doorNo' and 'ownerName').

public class HomeObject {
private int doorNo;
private String ownerName;

public int getDoorNo() {
return doorNo;
}
public void setDoorNo(int doorNo) {
this.doorNo = doorNo;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
}
Now we want to read data from Excel sheet columns and add into a list in our ExcelParser class.
For this purpose, we use The Apache POI project library.

First of all, we add this code into import part of your ExcelParser class.

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
We need a function that controls data compability in sheet columns.

private String getCellValue(HSSFCell cell) {
if (cell == null) return null;
String result = null;
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_BLANK:
result = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue() ? "true" : "false";
break;
case HSSFCell.CELL_TYPE_ERROR:
result = "ERROR: " + cell.getErrorCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result = String.valueOf((long) cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
result = cell.toString();
break;
default:
break;
}
return result;
}
and we add that code into main function in ExcelParser class.
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
// we assume we have data only in the first sheet.
HSSFSheet sheet = workbook.getSheetAt(0);
List homeObjects = new LinkedList();
HomeObject homeObject;
for (Iterator rows = sheet.rowIterator(); rows.hasNext();) {
HSSFRow row = (HSSFRow) rows.next();
// we know that the first line is for column names. we won't use it
if (row.getRowNum() == 0) continue;
homeObject = new HomeObject();
homeObject.setDoorNo(getCellValue(row.getCell((short) 0)));
homeObject.setOwnerName(getCellValue(row.getCell((short) 1)));
homeObjects.add(homeObject);
}
} catch (IOException e) {
// todo : error handling
}
Now, we have a list contains column data as HomeObject objects to use.

What is agile?

I decided to write about Agile development and Agile practices days ago. But I coudn't have time to write. At last I've decided to write all as a series of short blog-posts.

In this post, I'll try to explain "what is agile?".


Agile term used to mean for thinking and reacting very quickly. In software industry, it has more meaning. Agile refers to a group of software development practices which let the software project adopt to change quickly. But Agile is not a method by itself. It's an umbrealla term for some iterative and incremental methodologies such as Scrum, XP (Extreme Programming), Crystal Clear, and DSDM.

In 2001, 17 agile evangelists met to discuss the need for lighter alternatives to the traditional methodologies and declared Agile manifesto. This manifesto defined four values and twelve principles for agile software development. The values are:
  • Individuals and interactions over processes and tools
  • Working software over comprehensive documentation
  • Customer collaboration over contract negotiation
  • Responding to change over following a plan
and 12 principles:
  1. Customer satisfaction through early and continuous delivery of valuable software
  2. Welcome changing requirements, even late.
  3. Deliver working software frequently (iterations)
  4. Daily communication between business people and development team
  5. Projects around motivated individuals
  6. Face-to-face conversation to convey information within development team.
  7. The primary measure of progress is working software
  8. Sustainable development pace
  9. Technical excellence and good design
  10. Simplicity
  11. Self-organizing teams
  12. Reflection on how to become more effective at regular intervals
These priorities and principles are still the basis of agile. But I think Agile is more than these things. I mean Agile methods. I'll write about all step by step. :)

Hello World


public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello, World");
}

}