0

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);
 }
 } ;
PHPFan
  • 756
  • 3
  • 12
  • 46
  • 1
    Please format your code correctly – Paul Samsotha Dec 11 '13 at 08:39
  • You need to use custom `CellEditor` with `JButton`, take a look at [Table Button Column](http://tips4java.wordpress.com/2009/07/12/table-button-column/) – alex2410 Dec 11 '13 at 09:00
  • please can you explain this code for me , getActionCommand() couldn't be used in LWUIT app public void actionPerformed(ActionEvent e) { JTable table = (JTable)e.getSource(); int modelRow = Integer.valueOf( e.getActionCommand() ); ((DefaultTableModel)table.getModel()).removeRow(modelRow); } – PHPFan Dec 11 '13 at 10:46
  • Renderer is responsible for painting in the JTables view, don't put JComponent to the XxxTableModel, there is stored only value, description, for rendered component, many times Q&A here, incl. hover_over effect, basically everything is described in oracle tutorial - how to use tables – mKorbel Dec 11 '13 at 11:47
  • @mKorbel Are you mean My Idea is totally wrong? – PHPFan Dec 11 '13 at 11:56
  • yes, but nothing futher, cleaver without an SSCCE – mKorbel Dec 11 '13 at 11:57
  • @mkorbel See this code works normally http://tips4java.wordpress.com/2009/07/12/table-button-column/ – PHPFan Dec 11 '13 at 12:35
  • yes true very good code, quite refference code – mKorbel Dec 11 '13 at 19:05
  • Anyone suggest please? – PHPFan Dec 12 '13 at 10:09

1 Answers1

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

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Overriding createCell method on table class didn't solve the problem! plz see my thread again – PHPFan Dec 25 '13 at 23:28
  • How is it not working? Did you reach that code? That should work but you should also check the row since this method is also invoked for the header cell. – Shai Almog Dec 26 '13 at 05:37
  • I am sorry it works perfectly, I didn't notice well. – PHPFan Dec 31 '13 at 00:01