JTable (getCellEditorValue)
Von: , Frage gestellt am Mo, 10. Mai 2004
Hallo,
ich habe für mein Tabellenfeld einen Renderer und einen Editor.
Im Feld wird jeweils ein Icon und ein Text angezeigt.
Mein Problem ist, dass wenn ich das erste Feld editiere wird der geänderte Text auch gespeichert. Bei jedem weiteren Feld nihct. Ich kann zwar den Text ändern aber wenn ich das Feld verlasse steht wieder der alte text drin. Die Methode "getCellEditorValue" wird scheinbar nur beim ersten Feld aufgerufen und dann nie wieder.
Was ist falsch ?
Danke
Arni
Hier ist mein Editor:
public class TableObjectEditor
extends AbstractCellEditor implements TableCellEditor
{
protected EventListenerList listenerList = new EventListenerList () ;
protected Object value ;
protected ChangeEvent changeEvent = null ;
protected int clickCountToStart = 1 ;
TableObjectRenderer renderer = new TableObjectRenderer () ;
public TableObjectEditor ()
{
}
public Component getTableCellEditorComponent ( JTable table , Object value ,
boolean isSelected , int row , int column )
{
JTextField textField = renderer.getTextField () ;
textField.setBorder ( new BevelBorder ( BevelBorder.LOWERED ) ) ;
JLabel label = renderer.getLabel () ;
if ( value instanceof ObjectsToView )
{
ObjectsToView location = ( ObjectsToView ) value ;
textField.setText ( ( String ) location.getText () ) ;
ImageIcon icon = ( ImageIcon ) location.getIcon () ;
label.setIcon ( icon ) ;
}
else
{
textField.setText ( ( String ) value ) ;
}
return renderer ;
}
public Object getCellEditorValue ()
{
System.out.println("test");
return value ;
}
public void setCellEditorValue ( Object value )
{
this.value = value ;
}
public void setClickCountToStart ( int count )
{
clickCountToStart = count ;
}
public int getClickCountToStart ()
{
return clickCountToStart ;
}
public boolean isCellEditable ( EventObject eo )
{
if ( eo instanceof MouseEvent )
{
if ( ( ( MouseEvent ) eo ).getClickCount () < clickCountToStart )
{
return false ;
}
}
return true ;
}
public boolean shouldSelectCell ( EventObject eo )
{
return true ;
}
public boolean stopCellEditing ()
{
JTextField textField = renderer.getTextField () ;
setCellEditorValue ( textField.getText () ) ;
System.out.println(textField.getText ());
fireEditingStopped () ;
return true ;
}
public void cancelCellEditing ()
{
fireEditingCanceled () ;
}
public void addCellEditorListener ( CellEditorListener l )
{
listenerList.add ( CellEditorListener.class , l ) ;
}
public void removeCellEditorListener ( CellEditorListener l )
{
listenerList.remove ( CellEditorListener.class , l ) ;
}
protected void fireEditingStopped ()
{
Object[] listeners = listenerList.getListenerList () ;
for ( int i = listeners.length - 2 ; i >= 0 ; i -= 2 )
{
if ( listeners[ i ] == CellEditorListener.class )
{
if ( changeEvent == null )
{
changeEvent = new ChangeEvent ( this ) ;
( ( CellEditorListener )
listeners[ i + 1 ] ).editingStopped ( changeEvent ) ;
}
}
}
}
protected void fireEditingCanceled ()
{
Object[] listeners = listenerList.getListenerList () ;
for ( int i = listeners.length - 2 ; i >= 0 ; i -= 2 )
{
if ( listeners[ i ] == CellEditorListener.class )
{
if ( changeEvent == null )
{
changeEvent = new ChangeEvent ( this ) ;
( ( CellEditorListener )
listeners[ i + 1 ] ).editingCanceled ( changeEvent ) ;
}
}
}
}
}
************************
Hier ist der Renderer
public class TableObjectRenderer
extends JPanel
implements TableCellRenderer
{
int rows ;
private JTextField textField = new JTextField () ;
private JLabel label = new JLabel () ;
private Color unselectedForeground ;
private Color unselectedBackground ;
// protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public TableObjectRenderer ()
{
this ( 1 ) ;
}
public TableObjectRenderer ( int rows )
{
this.rows = rows ;
createPanel () ;
}
public void createPanel ()
{
this.setBorder ( null ) ;
textField.setBorder ( null ) ;
setLayout ( new BorderLayout () ) ;
add ( "West" , label ) ;
add ( "Center" , textField ) ;
}
public Component getTableCellRendererComponent ( JTable table ,
Object value , boolean isSelected , boolean hasFocus , int row ,
int column )
{
if ( value instanceof ObjectsToView )
{
ObjectsToView location = ( ObjectsToView ) value ;
textField.setText ( ( String ) location.getText () ) ;
ImageIcon icon = ( ImageIcon ) location.getIcon () ;
label.setIcon ( icon ) ;
}
else
{
textField.setText ( ( String ) value ) ;
}
textField.setAlignmentX ( JTextArea.LEFT_ALIGNMENT ) ;
textField.setAlignmentY ( JTextArea.TOP_ALIGNMENT ) ;
if ( isSelected )
{
textField.setForeground ( table.getSelectionForeground () ) ;
textField.setBackground ( table.getSelectionBackground () ) ;
}
else
{
textField.setForeground ( ( unselectedForeground != null ) ?
unselectedForeground :
table.getForeground () ) ;
textField.setBackground ( ( unselectedBackground != null ) ?
unselectedBackground :
table.getBackground () ) ;
}
if ( hasFocus )
{
// setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
if ( table.isCellEditable ( row , column ) )
{
textField.setForeground ( UIManager.getColor (
"Table.focusCellForeground" ) ) ;
textField.setBackground ( UIManager.getColor (
"Table.focusCellBackground" ) ) ;
}
}
else
{
// setBorder(noFocusBorder);
}
return this ;
}
public void setRows ( int rows )
{
this.rows = rows ;
}
public int getRows ()
{
return rows ;
}
public JTextField getTextField ()
{
return textField ;
}
public JLabel getLabel ()
{
return label ;
}
}
