GreyTeardrop
6/25/2011 - 6:59 PM

MultilineTableCellEditor.java

public class MultilineTableCellEditor extends DefaultCellEditor {
	private Object previousValue;
	private final JTextArea textArea;

	public MultilineTableCellEditor() {
		super(new JTextField());
		textArea = new JTextArea();
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		editorComponent = scrollPane;

		delegate = new EditorDelegate() {
			@Override
			public void setValue(Object value) {
				textArea.setText((value == null) ? "" : value.toString());
			}

			@Override
			public Object getCellEditorValue() {
				return textArea.getText();
			}
		};
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		previousValue = value;
		return super.getTableCellEditorComponent(table, value, isSelected, row, column);
	}

	@Override
	public boolean stopCellEditing() {
		String currentValue = textArea.getText();
		if (!currentValue.equals(previousValue)) {
			int confirmation = JOptionPane.showConfirmDialog(null, "Сохранить изменения?",
				"Обновление записи", JOptionPane.YES_NO_OPTION);
			if (confirmation == JOptionPane.NO_OPTION) {
				cancelCellEditing();
				return false;
			}
		}
		return super.stopCellEditing();
	}
}