I am trying to implement a JTable that uses a JTextArea for the Rendering and Editing component for a column. The rendering seems to work but the editor does not save the changes made. I suspect I am not handling events properly. When I click on a cell in the column using the JTextArea editor component, it displays the JTextArea properly and input is allowed. However, when I leave the cell, the changes I have made are gone. The custom editor code is shown below.
class JTextAreaEditor extends AbstractCellEditor implements TableCellEditor{
String text = null;
private JPanel panel = new JPanel();
private JScrollPane jsp;
private JTextArea jta;
public JTextAreaEditor(int rows, int cols){
panel.setLayout(new BorderLayout());
jta = new JTextArea(rows, cols);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jsp = new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(jsp, BorderLayout.CENTER);
jta.addFocusListener(new FocusListener(){
public void focusLost(FocusEvent e){
fireEditingStopped();
}
public void focusGained(FocusEvent e){
}
});
}
protected void fireEditingStopped(){
super.fireEditingStopped();
}
public Object getCellEditorValue(){
return text;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
jta.setText(value.toString());
text = value.toString();
return panel;
}
}
The custom renderer is as follows:
class JTextAreaRenderer extends JPanel implements TableCellRenderer{
protected static Border m_noFocusBorder;
protected JScrollPane jsp;
protected JTextArea jta;
public JTextAreaRenderer(int rows, int cols){
super();
setLayout(new BorderLayout());
jta = new JTextArea(rows, cols);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jsp = new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(jsp, BorderLayout.CENTER);
m_noFocusBorder = new EmptyBorder(1, 2, 1, 2);
setOpaque(true);
setBorder(m_noFocusBorder);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
String s = (String)value;
jta.setText(s);
jta.setLineWrap(true);
setBackground(isSelected && !hasFocus ? table.getSelectionBackground() : table.getBackground());
setForeground(isSelected && !hasFocus ? table.getSelectionForeground() : table.getForeground());
setFont(table.getFont());
setBorder(hasFocus? UIManager.getBorder("Table.focusCellHighlightBorder" : m_noFocusBorder);
return this;
}
}
If you can show me what I am doing wrong I would greatly appreciate it. I have looked through the Java Tutorial, The Swing Connection, the chapter in the book "Swing" as well as John Zukowski's Swing book and haven't found the answer. Thank you.
class JTextAreaEditor extends AbstractCellEditor implements TableCellEditor{
String text = null;
private JPanel panel = new JPanel();
private JScrollPane jsp;
private JTextArea jta;
public JTextAreaEditor(int rows, int cols){
panel.setLayout(new BorderLayout());
jta = new JTextArea(rows, cols);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jsp = new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(jsp, BorderLayout.CENTER);
jta.addFocusListener(new FocusListener(){
public void focusLost(FocusEvent e){
fireEditingStopped();
}
public void focusGained(FocusEvent e){
}
});
}
protected void fireEditingStopped(){
super.fireEditingStopped();
}
public Object getCellEditorValue(){
return text;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
jta.setText(value.toString());
text = value.toString();
return panel;
}
}
The custom renderer is as follows:
class JTextAreaRenderer extends JPanel implements TableCellRenderer{
protected static Border m_noFocusBorder;
protected JScrollPane jsp;
protected JTextArea jta;
public JTextAreaRenderer(int rows, int cols){
super();
setLayout(new BorderLayout());
jta = new JTextArea(rows, cols);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jsp = new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(jsp, BorderLayout.CENTER);
m_noFocusBorder = new EmptyBorder(1, 2, 1, 2);
setOpaque(true);
setBorder(m_noFocusBorder);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
String s = (String)value;
jta.setText(s);
jta.setLineWrap(true);
setBackground(isSelected && !hasFocus ? table.getSelectionBackground() : table.getBackground());
setForeground(isSelected && !hasFocus ? table.getSelectionForeground() : table.getForeground());
setFont(table.getFont());
setBorder(hasFocus? UIManager.getBorder("Table.focusCellHighlightBorder" : m_noFocusBorder);
return this;
}
}
If you can show me what I am doing wrong I would greatly appreciate it. I have looked through the Java Tutorial, The Swing Connection, the chapter in the book "Swing" as well as John Zukowski's Swing book and haven't found the answer. Thank you.