JCell value not set and gets copied
Hello All,
In the below code, I am trying to have Text area and button in each cell of the table.
Problem:
- When I type something in the cell, the value is not set there.
- When I click on another cell, the previously typed values get copied.
Any ideas how to solve this?
Thanks for your help in advance,
Irfaan
package Test;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
/**
* This example shows a keyboard/focus navigation problem when using a JPanel
* as a table cell editor. The JPanel contains a textfield and a button.
*
* Click in the editor to invoke it, and tab-off (or press cursor down). The
* edit ends, but the focus disappears rather than moving to the next cell in
* the table.
*
* Change the line that returns the panel to return a text field instead, and
* all is well.
*/
publicclass Test
{
publicstaticvoid main( String[] args )
{
new Test();
}
public Test()
{
JFrame f =new JFrame();
f.setContentPane(new TablePanel() );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize( 800, 600 );
f.setVisible(true );
}
class TablePanelextends JPanel
{
TablePanel()
{
setLayout(new BorderLayout() );
JTable table =new JTable();
table.setDefaultEditor( Object.class,new MyEditor() );
table.setModel(
new DefaultTableModel(
new Object[][]
{
{null,null},
{null,null},
{null,null},
{null,null}
},
new String[]{"A","B"} ) );
add( table, BorderLayout.CENTER );
}
}
class MyEditorextends AbstractCellEditorimplements TableCellEditor
{
private JComponent myEditorPanel;
private JTextField myTextField;
MyEditor()
{
myEditorPanel =new MyEditorPanel();
myTextField =new JTextField();
}
public Object getCellEditorValue()
{
returnnull;
}
public Component getTableCellEditorComponent( JTable table, Object value,
boolean isSelected,
int row,int column )
{
return myEditorPanel;// Problems...
//return myTextField;// Keyboard and focus as expected
}
}
class MyEditorPanelextends JPanel
{
JTextField tf;
MyEditorPanel()
{
setLayout(new BorderLayout() );
tf =new JTextField();
add( tf, BorderLayout.CENTER );
add(new JButton("edit" ), BorderLayout.EAST );
}
}
}

