请问,如何获取tableitem表格中当前选定的行列坐标?

chengyumao 2008-08-02 01:09:57
如何获取tableitem表格中当前选定的行列坐标? 我现在的tableitem是绑定在一个table上的....然后发现没办法获得当前光标停在哪一格上......我是小白....请各位多多指教啊....
...全文
202 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
x52j0_xiaoyaoyou 2008-08-02
学习~~~
回复
Harvey_He 2008-08-02
下面是一个简单的操作添加类.
package Harvey He.table;

import javax.microedition.lcdui.*;


public class TextInput extends TextBox
implements CommandListener
{

private static final Command cmd_ok = new Command("OK", 4, 1);
private static final Command cme_cancle = new Command("cancel", 3, 1);
private Table parent;
private Display display;

public TextInput(String text, Table parent, Display display)
{
super("Enter Text", text, 50, 0);
this.parent = parent;
this.display = display;
addCommand(cmd_ok);
addCommand(cme_cancle);
setCommandListener(this);
}

public void commandAction(Command c, Displayable d)
{
if(c == cmd_ok)
{
parent.setText(getString());
display.setCurrentItem(parent);
} else
if(c == cme_cancle)
display.setCurrentItem(parent);
}

}
回复
Harvey_He 2008-08-02
protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int visRect_inout[])
{
if(horz && vert)
switch(dir)
{
case 6: // '\006'
if(location == 0)
location = 1;
else
if(currentY < rows - 1)
{
currentY++;
repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
} else
{
location = 2;
return false;
}
break;

case 1: // '\001'
if(location == 2)
location = 1;
else
if(currentY > 0)
{
currentY--;
repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
} else
{
location = 0;
return false;
}
break;

case 2: // '\002'
if(currentX > 0)
{
currentX--;
repaint((currentX + 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}
break;

case 5: // '\005'
if(currentX < cols - 1)
{
currentX++;
repaint((currentX - 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}
break;
}
else
if(horz || vert)
switch(dir)
{
case 3: // '\003'
case 4: // '\004'
default:
break;

case 1: // '\001'
case 2: // '\002'
if(location == 2)
{
location = 1;
break;
}
if(currentX > 0)
{
currentX--;
repaint((currentX + 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
break;
}
if(currentY > 0)
{
currentY--;
repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
currentX = cols - 1;
repaint(currentX * dx, currentY * dy, dx, dy);
} else
{
location = 0;
return false;
}
break;

case 5: // '\005'
case 6: // '\006'
if(location == 0)
{
location = 1;
break;
}
if(currentX < cols - 1)
{
currentX++;
repaint((currentX - 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
break;
}
if(currentY < rows - 1)
{
currentY++;
repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
currentX = 0;
repaint(currentX * dx, currentY * dy, dx, dy);
} else
{
location = 2;
return false;
}
break;
}
visRect_inout[0] = currentX;
visRect_inout[1] = currentY;
visRect_inout[2] = dx;
visRect_inout[3] = dy;
return true;
}

public void setText(String text)
{
data[currentY][currentX] = text;
repaint(currentY * dx, currentX * dy, dx, dy);
}

public void setText(int x, int y, String text)
{
data[x][y] = text;
}

public void commandAction(Command c, Item i)
{
if(c == CMD_EDIT)
{
TextInput textInput = new TextInput(data[currentY][currentX], this, display);
display.setCurrent(textInput);
}
}

public int getCurrentX()
{
return currentX;
}

public int getCurrentY()
{
return currentY;
}

public String[][] getData()
{
return data;
}

public String getData(int x, int y)
{
return data[x][y];
}

public void setDX(int dx)
{
this.dx = dx;
}

public void setDY(int dy)
{
this.dy = dy;
}

}
回复
Harvey_He 2008-08-02
看在小白的份上,就帮你一下.
J2ME中是没有table类和接口的,要想使用必须自己定义一个table类,竟高人指点我这里正好有个,里面有很多操作.拿去吧:

package Harvey He.table;
import javax.microedition.lcdui.*;
public class Table extends CustomItem
implements ItemCommandListener
{

private static final Command CMD_EDIT = new Command("Edit", 8, 1);
private Display display;
private int rows;
private int cols;
private int dx;
private int dy;
private static final int UPPER = 0;
private static final int IN = 1;
private static final int LOWER = 2;
private int location;
private int currentX;
private int currentY;
private String data[][];
boolean horz;
boolean vert;

public Table(String title, Display d)
{
super(title);
rows = 6;
cols = 7;
dx = 33;
dy = 20;
location = 0;
currentX = 0;
currentY = 0;
display = d;
setDefaultCommand(CMD_EDIT);
setItemCommandListener(this);
int interactionMode = getInteractionModes();
horz = (interactionMode & 0x1) != 0;
vert = (interactionMode & 0x2) != 0;
data = new String[rows][cols];
}

public Table(String title, Display d, boolean flag)
{
super(title);
rows = 6;
cols = 7;
dx = 33;
dy = 20;
location = 0;
currentX = 0;
currentY = 0;
display = d;
if(flag)
{
setDefaultCommand(CMD_EDIT);
setItemCommandListener(this);
}
int interactionMode = getInteractionModes();
horz = (interactionMode & 0x1) != 0;
vert = (interactionMode & 0x2) != 0;
data = new String[rows][cols];
}

public Table(String title, int r, int c, Display d)
{
this(title, d);
rows = r;
cols = c;
}

public Table(String title, int r, int c, Display d, boolean flag)
{
this(title, d, flag);
rows = r;
cols = c;
}

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;
}

protected void paint(Graphics g, int w, int h)
{
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);

int oldColor = g.getColor();
g.setColor(0xd0d0d0);
g.fillRect(currentX * dx + 1, currentY * dy + 1, dx - 1, dy - 1);
g.setColor(oldColor);
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
if(data[i][j] != null)
{
int oldClipX = g.getClipX();
int oldClipY = g.getClipY();
int oldClipWidth = g.getClipWidth();
int oldClipHeight = g.getClipHeight();
g.setClip(j * dx + 1, i * dy, dx - 1, dy - 1);
g.drawString(data[i][j], j * dx + 2, (i + 1) * dy - 2, 36);
g.setClip(oldClipX, oldClipY, oldClipWidth, oldClipHeight);
}

}

}



回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2008-08-02 01:09
社区公告
暂无公告