![](/img/trans.png)
[英]Why doesn't click in JContextMenu invoked in JDialog fire an ActionEvent?
[英]ActionEvent of a component on a table cell doesn't take place
我的代碼屬於LWUIT應用程序,但是LWUIT和Java swing之間有一些共同點。
我有一張桌子,在它的最后一個單元格上設置了一個Button作為組件
我的問題很簡單,為什么當我按下該按鈕時為什么沒有動作。
我也嘗試過復選框,但是我什至無法檢查。
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);
}
} ;
您不應該在表中添加按鈕,也不應該調用setFocus()
。
將按鈕作為單獨的組件添加到公共父級,或者重寫表createCell方法以針對這種情況生成按鈕(前者更容易)。
使用requestFocus()代替setFocus()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.