问题
My code belongs to LWUIT apps but problem about something common between LWUIT and java swing.
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)
{
}
}
}
);
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);
}
} ;
回答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().
来源:https://stackoverflow.com/questions/20514181/actionevent-of-a-component-on-a-table-cell-doesnt-take-place