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):
Comments
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....
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
can you please tell me how to format TableItem component for example adding background image etc...