0

我的代码属于 LWUIT 应用程序,但是关于 LWUIT 和 java swing 之间的一些常见问题。

我有一个表格,在它的最后一个单元格上设置了一个按钮作为组件

我的问题很简单,为什么当我按下那个按钮时没有发生任何动作。

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

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 方法并不能解决问题

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

1 回答 1

1

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

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).

Use requestFocus() instead of setFocus().

于 2013-12-25T10:37:08.657 回答