我的代碼屬於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);
}
} ;
請正確格式化您的代碼 –
您需要在'JButton'中使用自定義'CellEditor',看一下[Table Button Column](http://tips4java.wordpress.com/2009/07/12/table-按鈕列/) – alex2410
請你能爲我解釋這段代碼,getActionCommand()不能用在LWUIT應用程序中public void actionPerformed(ActionEvent e) JTable table =(JTable)e.getSource(); int modelRow = Integer.valueOf(e.getActionCommand()); ((DefaultTableModel)table.getModel())。removeRow(modelRow); } – JavaFan