表格单元格上组件的ActionEvent不会发生 [英] ActionEvent of a component on a table cell doesn't take place

查看:94
本文介绍了表格单元格上组件的ActionEvent不会发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码属于LWUIT应用程序,但是LWUIT和Java swing之间有一些共同点.

My code belongs to LWUIT apps but problem about something common between LWUIT and java swing.

我有一张桌子,在它的最后一个单元格上有一个Button作为组件

I have a table there is a Button set on the last cell of it as a component

我的问题很简单,为什么当我按下该按钮时为什么没有动作.

My question simply why is there no action takes place when I press that button.

我也尝试了复选框,但是我什至无法检查.

I tried checkbox also but I even couldn't do check.

Button b,b2;

Object  [][] elhind;

b2.addActionListener(new ActionListener()
{
 public void actionPerformed(ActionEvent e)

 {
 elhind  = new  String[1][9];
 String elhind  [][] = {{"Netherlands","Germany","Austria","Romania","Bulgaria","England","Walse","Ireland","Belgium"}};

 Object ob  [][] = new  String[elhind.length][10];

 for(int col=0;col<9;col++) 
{

 for(int j=0;j<elhind.length;j++)
 {

  ob[j][col] = elhind[j][col];

 }
  }
  TableModel  model = new DefaultTableModel( new String[]{"col1","col2","col3","col4","col5","col6","col7","col8","col9","col10","col11"ob)   {
  public boolean isCellEditable(int row, int col)
 {
 return true;  
 }
 };

 elhind  = new  String[1][10];
 ob = new  String[1][10];

 Table  table = new Table(model,true);

 for(int col=0;col<10;col++)
 for(int j=0;j<1;j++)

 try
 {

 if(col ==8)
 {
  Button cb =new Button("lam");
 cb.setFocus(true);
 cb.addActionListener( new ActionListener()
 {
public void actionPerformed(ActionEvent acv)
{

System.out.print("Action done");
}
 });

 table.addComponent(cb);

}
 else 
 {
  model.setValueAt(j, col, elhind[j++][col++] )   ;
 }
 }
catch(java.lang.ArrayIndexOutOfBoundsException ee)
{
}
catch(java.lang.NullPointerException e3)
{
}
 }

}

);

在表类上覆盖createCell方法不能解决问题

Overriding createCell method on table class doesn't solve the problem

Table  table = new Table(model,true)
{     
protected Component createCell(Object value, final int row,
final int column, boolean editable) {
if (column == 0) {
try {
Button cod = new Button("cod");
cod.getStyle().setBgColor(0x00f0f0);
cod.addActionListener(new ActionListener()
 {
public void actionPerformed(ActionEventacv)                          
{      
System.out.print("hello LWuit");
} 
});
return cod;
}  
catch (Exception ex)
{
ex.printStackTrace();
}
 }
 return super.createCell(value, row, column, editable);
 }
 } ;

推荐答案

您不应在表中添加按钮,也不应调用setFocus().

You shouldn't add buttons to a Table and you shouldn't invoke setFocus().

将按钮作为单独的组件添加到公共父级,或者重写表createCell方法以针对这种情况生成按钮(前者更容易).

Either add the button as a separate component to a common parent or override the tables createCell method to generate a button for that case (the former it easier).

使用requestFocus()代替setFocus().

Use requestFocus() instead of setFocus().

这篇关于表格单元格上组件的ActionEvent不会发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆