A simple form containing the table created using CustomItem is displayed. This custom created table will allow us to traverse through the cells. It also allow us to modify the value in the cell and get the value from the cell.We can get the row number and column number of the selected cell
Midlet Class
Custom Item Table class:
Midlet Class
package customItemPack; /* * This midlet will display a simple form containing the table created using CustomItem */ import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class CustomItemTable extends MIDlet implements CommandListener{ private Display disp; private TextField CusID; private TextField mpin,pwd; private Command cmd_ok,cmd_getRow,cmd_getColumn,cmd_setValue,cmd_getValue; String str=null; private Form FormTable; //Table1 t=new Table1("My TABLE",disp); Table1 t; int count=0,getRow,getColumn; public CustomItemTable() { FormTable=new Form("CUSTOMISED TABLE"); CusID=new TextField("CUSTOMER ID",null,6,TextField.NUMERIC); mpin=new TextField("MPIN",null,4,TextField.NUMERIC); pwd=new TextField("PASSWORD",null,6,TextField.NUMERIC); cmd_ok=new Command("OK",Command.OK,1); cmd_getRow=new Command("GET ROW",Command.ITEM,1); cmd_getColumn=new Command("GET COLUMN",Command.ITEM,1); cmd_setValue=new Command("SET VALUE",Command.ITEM,1); cmd_getValue=new Command("GET VALUE",Command.ITEM,1); FormTable.append(CusID); FormTable.append(mpin); FormTable.addCommand(cmd_ok); FormTable.addCommand(cmd_getRow); FormTable.addCommand(cmd_getColumn); FormTable.addCommand(cmd_setValue); FormTable.addCommand(cmd_getValue); FormTable.setCommandListener(this); disp=Display.getDisplay(this); t=new Table1("My TABLE",disp); t.disableTraverse(); t.setSelectedCell(0,2); t.initialiseTableValues(); FormTable.append(t); FormTable.append(pwd); } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { disp.setCurrent(FormTable); } public void commandAction(Command c, Displayable d) { if(c==cmd_ok) { } if(c==cmd_getRow) { int row=t.getSelectedRow(); System.out.println("ROW NUMBER IS :"+row); } if(c==cmd_getColumn) { int column=t.getSelectedColumn(); System.out.println("COLUMN NUMBER IS :"+column); } if(c==cmd_setValue) { t.setCellValue(2, 1, "SACHIN"); } if(c==cmd_getValue) { String returnValue=t.getValue(2,1); } } }
Custom Item Table class:
package customItemPack; /* * This class is used to create the table using CustomItem class. It will display the table with initial value highlighted in red * It will allow you to traverse the table cell on clicking Menu OK button. It will stop traversing the table cell again if you click the Menu OK button */ import javax.microedition.*; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CustomItem; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Item; public class Table1 extends CustomItem { private static final int UPPER = 0; private static final int IN = 1; private static final int LOWER = 2; private String[][]tableValues; private Display display; private int rows = 4; private int cols = 2; private int dx = 50; private int dy = 20; public int location = UPPER; private int currentX = 0; private int currentY = 0; private int a[]={0,50}; boolean change=false; int count =0; public static int rowDetector=0; public static int columnDetector=0; private boolean traverse=true; private int selected=0; public static int rowSelection; public static int columnSelection; boolean horz; private String cellValue; boolean vert; public Table1(String title, Display d) { super (title); display = d; int interactionMode = getInteractionModes(); tableValues=new String[rows][cols]; } protected int getMinContentHeight() { return (rows * dy) + 1; } protected int getMinContentWidth() { return (cols * dx) + 1; } protected int getPrefContentHeight(int width) { return (rows * dy) + 1; } protected int getPrefContentWidth(int height) { return (cols * dx) + 1; } /* * The paint() is used to draw the table and its contents * */ protected void paint(Graphics g, int w, int h) { System.out.println("STARTING TO PAINT"); if(selected==0) { /* * To draw the row lines */ for (int i = 0; i <= rows; i++) { g.drawLine(0, i * dy, cols * dx, i * dy); } /* * To draw the column lines */ for (int i = 0; i <= cols; i++) { g.drawLine(i * dx, 0, i * dx, rows * dy); } System.out.println("NOT SELECTED OPTION 0"); int oldColor = g.getColor(); // g.setColor(0x00D0D0D0); g.setColor(0x000000FF); g.fillRect((rowDetector * dx) + 1, (columnDetector * dy) + 1, dx - 1, dy - 1); if(change==false) { for(int x=0;x<a.length;x++) { for(int j=0;j<rows;j++) { g.setColor(0,0,0); /* * */ g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } } } if(change==true) { for(int x=0;x<a.length;x++) { for(int j=0;j<rows;j++) { if(x==rowSelection&&j==columnSelection){ g.setColor(0, 0, 0); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } else{ g.setColor(0, 0, 0); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } } } } g.setColor(oldColor); } if(selected==1){ for (int i = 0; i <= rows; i++) { g.drawLine(0, i * dy, cols * dx, i * dy); } for (int i = 0; i <= cols; i++) { g.drawLine(i * dx, 0, i * dx, rows * dy); } System.out.println("SELECTED OPTION"); int oldColor = g.getColor(); // g.setColor(0x00D0D0D0); g.setColor(0x00FF0000); g.fillRect((rowSelection * dx) + 1, (columnSelection * dy) + 1, dx - 1, dy - 1); if(change==false) { for(int x=0;x<a.length;x++) { for(int j=0;j<rows;j++) { g.setColor(0, 0, 0); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } } } if(change==true) { for(int x=0;x<a.length;x++) { for(int j=0;j<rows;j++) { if(x==rowSelection&&j==columnSelection){ g.setColor(0, 0, 0); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } else{ g.setColor(0, 0, 0); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } } } } g.setColor(oldColor); selected=2; } if(selected==2) { for (int i = 0; i <= rows; i++) { g.drawLine(0, i * dy, cols * dx, i * dy); } for (int i = 0; i <= cols; i++) { g.drawLine(i * dx, 0, i * dx, rows * dy); } System.out.println("NOT SELECTED OPTION 2"); int oldColor = g.getColor(); // g.setColor(0x00D0D0D0); g.setColor(0x000000FF); g.fillRect((0 * dx) + 1, (0 * dy) + 1, dx - 1, dy - 1); if(change==false) { for(int x=0;x<a.length;x++) { for(int j=0;j<rows;j++) { g.setColor(255, 255, 255); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } } } if(change==true) { for(int x=0;x<a.length;x++) { for(int j=0;j<rows;j++) { if(x==rowSelection&&j==columnSelection){ g.setColor(0, 0, 0); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } else{ g.setColor(0, 0, 0); g.drawString(tableValues[j][x], a[x]+2, j*dy+2, g.TOP|g.LEFT); } } } } g.setColor(oldColor); selected=0; } } /* * For Traversing the table cells * */ protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) { System.out.println("ENTERED TRAVERSE"); if (horz && vert) { switch (dir) { case Canvas.DOWN: if (location == UPPER) { location = IN; } else { if (columnDetector < (rows - 1)) { columnDetector++; repaint(rowDetector * dx, (columnDetector - 1) * dy, dx,dy); repaint(rowDetector * dx, columnDetector * dy, dx, dy); // rowDetector=currentX; // columnDetector=currentY; } else { location = LOWER; return false; } } break; case Canvas.UP: if (location == LOWER) { location = IN; } else { if (columnDetector > 0) { columnDetector--; repaint(rowDetector * dx, (columnDetector + 1) * dy, dx, dy); repaint(rowDetector * dx, columnDetector * dy, dx, dy); // rowDetector=currentX; // columnDetector=currentY; } else { location = UPPER; disableTraverse(); return false; } } break; case Canvas.LEFT: if (rowDetector > 0) { rowDetector--; repaint((rowDetector + 1) * dx, columnDetector * dy, dx, dy); repaint(rowDetector * dx, columnDetector * dy, dx, dy); // rowDetector=currentX; // columnDetector=currentY; } break; case Canvas.RIGHT: if (rowDetector < (cols - 1)) { rowDetector++; repaint((rowDetector - 1) * dx, columnDetector * dy, dx, dy); repaint(rowDetector * dx, columnDetector * dy, dx, dy); // rowDetector=currentX; // columnDetector=currentY; } } } else if (horz || vert) { switch (dir) { case Canvas.UP: case Canvas.LEFT: if (location == LOWER) { location = IN; } else { if (currentX > 0) { currentX--; repaint((currentX + 1) * dx, currentY * dy, dx,dy); repaint(currentX * dx, currentY * dy, dx, dy); rowDetector=currentX; columnDetector=currentY; System.out.println("PETERSON"+rowDetector); System.out.println("COLLINGWOOD"+columnDetector); } else if (currentY > 0) { currentY--; repaint(currentX * dx, (currentY + 1) * dy, dx,dy); currentX = cols - 1; repaint(currentX * dx, currentY * dy, dx, dy); rowDetector=currentX; columnDetector=currentY; System.out.println("PETERSON"+rowDetector); System.out.println("COLLINGWOOD"+columnDetector); } else { location = UPPER; return false; } } break; case Canvas.DOWN: case Canvas.RIGHT: if (location == UPPER) { location = IN; } else { if (currentX < (cols - 1)) { currentX++; repaint((currentX - 1) * dx, currentY * dy, dx,dy); repaint(currentX * dx, currentY * dy, dx, dy); } else if (currentY < (rows - 1)) { currentY++; repaint(currentX * dx, (currentY - 1) * dy, dx,dy); currentX = 0; repaint(currentX * dx, currentY * dy, dx, dy); rowDetector=currentX; columnDetector=currentY; System.out.println("PETERSON"+rowDetector); System.out.println("COLLINGWOOD"+columnDetector); } else { location = LOWER; return false; } } } } visRect_inout[0] = rowDetector; visRect_inout[1] = columnDetector; visRect_inout[2] = dx; visRect_inout[3] = dy; return true; } public void commandAction(Command c, Item i) { } public void enableTraverse() { horz =true; vert = true; } public void disableTraverse() { horz =false; vert = false; } protected void keyPressed(int key) { System.out.println("key pressed ...."+key); switch(key){ case -5: // center if(count==0) { enableTraverse(); count =1; }else if(count==1){ disableTraverse(); count=0; } System.out.println("ONE"); break; case -6: // left soft key System.out.println("TWO"); break; case -7: // right soft key System.out.println("THREE"); break; case -1: // left arrow System.out.println("FOUR"); break; case -2://right System.out.println("FOUR"); break; case -3://up System.out.println("FOUR"); break; case -4://down System.out.println("FOUR"); break; case 51: System.out.println("FOUR"); break; } } public int getSelectedRow() { return columnDetector+1; } public int getSelectedColumn() { return rowDetector+1; } public void setSelectedCell(int row,int column) { selected=1; rowSelection=row; columnSelection=column; System.out.println("HUSSEY"); repaint(rowDetector*dx,columnDetector*dy,dx,dy); repaint(row*dx,column*dy,dx,dy); rowDetector=row; columnDetector=column; // repaint(rowDetector*dx,columnDetector*dy,dx,dy); } /* * To set the Value in a table cell */ public void setCellValue(int row,int column,String value) { rowSelection=row; columnSelection=column; tableValues[rowSelection][columnSelection]=value; repaint(rowDetector*dx,columnDetector*dy,dx,dy); repaint(row*dx,column*dy,dx,dy); change=true; } public void initialiseTableValues() { for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { tableValues[i][j]="MyValue"; } } } /* * To get the Value in a table cell */ public String getValue(int row,int column) { // String returnValue=""; return tableValues[row][column]; } }