×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    85

    Key Press event in Touch Screen mobiles in J2ME MIDlet

    Hi all,

    I developed a application that works well with keypad mobiles, that application handles key press events.
    But the same application does works well with touch screen mobile (Nokia 5230). I posted the code

    Code:
          public class TableCanvas extends Canvas
           {
    	ScrollableTable table = null;
            HelloMIDlet iHelloMidlet=null;
            String screenName=null;
    	public TableCanvas(String[][] ar,HelloMIDlet aHelloMidlet,String aScreenName)
    	{
                    iHelloMidlet=aHelloMidlet;
                    screenName=aScreenName;
    		this.table = new ScrollableTable(ar, getWidth()-20, getHeight() - 20);
    	}
    	
    	protected void keyPressed(int key)
    	{
    		int keyCode = getGameAction(key);
    
                    if(screenName.compareTo("OrderEntry")==0){
                        if(keyCode == FIRE)
                        {
                            if(table.getSelectedRow()!=-1)
                               iHelloMidlet.DisplayOrderEntryScreen(table.getSelectedRow(),false);
                        }
                        else if(keyCode==0){
                            iHelloMidlet.ShowSearchForm();
                        }
                        else
                        {
                                table.keyPressed(keyCode);
                                repaint();
                        }
                    }
                    else if(screenName.compareTo("Order")==0){
                         table.keyPressed(keyCode);
                         repaint();
                    }
    	}
    
    	protected void paint(Graphics g)
    	{
    		g.setColor(0xffffff);
                    
    		g.fillRect(0, 0, getWidth(), getHeight());
    
    		g.translate(10, 10);
    
    		table.paint(g);
    
    		g.translate(-10, -10);
    	}
            
            public int getSelectedRow()
    	{
               return table.getSelectedRow();
    	}
    }
    
    
    public class ScrollableTable {
    public int currentCol = 0;
    	public int currentRow = 0;
    
    	static final String VOID_STRING = "";
    	static final char SPACE_CHAR = ' ';
    
    	int rows = 0;
    	int cols = 0;
    	int[] colWidths = null;
    	int[] rowHeights = null;
    	int[] colLeft = null;
    	int[] rowTop = null;
    
    	String[][] data = null;
    	String[][][] dataRows = null;
    
    	public static final int SCROLL_STEP = 25;
    	int viewportWidth = 0;
    	int viewportHeight = 0;
    	int viewportX = 0;
    	int viewportY = 0;
    	int width = 0;
    	int height = 0;
    
    	Font font = Font.getDefaultFont();
    
    	int bgColor = 0xffffff;
    	int bgFocusedColor = 0xff0000;
    	int foreColor = 0x000000;
    	int borderWidth = 1;
    	int borderColor = 0x000000;
    	int interline = 2;
    	int padding = 2;
    
    	public ScrollableTable(String[][] data, int width, int height)
    	{
    		this.viewportWidth = width;
    		this.viewportHeight = height;
    
    		setData(data);
    	}
    	public void setData(String[][] data)
    	{
    		this.data = data;
    
    		this.rows = this.data.length;
    		this.cols = this.data[0].length;
    
    		initialize();
    	}
    	void initialize()
    	{
    		this.rowTop = new int[rows];
    		this.colLeft = new int[cols];
    
    		this.colWidths = new int[cols];
    		this.dataRows = new String[rows][][];
    
    		int mediumWidth = (viewportWidth - cols * (2 * padding + borderWidth) - borderWidth) / cols;
    		int extraWidth = 0;
    		int overflowCols = 0;
    
    		for(int i = 0; i < cols; i++)
    		{
    			int colWidth = 0;
    
    			for(int j = 0; j < rows; j++)
    			{
    				colWidth = Math.max(
    					colWidth,
    					font.stringWidth(data[j][i])
    
    				);
    			}
    			if(colWidth < mediumWidth)
    			{
    				colWidths[i] = colWidth;
    
    				extraWidth += (mediumWidth - colWidth);
    			}
    			else
    			{
    				colWidths[i] = mediumWidth;
    
    				overflowCols++;
    			}
    		}
    
    		for(int i = 0; i < cols; i++)
    		{
    			if(overflowCols > 0 && extraWidth > 0 && colWidths[i] == mediumWidth)
    			{
    				colWidths[i] += (extraWidth / overflowCols);
    			}
    			colLeft[i] = width;
    			colWidths[i] += + 2 * padding;
    
    			width += colWidths[i] + borderWidth;
    		}
    		width += borderWidth;
    
    		rowHeights = new int[rows];
    
    		for(int i = 0; i < rows; i++)
    		{
    			this.dataRows[i] = new String[cols][];
    
    			for(int j = 0; j < cols; j++)
    			{
    				this.dataRows[i][j] = getTextRows(this.data[i][j], font, colWidths[j]);
    
    				rowHeights[i] = Math.max(
    					rowHeights[i],
    					dataRows[i][j].length * (font.getHeight() + interline) - (dataRows[i][j].length > 0 ? interline : 0)
    				);
    			}
    			rowHeights[i] += 2 * padding;
    			rowTop[i] = height;
    
    			height += rowHeights[i] + borderWidth;
    		}
    
    		height += borderWidth;
    	}
    
    	public void paint(Graphics g)
    	{
    		g.setClip(0, 0, viewportWidth, viewportHeight);
    
                    g.translate(- viewportX, - viewportY);
    
    		g.setColor(bgColor);
    		g.fillRect(0, 0, width, height);
    
    		int currentX = 0;
    		int currentY = 0;
    
    		for(int i = 0; i < rows; i++)
    		{
    			currentX = 0;
    
    			g.setColor(borderColor);
    
    			g.fillRect(0, currentY, width, borderWidth);
    
    			currentY += borderWidth;
    
    			for(int j = 0; j < cols; j++)
    			{
    				if(i == 0)
    				{
    					g.setColor(borderColor);
    
    					g.fillRect(currentX, 0, borderWidth, height);
    				}
    				if(j == currentCol && i == currentRow)
    				{
    					g.setColor(bgFocusedColor);
    
    					g.fillRect(borderWidth + currentX, currentY, colWidths[j], rowHeights[i]);
    
    					g.setColor(foreColor);
    				}
    
    				currentX += borderWidth;
    
    				g.setColor(foreColor);
    
    				for(int k = 0; k < dataRows[i][j].length; k++)
    				{
    					g.drawString(dataRows[i][j][k], padding + currentX, padding + currentY + k * (font.getHeight() + interline), Graphics.TOP | Graphics.LEFT);
    				}
    				currentX += colWidths[j];
    			}
    			if(i == 0)
    			{
    				g.setColor(borderColor);
    
    				g.fillRect(currentX, 0, borderWidth, height);
    			}
    
    			currentY += rowHeights[i];
    		}
    		g.setColor(borderColor);
    
    		g.fillRect(0, currentY, width, borderWidth);
    
    		g.translate(viewportX, viewportY);
    
    	}
    	public String getSelectedData()
    	{
                    if(currentRow!=0)
                        return data[currentRow][0];
                    else
                        return null;
    	}
    
            public int getSelectedRow()
    	{
                    if(currentRow!=0)
                        return currentRow;
                    else
                        return -1;
    	}
    
    	public void keyPressed(int keyCode)
    	{
    		switch(keyCode)
    		{
    		case Canvas.UP:
    			move(0, -1);
    			break;
    		case Canvas.DOWN:
    			move(0, 1);
    			break;
    		case Canvas.RIGHT:
    			move(1, 0);
    			break;
    		case Canvas.LEFT:
    			move(-1, 0);
    			break;
    		}
    	}
    	boolean scroll(int colDelta, int rowDelta)
    	{
    		boolean scrolled = false;
    
    		if(colDelta > 0 && colLeft[currentCol] + colWidths[currentCol] > viewportX + viewportWidth)
    		{
    			viewportX += SCROLL_STEP;
    
    			scrolled = true;
    		}
    		else if(colDelta < 0 && colLeft[currentCol] < viewportX)
    		{
    			viewportX -= SCROLL_STEP;
    
    			scrolled = true;
    		}
    		if(rowDelta > 0 && rowTop[currentRow] + rowHeights[currentRow] > viewportY + viewportHeight)
    		{
    			viewportY += SCROLL_STEP;
    
    			scrolled = true;
    		}
    		else if(rowDelta < 0 && rowTop[currentRow] < viewportY)
    		{
    			viewportY -= SCROLL_STEP;
    
    			scrolled = true;
    		}
    		if(scrolled)
    		{
    			viewportX = Math.max(0, Math.min(viewportX, width - viewportWidth));
    			viewportY = Math.max(0, Math.min(viewportY, height - viewportHeight));
    		}
    		return scrolled;
    	}
    	void move(int colDelta, int rowDelta)
    	{
    		if(!scroll(colDelta, rowDelta))
    		{
    			int newCol = currentCol + colDelta;
    			int newRow = currentRow + rowDelta;
    
    			if(newCol >= 0 && newRow >= 0 && newRow < rows && newCol < cols)
    			{
    				currentCol = newCol;
    				currentRow = newRow;
    
    				scroll(colDelta, rowDelta);
    			}
    		}
    	}
    
    	static String[] getTextRows(String text, Font font, int width)
    	{
    		int prevIndex = 0;
    		int currIndex = text.indexOf(SPACE_CHAR);
    
    		Vector rowsVector = new Vector();
    
                    StringBuffer stringBuffer = new StringBuffer();
    
                    String currentToken;
    
                    String currentRowText = VOID_STRING;
    
                while(prevIndex != -1)
                {
                        int startCharIndex = prevIndex == 0 ? prevIndex : prevIndex + 1;
    
                        if(currIndex != -1)
                                currentToken = text.substring(startCharIndex, currIndex);
                        else
                                currentToken = text.substring(startCharIndex);
    
                        prevIndex = currIndex;
    
                        currIndex = text.indexOf(SPACE_CHAR, prevIndex + 1);
    
                        if(currentToken.length() == 0)
                        {
                                continue;
                        }
    
                        if(stringBuffer.length() > 0)
                                stringBuffer.append(SPACE_CHAR);
    
                        stringBuffer.append(currentToken);
    
                    if(font.stringWidth(stringBuffer.toString()) > width)
                    {
                        if(currentRowText.length() > 0)
                        {
                                rowsVector.addElement(currentRowText);
                        }
                        stringBuffer.setLength(0);
    
                        currentRowText = VOID_STRING;
    
                        stringBuffer.append(currentToken);
    
                        currentRowText = stringBuffer.toString();
                    }
                    else
                    {
                        currentRowText = stringBuffer.toString();
                    }
                }
    
                    if(currentRowText.length() > 0)
                    {
                        rowsVector.addElement(currentRowText);
                    }
    
                String[] rowsArray = new String[rowsVector.size()];
                rowsVector.copyInto(rowsArray);
                return rowsArray;
            }
    }
    What is the solution for this problem?

    Thanks in advance.

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: Key Press event in Touch Screen mobiles in J2ME MIDlet

    Hello s.abulkalamazad,

    According to the documentation:

    Note: FIRE key in on-screen keypad behaves mostly the same way as Selection key in physical keypad. Thus if there is no on-screen keypad defined for a full screen mode Canvas application, then there are no softkeys available for mapping possible application commands. Also Canvas (normal mode and full screen mode with Commands and CommandListener defined) does not get the low-level key event from Selection key unless this is explicitly enabled. For enabling low-level key event triggering from Selection key, see Canvas.

    Source:

    Java Developer's Library 3.11 > Developer's guides > UI and graphics > Touch UI > Touch interaction in Symbian > On-screen keypad (OSK) > OSK on devices with no keyboard

    http://library.developer.nokia.com/i...3F891DA52.html

    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

Similar Threads

  1. J2ME MIDLET attribute full touch screen
    By nyerunkar in forum Mobile Java General
    Replies: 13
    Last Post: 2012-08-03, 19:33
  2. How to put the sigature in touch Screen Mobile(5230) using j2me midlet
    By chandrasekaranbe in forum Mobile Java General
    Replies: 4
    Last Post: 2011-05-23, 11:02
  3. How to invode touch functionality in touch screen mobiles
    By j2me_beginner in forum Mobile Java General
    Replies: 0
    Last Post: 2009-09-30, 14:04
  4. Image click Event in mobiles without touch screen
    By swarna.nokia in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2009-06-16, 14:50

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×