2013-12-11 20 views
0

我的代碼屬於LWUIT應用程序,但有關LWUIT和java swing之間常見的問題。表格單元上的組件的ActionEvent不會發生

我有一個表有一個按鈕,在它的最後一個單元格設置爲一個組件

我的問題只是爲什麼沒有行動發生時,我按下該按鈕。

我試過複選框,但我甚至無法檢查。

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); 
} 
} ; 
+1

請正確格式化您的代碼 –

+0

您需要在'JButton'中使用自定義'CellEditor',看一下[Table Button Column](http://tips4java.wordpress.com/2009/07/12/table-按鈕列/) – alex2410

+0

請你能爲我解釋這段代碼,getActionCommand()不能用在LWUIT應用程序中public void actionPerformed(ActionEvent e) JTable table =(JTable)e.getSource(); int modelRow = Integer.valueOf(e.getActionCommand()); ((DefaultTableModel)table.getModel())。removeRow(modelRow); } – JavaFan

回答

1

不要添加按鈕,一個表,你不應該調用setFocus()

將按鈕作爲單獨的組件添加到公共父級,或者覆蓋表createCell方法來爲該情況生成按鈕(前者更容易)。

使用requestFocus()代替setFocus()。

+0

在表類上重寫createCell方法並沒有解決問題! plz再次看到我的線程 – JavaFan

+0

它不工作?你是否達到了這個規範? 這應該工作,但你也應該檢查行,因爲這個方法也被調用的標題單元格。 –

+0

對不起,它完美的作品,我沒有注意到。 – JavaFan

相關問題