Friday, April 2, 2010

J2ME Selectable Table in NetBeans

// ONLY WORKS FOR SELECTING ENTIRE ROWS!!!
// This is a h@ck. Use at your own risk. -Tres

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import org.netbeans.microedition.lcdui.TableItem;
import org.netbeans.microedition.lcdui.TableModel;
import org.netbeans.microedition.lcdui.laf.ColorSchema;

/**
*
* @author A. Tres Finocchiaro
*/
public class SkidletTableItem extends TableItem {

protected double rowBias = -1;
protected double rowCount = -1;

public SkidletTableItem(Display display, String title) {
super(display, title);
}

public SkidletTableItem(Display display, String title, TableModel model) {
super(display, title, model);
}

public SkidletTableItem(Display display, String title, TableModel model, ColorSchema colorSchema) {
super(display, title, model, colorSchema);
}

public void setModel(TableModel model) throws IllegalArgumentException {
rowBias = -1;
rowCount = -1;
super.setModel(model);
}

public void tableModelChanged(TableModel changedModel) {
rowBias = -1;
rowCount = -1;
super.tableModelChanged(changedModel);
}



protected void pointerPressed(int x, int y) {
selectRow((int) calculateRow(y));
super.pointerPressed(x, y);
}

protected void selectRow(int row) {
// Do not select a row
if (row == getSelectedCellRow() || row > rowCount - 1 || row <> getSelectedCellRow()) {
traverse(Canvas.DOWN, 0, 0, new int[]{0, 0, 0, 0});
}
}

protected double calculateRow(double y) {
// Accomodate for titles and labels
if (rowBias == -1) {
rowBias = 0;
if (this.getTitle() != null) {
rowBias++;
}
if (this.getLabel() != null) {
rowBias++;
}
}

int row = -1;
while (rowCount == -1) {
traverse(Canvas.DOWN, 0, 0, new int[]{0, 0, 0, 0});
if (row == getSelectedCellRow()) {
rowCount = getSelectedCellRow() + 1;
}
row = getSelectedCellRow();
}

double h = getMinContentHeight();
double retVal = ((rowCount + rowBias) * y) / h;
return retVal + 0.25 - rowBias; // Round up slightly
}
}
 

Selecting a row in-action (Windows Mobile 6 Emulator)





























Implementation in NetBeans (click picture to enlarge):



16 comments:

a said...

hello
i'm trying to use org.netbeans.microedition.lcdui package.but my netbeans 6.8 says the package do not exist.
can u plz help....

FatButtLarry said...

You need to install the Mobility stuff from the plugins.

Search "Mobility" from available Tools, Plugins and install both.

You may also need to download and install the J2ME JDK from here:
http://java.sun.com/javame/downloads/sdk30.jsp

After installed, you can register it with Netbeans by going to Tools, Java Platforms, Add, and select the folder that it is installed in.

-Tres

Mr Lonely said...
This comment has been removed by a blog administrator.
韻玉 said...
This comment has been removed by a blog administrator.
童紫勳 said...
This comment has been removed by a blog administrator.
GeraldF_Rotter雅慧 said...
This comment has been removed by a blog administrator.
雲亨 said...
This comment has been removed by a blog administrator.
c0527onniemcgehee said...
This comment has been removed by a blog administrator.
香廷 said...
This comment has been removed by a blog administrator.
韋于倫成 said...
This comment has been removed by a blog administrator.
517SeritaG_Jamar0 said...
This comment has been removed by a blog administrator.
HaroldM22 said...
This comment has been removed by a blog administrator.
07_TeddyF_Silvey0 said...
This comment has been removed by a blog administrator.
huntb said...
This comment has been removed by a blog administrator.
王周宏儒 said...
This comment has been removed by a blog administrator.
vaza santosh said...

Hi, Thanks for help
can you please tell me how to format TableItem component for example adding background image etc...