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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | 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]; } } |