I have an editable JTable. As the user types, if the text is longer than the width I need the height to grow. I have set linewrap to true, but it only changes the height after the user presses enter. What am I missing? I have looked at answers that solve the resizing issue (such as this in the cellRenderer, but I need to adjust the height as the user types, not after they are finished typing.
public class EndCycleCellEditor extends AbstractCellEditor implements TableCellEditor, KeyListener {
JComponent component;
private ArrayList<ArrayList<Integer>> rowColHeight = new ArrayList<ArrayList<Integer>>();
public EndCycleCellEditor(){
component = new JTextArea();
((JTextArea) component).setWrapStyleWord(true);
((JTextArea) component).setLineWrap(true);
component.addKeyListener(this);
}
@Override
public Object getCellEditorValue() {
return ((JTextArea) component).getText();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
((JTextArea)component).setText(value.toString());
return component;
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
if(((JTextArea) component).getText().length() >= 200){
Toolkit.getDefaultToolkit().beep();
((JTextArea)component).setText(((JTextArea)component).getText().substring(0,200) );
}
}
}
1) resize this way is possible, but ugly and not user_friendly
2) don't use non_standard hack as MultiLines span in the JTable
3) put JTextArea to the JScrollPane, but then you have to override Scrolling
JScrollPane
inside anotherJScrollPane