Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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.

Hello World


public class HelloWorld {

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

}