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 );

}

}

}

[5271 byte] By [Irfan_dcrtia] at [2007-11-27 11:25:41]
# 1

In your MyEditor Class you define a textfield.... then you add another textfield in your editor panel

> class MyEditor extends AbstractCellEditor

> implements TableCellEditor

> {

>private JComponent myEditorPanel;

>private JTextField myTextField;

>MyEditor()

> {

>myEditorPanel = new MyEditorPanel();

> myTextField = new JTextField();

>}

>public Object getCellEditorValue()

> {

>return null;

>

>

> public Component getTableCellEditorComponent(

> JTable table, Object value,

>

> oolean isSelected,

> int

> row, int column )

>{

>return myEditorPanel;// Problems...

> return myTextField;// Keyboard and focus as

> expected

>}

>

>

> class MyEditorPanel extends JPanel

> {

>JTextField tf;

>MyEditorPanel()

> {

>setLayout( new BorderLayout() );

> tf = new JTextField();

>add( tf, BorderLayout.CENTER );

> add( new JButton( "edit" ), BorderLayout.EAST );

>}

>

Add an action listener to both the textfield and the button and ensure that editingStopped() is called when the user presses enter

c0demonk3ya at 2007-7-29 16:05:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hey, Thanks for your reply. how do I get the ChangeEvent? I need to pass the ChangeEvent parameter for edittingStopped right?

for example, if I am calling table.edittingStopped(ChangeEvent e). What should the e be? How can I get it from my view?

Irfan_dcrtia at 2007-7-29 16:05:54 > top of Java-index,Desktop,Core GUI APIs...